ADT Implementation – Linked List
1
Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - 3131
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
1
Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - 3131
2
3
4
5
6
7
data data
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
– 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
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
public class DLinkBag <T> implements InterfaceBag <T> { DLNode firstNode; int numberOfEntries; public DLinkBag() { firstNode=null; numberOfEntries=0; }
… ….
27
28
null
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
31
32
33
– 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