Dummy Nodes Doubly Linked Lists and Circular Linked Lists Dummy Nodes Plain Linked List Linked List has annoying special cases InsertAt index value If index 0 InsertFirst value Else ID: 761467
Download Presentation The PPT/PDF document "Dummy Nodes, Doubly Linked Lists and Cir..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Dummy Nodes
Plain Linked ListLinked List has annoying special cases: InsertAt (index, value) If index = 0 InsertFirst (value)Else Node* newNode = new Node(value) Node* temp = head Repeat (index – 1) times temp = temp->next If temp == nullptr error newNode->next = temp->next temp->next = newNode
Plain Linked ListSolution : Dummy Node ListNode that head always points to Created at construction, does not count in length Empty List: List with 1 value:
Plain Linked ListEliminates special cases InsertAt (index, value) Node* newNode = new Node(value) Node* temp = head Repeat index times temp = temp->next If temp == nullptr error newNode->next = temp->next temp->next = newNode
Linked List Directional Challenges
BigO'sWhat is BigO for our improved linked list operations? With length & tail Get Length Insert Start Insert at middle InsertEnd Retrieve First / L ast Value Retrieve Middle Value Remove Beginning Remove End O(1) O(1) O(n) O(1) O(1) O(n) O(1) O(n)
BigO'sWhat is BigO for our improved linked list operations? With length & tail Get LengthInsert StartInsert at middleInsertEndRetrieve First / Last ValueRetrieve Middle ValueRemove BeginningRemove End O(1) O(1) O(n) O(1) O(1) O(n) O(1) O(n)
Reverse TraverseWhat if we want to reverse through the list? E.g. Print in reverse order
Reverse TraverseWhat if we want to print the list in reverse order? Iterative: for( i = length-1 … 0) cout << retrieveAt(i)BigO?
Reverse TraverseWhat if we want to print the list in reverse order? Iterative: for( i = length-1 … 0) //O(n) cout << retrieveAt(i) //O(n)BigO? O(n2)
LessonAccess middle: ArrayList : O(1) LinkedList : O(n) Avoid retrieveAt(i) in linked list!
Doubly Linked Lists
Doubly Linked NodeDoubly linked node Pointers to previous and next
Doubly Linked ListDoubly Linked List
With DummiesDummy/Sentinel nodes to avoid special cases
ConstructValid starting state Head/Tail dummy nodes Head prev : null next : tailTailprev : headnext : nullLength : 0
Inserting InsertAt (index, value) : Insert(0, 8 ) previous = head repeat index times previous = previous->nextmake newNode
Inserting InsertAt (index, value) : Insert(0, 8) previous = head repeat index times previous = previous->nextmake newNodeattach newNode to prev and prev->next
Inserting InsertAt (index, value) : Insert(0 , 8) previous = headrepeat index times previous = previous->nextmake newNodeattach newNode to prev and prev->nextattach newNode neighbors to newNode
Inserting InsertAt (index, value) : Insert(0, 8) previous = head repeat index times previous = previous->next make newNodeattach newNode to prev and prev->nextattach newNode neighbors to newNodeupdate length
Inserting InsertAt (2, 15 ) previous = head repeat index times previous = previous->nextmake newNodeattach newNode to prev and prev->nextattach newNode neighbors to newNodeupdate length
Inserting InsertAt (2, 15 ) previous = head repeat index times previous = previous->nextmake newNodeattach newNode to prev and prev->nextattach newNode neighbors to newNodeupdate length
Inserting InsertAt (2, 15 ) previous = head repeat index times previous = previous->nextmake newNodeattach newNode to prev and prev->nextattach newNode neighbors to newNodeupdate length
Inserting InsertAt (2, 15 ) previous = head repeat index times previous = previous->nextmake newNodeattach newNode to prev and prev->nextattach newNode neighbors to newNodeupdate length
Removal RemoveAt (index) : RemoveAt (1) if index < 0 or index >= length, errorcurrent = head->nextrepeat index times current = current >nextattach current's neighbors to each otherdelete currentupdate length
Removal RemoveAt (index) : RemoveAt (1) if index < 0 or index >= length, error current = head->nextrepeat index times current = current >nextattach current's neighbors to each otherdelete currentupdate length
Removal RemoveAt (index) : RemoveAt (1) if index < 0 or index >= length, error current = head->nextrepeat index times current = current >nextattach current's neighbors to each otherdelete currentupdate length
Removal RemoveAt (index) : RemoveAt (1) if index < 0 or index >= length, error current = head->nextrepeat index times current = current >nextattach current's neighbors to each otherdelete currentupdate length
Final BigOBig O's for Doubly Linked Get Length Anything at start or end Anything in middle Forward Traversal of list Reverse Traversal of list O(1)O(1)O(n)O(n)O(n)
Circular linked listLast node links back to firstUsed to model domains, not for efficiency Circular List
Doubly Linked TasksImplement: Constructor InsertAt RemoveAt