chapter 17 linked list
DESCRIPTION
Chapter 17 Linked List. Saurav Karmakar Spring 2007. LISTS. Easiest implementation of LIST --- ARRAY :: has Disadvantages. 1) To insert an item at the beginning or middle of an array sliding lots of items over one place to make - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/html5/thumbnails/1.jpg)
Chapter 17Linked List
Saurav KarmakarSpring 2007
![Page 2: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/html5/thumbnails/2.jpg)
LISTS Easiest implementation of LIST --- ARRAY
:: has Disadvantages.
1) To insert an item at the beginning or middle of an array sliding lots of items over one place to
make room :: ~ O(n)
2) Arrays have a fixed length :: can't be changed.
Adding items to a list Allocation a whole new array if the array containing LIST is full and then move all the items from the old array to the new one.
![Page 3: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/html5/thumbnails/3.jpg)
Linked List Consists of connected, dynamically
allocated nodes.
A linked list is made up of "nodes". Each node has two components: an item, and a reference to the next node in the list.
![Page 4: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/html5/thumbnails/4.jpg)
Comparison with Array1. Arrays
contiguousdirect 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 5: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/html5/thumbnails/5.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 ;
a
Iterating through the data structureIterating through the data structure
![Page 6: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/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
Adding an element
![Page 7: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/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](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/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
first last
![Page 9: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/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](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/html5/thumbnails/10.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 11: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/html5/thumbnails/11.jpg)
class ListNode{ Object element; ListNode* next;}At any point, we can insert a new item x by doing this:tmp = new ListNode();Tmp->element = x;Tmp->next = current->next;Current->next = tmp;
last
Inserting an element
A0 A1 A2
first current
![Page 12: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/html5/thumbnails/12.jpg)
class ListNode{ Object element; ListNode* next;}At any point, we can insert a new item x by doing this:
tmp = new ListNode();Tmp->element = x;Tmp->next = current->next;Current->next = tmp;
Inserting an element
A0 A1 A2
first lastcurrent
tmp
![Page 13: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/html5/thumbnails/13.jpg)
class ListNode{ Object element; ListNode* next;}At any point, we can insert a new item x by doing this:
tmp = new ListNode();Tmp->element = x;Tmp->next = current->next;Current->next = tmp;
Inserting an element
A0 A1 A2
first lastcurrent x
tmp
![Page 14: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/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;
Inserting an element
A0 A1 A2
first lastcurrent x
tmp
![Page 15: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/html5/thumbnails/15.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;
Inserting an element
A0 A1 A2
first lastcurrent x
tmp
![Page 16: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/html5/thumbnails/16.jpg)
Simplified version
Current->next = new ListNode(x, current->next)
tmp = new ListNode();
Current->next = tmp;
Tmp->next = current->next;
Tmp->element = x;
![Page 17: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/html5/thumbnails/17.jpg)
class ListNode{ Object element; ListNode* next;}
Current->next = current->next->next;
Deleting an element
A0 A1 A2
current last
![Page 18: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/html5/thumbnails/18.jpg)
class ListNode{ Object element; ListNode* next;}
Current->next = current->next->next; Memory leak!
Deleting an element
A0 A1 A2
lastcurrent
![Page 19: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/html5/thumbnails/19.jpg)
Delete a Node
Node *deletedNode = current->next;
Current->next = current->next->next;
Delete deletedNode;
![Page 20: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/html5/thumbnails/20.jpg)
Length Function The Length function takes a linked list and
computes the number of elements in the list.
/*Given a linked list head pointer, compute and return the number of nodes in the list.*/
int Length(struct node* head) {struct node* current = head;int count = 0;while (current != NULL) {count++;current = current->next;}return count;}
![Page 21: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/html5/thumbnails/21.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 node
![Page 22: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/html5/thumbnails/22.jpg)
a b c
head tail
class DoubleListNode{ Object element; ListNode* next; ListNode* prev;}
class DoubleListNode{ Object element; ListNode* next; ListNode* prev;}
A doubly linked list allows bidirectional traversal by storing two pointers per node.
Doubly Linked Lists
![Page 23: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/html5/thumbnails/23.jpg)
head tail// constructorDoubleList(){ head = new DoubleListNode (); tail = new DoubleListNode (); head->next = tail; tail->prev = head;}
Empty Doubly Linked List
![Page 24: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/html5/thumbnails/24.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 25: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/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](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/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](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/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](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/html5/thumbnails/28.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 29: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/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](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/html5/thumbnails/30.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 31: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/html5/thumbnails/31.jpg)
Circular Linked Lists
a b c d
first
![Page 32: Chapter 17 Linked List](https://reader035.vdocuments.us/reader035/viewer/2022081506/5681385d550346895da00bf9/html5/thumbnails/32.jpg)
Sorted Linked List
A sorted link list is one in which items are in sorted order.
The major difference from linked list is the insertion operation.