Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes...
-
Upload
randolph-mcbride -
Category
Documents
-
view
236 -
download
0
Transcript of Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes...
![Page 1: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/1.jpg)
Chapter 17
Linked List
![Page 2: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/2.jpg)
Objective
Linked lists basic ideas: header nodes and iterator
classes Implementation details doubly linked lists circular linked lists
![Page 3: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/3.jpg)
1. Arrayscontiguousdirect access of elementsinsertion / deletion difficult
2. Linked Listsnoncontiguousmust scan for elementinsertion /deletion easy
1. Arrayscontiguousdirect access of elementsinsertion / deletion difficult
2. Linked Listsnoncontiguousmust scan for elementinsertion /deletion easy
arrayname
![Page 4: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/4.jpg)
for (int i = 0; i < length; i++) cout<< a[i];
for (ListNode p = theList.first; p != null; p = p.next) cout<< p.data ;
for (int i = 0; i < length; i++) cout<< a[i];
for (ListNode p = theList.first; p != null; p = p.next) cout<< p.data ;
aIterating through a data structure
Iterating through a data structure
![Page 5: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/5.jpg)
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;
A0 A1 A2
first last
Adding an element
![Page 6: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/6.jpg)
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;
A0 A1 A2
first last
![Page 7: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/7.jpg)
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;
A0 A1 A2
first last
![Page 8: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/8.jpg)
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;
A0 A1 A2 x
first last
![Page 9: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/9.jpg)
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;
A0 A1 A2 x
first last
![Page 10: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/10.jpg)
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;
A0 A1 A2
first last
Inserting an element
current
![Page 11: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/11.jpg)
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;
A0 A1 A2
first lastcurrent
tmp
![Page 12: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/12.jpg)
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;
A0 A1 A2
first lastcurrent x
tmp
![Page 13: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/13.jpg)
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;
A0 A1 A2
first lastcurrent x
tmp
![Page 14: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/14.jpg)
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;
A0 A1 A2
first lastcurrent x
tmp
![Page 15: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/15.jpg)
Simplified version
current->next = new ListNode(x, current->next);
![Page 16: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/16.jpg)
class ListNode{ Object element; ListNode* next;}
current->next = current->next->next;
A0 A1 A2
Deleting an element
current last
![Page 17: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/17.jpg)
class ListNode{ Object element; ListNode* next;}
Current->next = current->next->next; Memory leak!
A0 A1 A2
last
Deleting an element
current
![Page 18: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/18.jpg)
Node *deletedNode = current->next;current->next = current->next->next;delete deletedNode;
A0 A1 A2
last
Deleting an element
current
![Page 19: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/19.jpg)
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.
Header Nodes
![Page 20: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/20.jpg)
We have a list. This list consists of listNodes. In order to access these listNodes, we
need an iterator. Code: online
C++ implementation
![Page 21: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/21.jpg)
Doubly Linked ListsDoubly Linked Lists
a b c
head tail
class DoubleListNode{ Object element; ListNode* next; ListNode* prev;}
class DoubleListNode{ Object element; ListNode* next; ListNode* prev;}
Consider how hard it is to back up in a singly linked list.
Also consider text editor example
![Page 22: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/22.jpg)
Empty Doubly Linked ListEmpty Doubly Linked List
c
head tail
// constructorDoubleList(){ head = new DoubleListNode (); tail = new DoubleListNode (); head->next = tail; tail->prev = head;}
![Page 23: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/23.jpg)
newNode = new DoublyLinkedListNode()newNode->prev = current;newNode->next = current->next;newNode->prev->next = newNode;newNode->next->prev = newNode;current = newNode
Inserting into a Doubly Linked List
a c
head tailcurrent
![Page 24: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/24.jpg)
Inserting into a Doubly Linked List
a c
head tail
newNode = new DoublyLinkedListNode()newNode->prev = current;newNode->next = current->next;newNode->prev->next = newNode;newNode->next->prev = newNode;current = newNode
bcurrent
![Page 25: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/25.jpg)
Inserting into a Doubly Linked List
a c
head tail
newNode = new DoublyLinkedListNode()newNode->prev = current;newNode->next = current->next;newNode->prev->next = newNode;newNode->next->prev = newNode;current = newNode
bcurrent
![Page 26: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/26.jpg)
Inserting into a Doubly Linked List
a c
head tail
newNode = new DoublyLinkedListNode()newNode->prev = current;newNode->next = current->next;newNode->prev->next = newNode;newNode->next->prev = newNode;current = newNode
bcurrent
![Page 27: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/27.jpg)
Inserting into a Doubly Linked List
a c
head tail
newNode = new DoublyLinkedListNode()newNode->prev = current;newNode->next = current->next;newNode->prev->next = newNode;newNode->next->prev = newNode;current = newNode
bcurrent
![Page 28: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/28.jpg)
Inserting into a Doubly Linked List
a c
head tail
newNode = new DoublyLinkedListNode()newNode->prev = current;newNode->next = current->next;newNode->prev->next = newNode;newNode->next->prev = newNode;current = newNode
bcurrent
![Page 29: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/29.jpg)
newNode = new DoublyLinkedListNode()newNode->prev = current;newNode->next = current->next;newNode->prev->next = newNode;newNode->next->prev = newNode;current = newNode
Inserting into a Doubly Linked List
a c
head tailbcurrent
![Page 30: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/30.jpg)
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
head bcurrent
![Page 31: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/31.jpg)
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
head bcurrent
oldNode
![Page 32: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/32.jpg)
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
head bcurrent oldNode
![Page 33: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/33.jpg)
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
head bcurrent oldNode
![Page 34: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/34.jpg)
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
head bcurrent oldNode
![Page 35: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/35.jpg)
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
headcurrent
![Page 36: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/36.jpg)
a b c d
first
Circular Linked lists
![Page 37: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/37.jpg)
Sorted Linked ListSorted Linked List
A sorted link list is one in which items are in sorted order. It can be derived from a list class.
code
![Page 38: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/38.jpg)
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
![Page 39: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/39.jpg)
In class exercises …
Question 17.3 from the book.Write an algorithm for printing a singly linked list
in reverse. (Don’t use recursion).
![Page 40: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/40.jpg)
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)
![Page 41: Chapter 17 Linked List. Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked.](https://reader036.fdocuments.us/reader036/viewer/2022081511/56649dab5503460f94a99ac7/html5/thumbnails/41.jpg)
Programming Homework
Implement a linked listOn lineDue Feb 28th.