ADT Implementation Linked Listtmengistu/Courses/Fall2015/CS220/Slides/ADT... · ADT Implementation...

35
ADT Implementation Linked List 1 Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale [email protected] Room - 3131

Transcript of ADT Implementation Linked Listtmengistu/Courses/Fall2015/CS220/Slides/ADT... · ADT Implementation...

ADT Implementation – Linked List

1

Tessema M. MengistuDepartment of Computer Science

Southern Illinois University [email protected]

Room - 3131

Outline

• Linked Lists

• BagInterface Linked list Implementation

• Pros and Cons

• Variants of Linked Lists

2

Linked Lists

• Linked lists are container types that storecollections of data in a sequential order

• A linked list can be represented as a collectionof data objects (Nodes) tied together in achain, with each object keeping track of thenext object in the list.

3

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

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

Bag Interface

6

A Linked Implementation of the ADT Bag

• The private class Node

7data data

A Linked Implementation …

8

A Linked Implementation …

9

Add Method

• Two cases:

– Empty bag

10

Add Method

• Non Empty bag

11

Add Method

12

toArray Method

13

getFrequencyOf Method

14

contains Method

15

Other Methods

• clear()

16

More Methods …• isEmpty()

public boolean isEmpty()

{

return firstNode==null;

}

• getCurrentSize()

17

remove Method

• remove()

18

remove Method

19

remove Method

• Remove a specific entryLocate 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

remove Method

21

remove Method

22

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 ofarray or chain

– Chain requires more memory than array of samelength

23

Variants of Listed Lists

• Different variants

– Sorted Vs Unsorted

– Singly linked Vs doubly linked

– Circular

24

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

Double Linked Listprivate 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

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

Method add()• Two cases

– If empty

28

null

• If not empty

29

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

Method remove

• First node

firstNode = firstNode.next;

firstNode.prev=null;

31

Remove Method

• Specific node

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

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

32

Remove Method

(currentNode.prev).next =null;

currentNode.prev=null;

33

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

35