1 / 41

Chapter 17

Chapter 17 . Linked List. Objective. Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked lists. arrayname. Arrays contiguous direct access of elements insertion / deletion difficult Linked Lists noncontiguous

carson
Download Presentation

Chapter 17

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 17 Linked List

  2. Objective • Linked lists • basic ideas: header nodes and iterator classes • Implementation details • doubly linked lists • circular linked lists

  3. arrayname • Arrays • contiguous • direct access of elements • insertion / deletion difficult • Linked Lists • noncontiguous • must scan for element • insertion /deletion easy

  4. a Iterating through a data structure for (int i = 0; i < length; i++) cout<< a[i]; for (ListNode p = theList.first; p != null; p = p.next) cout<< p.data ;

  5. Adding an element A0 A1 A2 first last class ListNode { Object data; ListNode* next; } At any point, we can add a new last item x by doing this: last->next = new ListNode(); last = last->next; last->data = x; last->next = null;

  6. A0 A1 A2 first last class ListNode { Object data; ListNode* next; } At any point, we can add a new last item x by doing this: last->next = new ListNode() ; last = last->next; last->data = x; last->next = null;

  7. A0 A1 A2 first last class ListNode { Object data; ListNode* next; } At any point, we can add a new last item x by doing this: last->next = new ListNode(); last = last->next; last->data = x; last->next = null;

  8. A0 A1 A2 x first last class ListNode { Object data; ListNode* next; } At any point, we can add a new last item x by doing this: last->next = new ListNode(); last = last->next; last->data = x; last->next = null;

  9. A0 A1 A2 x first last class ListNode { Object data; ListNode* next; } At any point, we can add a new last item x by doing this: last->next = new ListNode(); last = last->next; last->data = x; last->next = null;

  10. Inserting an element A0 A1 A2 current first last class ListNode { Object element; ListNode* next; } At any point, we can add a new last item x by doing this: tmp = new ListNode(); tmp->element = x; tmp->next = current->next; Current->next = tmp;

  11. A0 A1 A2 current first last class ListNode { Object element; ListNode* next; } At any point, we can add a new last item x by doing this: tmp = new ListNode(); tmp->element = x; tmp->next = current->next; Current->next = tmp; tmp

  12. A0 A1 A2 current x first last class ListNode { Object element; ListNode* next; } At any point, we can add a new last item x by doing this: tmp = new ListNode(); tmp->element = x; tmp->next = current->next; current->next = tmp; tmp

  13. A0 A1 A2 current x first last class ListNode { Object element; ListNode* next; } At any point, we can add a new last item x by doing this: tmp = new ListNode(); tmp->element = x; tmp->next = current->next; current->next = tmp; tmp

  14. A0 A1 A2 current x first last class ListNode { Object element; ListNode* next; } At any point, we can add a new last item x by doing this: tmp = new ListNode(); tmp->element = x; tmp->next = current->next; current->next = tmp; tmp

  15. Simplified version current->next = new ListNode(x, current->next);

  16. Deleting an element A0 A1 A2 current last class ListNode { Object element; ListNode* next; } current->next = current->next->next;

  17. Deleting an element A0 A1 A2 current last class ListNode { Object element; ListNode* next; } Current->next = current->next->next; Memory leak!

  18. Deleting an element A0 A1 A2 current last Node *deletedNode = current->next; current->next = current->next->next; delete deletedNode;

  19. Header Nodes a b c header Header nodes allow us to avoid special cases [in the code] such as insertion of the first element and removal of the last element. The header node holds no data but serves to satisfy the requirement that every node have a previous node. Not necessarily a standard implementation.

  20. C++ implementation • We have a list. • This list consists of listNodes. • In order to access these listNodes, we need an iterator. • Code: online

  21. Doubly Linked Lists a b c head tail Consider how hard it is to back up in a singly linked list. Also consider text editor example class DoubleListNode { Object element; ListNode* next; ListNode* prev; }

  22. Empty Doubly Linked List c head tail // constructor DoubleList() { head = new DoubleListNode (); tail = new DoubleListNode (); head->next = tail; tail->prev = head; }

  23. Inserting into a Doubly Linked List a c head tail current newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode

  24. Inserting into a Doubly Linked List a c b head tail current newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode

  25. Inserting into a Doubly Linked List a c b head tail current newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode

  26. Inserting into a Doubly Linked List a c b head tail current newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode

  27. Inserting into a Doubly Linked List a c b head tail current newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode

  28. Inserting into a Doubly Linked List a c b head tail current newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode

  29. Inserting into a Doubly Linked List a c b head tail current newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode

  30. Deleting an element from a double linked list oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current= oldNode->prev; delete oldNode; a c b head current

  31. Deleting an element from a double linked list a c b head current oldNode oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode;

  32. Deleting an element from a double linked list a c b head current oldNode oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode;

  33. Deleting an element from a double linked list a c b head current oldNode oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode;

  34. Deleting an element from a double linked list a c b head current oldNode oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode;

  35. Deleting an element from a double linked list a c head current oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode;

  36. Circular Linked lists a b c d first

  37. Sorted Linked List A sorted link list is one in which items are in sorted order. It can be derived from a list class. code

  38. Common errors (Page 599 ) • Splicing in nodes incorrectly when performing insertion • Forgetting incomplete class declaration • Calling delete at wrong time during remove() • More errors on page 599 are given

  39. In class exercises … • Question 17.3 from the book. Write an algorithm for printing a singly linked list in reverse. (Don’t use recursion).

  40. In class exercises • Question 17.7 from the book. Suppose that you have a pointer to a node in singly linked list that guaranteed not to be the last node in the list. You do not have pointers to any other nodes (except by following links). Describe an O(1) algorithm that logically removes the value stored in such a node from the linked list, maintaining the integrity of the linked list (Hint: Involve the next node)

  41. Programming Homework • Implement a linked list • On line • Due Feb 28th.

More Related