1 / 64

Chapter 20 Implementing lists : Linked implementations

Chapter 20 Implementing lists : Linked implementations. Objectives. After studying this chapter you should understand the following: the design and structure of linked list implementations; variations in designs for linked lists, including doubly linked lists;

owensjoshua
Télécharger la présentation

Chapter 20 Implementing lists : Linked implementations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 20 Implementing lists : Linked implementations

  2. Objectives • After studying this chapter you should understand the following: • the design and structure of linked list implementations; • variations in designs for linked lists, including doubly linked lists; • advantages and disadvantages of linked vs. array-based implementations. • Also, you should be able to: • write algorithms for linked-based structures; • define classes implemented with linked-based structures. NH-Chapter 20

  3. Linked implementations • Structure built from collection of nodes that reference each other. • A node • contains reference to a list element, • one or more variables referencing other structure nodes. NH-Chapter 20

  4. node A B C D First linked implementation • LinkedList: Each node references node containing next list element. NH-Chapter 20

  5. Class Node • Class Node, local to LinkedList, models nodes. • Node structure: • must know an element of the List, • must know the next Node in sequence. • As local class of LinkedList has no methods. NH-Chapter 20

  6. Class Node implementation public class LinkedList<Element> extends AbstractList<Element> { … private class Node { private Element element; private Node next; // Create a Node containing specified element. public Node (Element element) { this.element = element; this.next = null; } } // end of class Node } // end of class LinkedList NH-Chapter 20

  7. LinkedList implementation • public class LinkedList<Element> extends AbstractList<Element> { • private int size; • private Node first; • // Create an empty LinkedList<Element>. • public LinkedList () { • size = 0; • first = null; • } • … • } NH-Chapter 20

  8. An empty linked list NH-Chapter 20

  9. Linked list with three entries NH-Chapter 20

  10. Method to get the i-th node /** * The i-th node of this LinkedList. * The LinkedList must be non-empty. * require 0 <= i && i < this.size() */ private Node getNode (int i) { Node p = first; int pos = 0; // p is pos-th Node while (pos != i) { p = p.next; pos = pos + 1; } return p; } NH-Chapter 20

  11. pos 0 Method to get the i-th node : i == 2 LinkedList size 4 first p Node Node Node Node next next next next element É element É element É element É NH-Chapter 20

  12. pos 1 Method to get the i-th node : i == 2 LinkedList size 4 first p Node Node Node Node next next next next element É element É element É element É NH-Chapter 20

  13. pos 2 Method to get the i-th node : i == 2 LinkedList size 4 first p Node Node Node Node next next next next element É element É element É element É NH-Chapter 20

  14. Implementing LinkedList methods public Element get (int index) { Node p = getNode(index); return p.element; } • Note: method get • uses getNode • get(i) will take i+1 steps to get the element. NH-Chapter 20

  15. Method remove(int) • Given an index referencing a node to be removed: • Find previous node referencing it. • Update that node’s next. NH-Chapter 20

  16. Method remove(int) • Find previous node. LinkedList Element to be removed size 4 first Node Node Node Node next next next next element É element É element É element É NH-Chapter 20

  17. Method remove(int) LinkedList Element to be removed size 4 first p Node Node Node Node next next next next element É element É element É element É NH-Chapter 20

  18. LinkedList Element to be removed size 4 first p Node Node Node Node next next next next element É element É element É element É Method remove(int) NH-Chapter 20

  19. LinkedList Element to be removed size 4 first p Node Node Node Node next next next next element É element É element É element É Copy Method remove(int) NH-Chapter 20

  20. Method remove(int) LinkedList Element to be removed size 4 first p Node Node Node Node next next next next element É element É element É element É NH-Chapter 20

  21. Method remove(int) LinkedList Element to be removed size 3 first p Node Node Node Node next next next next element É element É element É element É NH-Chapter 20

  22. Method remove(int), special case • Recall getNode requires argument >= 0. • If index is 0, it’s not legal to write Node p = getNode(index-1); • Special case: index 0 removes first linked element. • instance variable first of LinkedList must be modified. NH-Chapter 20

  23. Method remove(int) LinkedList size 4 node to be removed first Node Node Node Node next next next next element É element É element É element É NH-Chapter 20

  24. Method remove(int) LinkedList size 4 node to be removed first Node Node Node Node next next next next element É element É element É element É copy NH-Chapter 20

  25. LinkedList size 4 first Method remove(int) node to be removed Node Node Node Node next next next next element É element É element É element É NH-Chapter 20

  26. LinkedList size 3 first Method remove(int) node to be removed Node Node Node Node next next next next element É element É element É element É NH-Chapter 20

  27. Implementation of remove(int) public void remove (int index) { if (index == 0) first = first.next; else { Node p = getNode(index-1); p.next = p.next.next; } size = size - 1; } NH-Chapter 20

  28. Method add (int, Element) • To add an element at the i-th position • must find the element with index i-1 • special case to consider when i is 0. NH-Chapter 20

  29. LinkedList Node size 4 Node Node Node Node Element to add first next next next next next element É element É element É element É element É Method add(int, Element) • Add an element at index 2. • Must find previous node. NH-Chapter 20

  30. p LinkedList Node size 4 Node Node Node Node Element to add first next next next next next element É element É element É element É element É Method add(int, Element) NH-Chapter 20

  31. p LinkedList Node size 4 Node Node Node Node Element to add first next next next next next element É element É element É element É element É Method add(int, Element) • New node next is p’s next NH-Chapter 20

  32. p LinkedList Node size 4 Element to add first next element É Method add(int, Element) • p node next is new node Node Node Node Node next next next next element É element É element É element É NH-Chapter 20

  33. p LinkedList Node size 5 Element to add first next element É Method add(int, Element) • LinkedList size is incremented. Node Node Node Node next next next next element É element É element É element É NH-Chapter 20

  34. LinkedList Node size 4 Element to add first next element É Method add(int, Element) • add at index 0 Node Node Node Node next next next next element É element É element É element É NH-Chapter 20

  35. LinkedList Node size 4 Element to add first next element É Method add(int, Element) • add at index 0 Node Node Node Node next next next next element É element É element É element É NH-Chapter 20

  36. LinkedList Node size 5 Element to add first next element É Method add(int, Element) • add at index 0 Node Node Node Node next next next next element É element É element É element É NH-Chapter 20

  37. Method add (int, Element) public void add (int index, Element element) { Node newNode = new Node(element); if (index == 0) { newNode.next = first; first = newNode; } else { Node p = getNode(index-1); newNode.next = p.next; p.next = newNode; } size = size + 1; } NH-Chapter 20

  38. Linked list variations • Adding a reference to last node makes adding to end of list constant. NH-Chapter 20

  39. LinkedList: adding a last reference • Creates special cases in other methods. For instance, remove public void remove (int index) { if (size == 1) { // remove only element first = null; last = null; }else if (index == 0) {//remove first element first = first.next; }else { Node p = getNode(index-1); p.next = p.next.next; if (index == size-1) //last element removed last = p; } size = size - 1; } NH-Chapter 20

  40. Header nodes • “dummy” node • Always present at front of list, • contains no element, • first always references header node, • first is never null. • Reduces special cases in the implementation of some methods. NH-Chapter 20

  41. Header nodes NH-Chapter 20

  42. Circular lists • Last node references the first. NH-Chapter 20

  43. Doubly-linked lists NH-Chapter 20

  44. Doubly-linked list constructor • Assuming Header extends NodeDoublyLinkedList constructor creates header Node, and links it to itself. public DoublyLinkedList () { size = 0; header = new Header(); header.next = header; header.previous = header; } NH-Chapter 20

  45. Doubly-linked list constructor NH-Chapter 20

  46. Doubly-linked lists: add(Element) Header A B C New NH-Chapter 20

  47. Doubly-linked lists: add(Element) • new node previous is node C Header A B C New NH-Chapter 20

  48. Doubly-linked lists: add(Element) • new node next is header Header A B C New NH-Chapter 20

  49. Doubly-linked lists: add(Element) • Header previous is new node Header A B C New NH-Chapter 20

  50. Doubly-linked lists: add(Element) • C node next is new node Header A B C New NH-Chapter 20

More Related