List Tessema M. Mengistu Department of Computer Science Southern - - PowerPoint PPT Presentation

list
SMART_READER_LITE
LIVE PREVIEW

List Tessema M. Mengistu Department of Computer Science Southern - - PowerPoint PPT Presentation

ADT Implementation Linked List Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - 3131 1 Outline Linked Lists BagInterface Linked list Implementation


slide-1
SLIDE 1

ADT Implementation – Linked List

1

Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - 3131

slide-2
SLIDE 2

Outline

  • Linked Lists
  • BagInterface Linked list Implementation
  • Pros and Cons
  • Variants of Linked Lists

2

slide-3
SLIDE 3

Linked Lists

  • Linked lists are container types that store

collections of data in a sequential order

  • A linked list can be represented as a collection
  • f data objects (Nodes) tied together in a

chain, with each object keeping track of the next object in the list.

3

slide-4
SLIDE 4

Linked Lists

  • Has at least two parts:

– The data - data portion – The link to the next object – the link portion

  • The object often called Node

4

slide-5
SLIDE 5

Terminology

  • The first node is called the header(first) node
  • A node’s successor is the next node in the

sequence

– The last node has no successor (its link portion is null)

  • A node’s predecessor is the previous node in the

sequence

– The first node has no predecessor

  • A list’s length is the number of elements in it

– A list may be empty (contain no elements)

5

slide-6
SLIDE 6

Bag Interface

6

slide-7
SLIDE 7

A Linked Implementation

  • f the ADT Bag
  • The private class Node

7

data data

slide-8
SLIDE 8

A Linked Implementation …

8

slide-9
SLIDE 9

A Linked Implementation …

9

slide-10
SLIDE 10

Add Method

  • Two cases:

– Empty bag

10

slide-11
SLIDE 11

Add Method

  • Non Empty bag

11

slide-12
SLIDE 12

Add Method

12

slide-13
SLIDE 13

toArray Method

13

slide-14
SLIDE 14

getFrequencyOf Method

14

slide-15
SLIDE 15

contains Method

15

slide-16
SLIDE 16

Other Methods

  • clear()

16

slide-17
SLIDE 17

More Methods …

  • isEmpty()

public boolean isEmpty()

{ return firstNode==null; }

  • getCurrentSize()

17

slide-18
SLIDE 18

remove Method

  • remove()

18

slide-19
SLIDE 19

remove Method

19

slide-20
SLIDE 20

remove Method

  • Remove a specific entry

Locate a node N that contains anEntry if (node N exists) { Replace the entry in node N with the entry in the first node remove the first node } return true or false according to whether the operation succeeds

20

slide-21
SLIDE 21

remove Method

21

slide-22
SLIDE 22

remove Method

22

slide-23
SLIDE 23

Pros and Cons of Using a Chain

  • Pros

– Bag can grow and shrink in size as necessary – Possible to remove and recycle nodes – Copying values for array enlargement not needed

  • Cons

– Removing specific value entry requires search of array or chain – Chain requires more memory than array of same length

23

slide-24
SLIDE 24

Variants of Listed Lists

  • Different variants

– Sorted Vs Unsorted – Singly linked Vs doubly linked – Circular

24

slide-25
SLIDE 25

Variants of Listed Lists

  • Double Linked List

– Consists of nodes with two link members one points to the previous and other to the next node – Maximizes the needs of list traversals – Compared to the Singled list inserting and deleting nodes is a bit slower as both the links has to be updated – It requires the extra storage space for the second link

25

slide-26
SLIDE 26

Double Linked List

private class DLNode { private T data; private DLNode next; private DLNode prev; private DLNode(T dataPortion) { this(dataPortion, null,null); } private DLNode (T dataPortion, DLNode next, DLNode prev) { this.data=dataportion; this.next=next; this.prev=prev; } }

26

slide-27
SLIDE 27

Double Linked List

public class DLinkBag <T> implements InterfaceBag <T> { DLNode firstNode; int numberOfEntries; public DLinkBag() { firstNode=null; numberOfEntries=0; }

<the rest of the methods comes here>

… ….

}

27

slide-28
SLIDE 28

Method add()

  • Two cases

– If empty

28

null

slide-29
SLIDE 29
  • If not empty

29

slide-30
SLIDE 30

public boolean add(T anEntry) { DLNode entry = new DLNode(anEntry,null,null); boolean result= false; if(firstNode == null) { firstNode=entry; numberOfEntries++; result=true; } else { entry.next=firstNode; firstNode.prev=entry; firstNode=entry; numberOfEntries++; result=true; } return result; }

30

slide-31
SLIDE 31

Method remove

  • First node

firstNode = firstNode.next; firstNode.prev=null;

31

slide-32
SLIDE 32

Remove Method

  • Specific node

(current.next).prev = current.prev; (current.prev).next = current.next;

32

slide-33
SLIDE 33

Remove Method

(currentNode.prev).next =null; currentNode.prev=null;

33

slide-34
SLIDE 34

Different Variants of Lists

  • Circular Linked list

– Similar to the doubly linked list, but the last node link points to the first node instead of null – Useful in algorithms where there is no particular first or last item – Advantage is we can make head to point any node without destroying the list – Operations are similar to single linked list except identifying the last node in the list. To find the last node compare the last node to head node (i.e you have found the last node if its link points to the same node that the head points)

34

slide-35
SLIDE 35

35