1 / 30

Chapter 17 Linked List

Chapter 17 Linked List. Bernard Chen Spring 2006. Arrays contiguous direct access of elements insertion / deletion difficult Linked Lists noncontiguous must scan for element insertion /deletion easy. arrayname. Comparison with array. Iterating through a data structure. a.

Download Presentation

Chapter 17 Linked List

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 17Linked List Bernard Chen Spring 2006

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

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

  4. A0 A1 A2 first last Adding an element 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;

  5. 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 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;

  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 current first Inserting an element 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;

  10. A0 A1 A2 current first last tmp Inserting an element 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 x first last tmp Inserting an element 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;

  12. A0 A1 A2 current x first last tmp Inserting an element 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;

  13. A0 A1 A2 current x first last tmp Inserting an element 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;

  14. Simplified version Current->next = new ListNode(x, current->next) Current->next = tmp; tmp = new ListNode(); Tmp->element = x; Tmp->next = current->next;

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

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

  17. Delete a Node Node *deletedNode = current->next; Current->next = current->next->next; Delete deletedNode;

  18. Header node 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.

  19. a b c head tail Doubly Linked Lists A doubly linked list allows bidirectional traversal by storing two pointers per node. class DoubleListNode { Object element; ListNode* next; ListNode* prev; }

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

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

  22. 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

  23. 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

  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 current b head tail 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. Circular Linked Lists a b c d first

  29. Sorted Linked List • A sorted link list is one in which items are in sorted order. It can be derived from a list class. • The major difference from linked list is the insertion operation.

  30. In Class exercise • Suppose you have a pointer to a node in a single linked list that is guaranteed not to be the last node in the list. You don’t have pointers to any other nodes. 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.

More Related