list
play

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. ADT Implementation – Linked List Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - 3131 1

  2. Outline • Linked Lists • BagInterface Linked list Implementation • Pros and Cons • Variants of Linked Lists 2

  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 of data objects ( Nodes ) tied together in a chain, with each object keeping track of the next object in the list. 3

  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

  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

  6. Bag Interface 6

  7. A Linked Implementation of the ADT Bag • The private class Node data data 7

  8. A Linked Implementation … 8

  9. A Linked Implementation … 9

  10. Add Method • Two cases: – Empty bag 10

  11. Add Method • Non Empty bag 11

  12. Add Method 12

  13. toArray Method 13

  14. getFrequencyOf Method 14

  15. contains Method 15

  16. Other Methods • clear() 16

  17. More Methods … • isEmpty() public boolean isEmpty() { return firstNode==null; } • getCurrentSize() 17

  18. remove Method • remove() 18

  19. remove Method 19

  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

  21. remove Method 21

  22. remove Method 22

  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

  24. Variants of Listed Lists • Different variants – Sorted Vs Unsorted – Singly linked Vs doubly linked – Circular 24

  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

  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

  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

  28. Method add() • Two cases – If empty null 28

  29. • If not empty 29

  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 }

  31. Method remove • First node firstNode = firstNode.next; firstNode.prev=null; 31

  32. Remove Method • Specific node (current.next).prev = current.prev; (current.prev).next = current.next; 32

  33. Remove Method (currentNode.prev).next =null; currentNode.prev=null; 33

  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

  35. 35

Recommend


More recommend