Lecture 5 - Linked List Variations
Lecture 5 - Linked List Variations
Data Structures
Fall 2022
Link List – Variations and Applications
Previous Lecture
• Introduction to linked lists
• A pointer-based implementation in C++
Today
• Pors and Cons of linked lists
• Variations of linked lists
– Doubly linked lists
– Circular Linked lists
2
Linked List – Advantages
• Access any item as long as link to first item is maintained
3
Linked List – Disadvantages (1)
• Overhead of links
– Extra space for pointers with each item-node, pure overhead
4
Linked List – Disadvantages (2)
• List-processing algorithms that require fast access to each
element cannot be done as efficiently with linked lists
• Consider adding an element at the end of the list
5
Linked List – Disadvantages (3)
• List-processing algorithms that require fast access to each
element cannot be done as efficiently with linked lists
• Consider adding an element at the end of the list
6
Linked List – Disadvantages (4)
• List-processing algorithms that require fast access to each
element cannot be done as efficiently with linked lists
• Consider adding an element at the end of the list
7
Linked List – Disadvantages (5)
• List-processing algorithms that require fast access to each
element cannot be done as efficiently with linked lists
• Consider adding an element at the end of the list
8
Some Applications
• Main: Implement many other abstract datatypes such as
stacks, queues, binary trees, and fib. heaps etc.
9
Reverse the list
10
void List :: Reverse(){
12
Doubly Linked List
• Every node contains the address of the previous node
except the first node
– Both forward and backward traversal of the list is possible
next
x a b c
13
Node Class
• Node class contains three data members
– data: double-type data in this example
– next: a pointer to the next node in the list
– Prev: a pointer to the pervious node in the list
class Node {
public:
double data; // data
Node* next; // pointer to next
Node* prev; // pointer to previous
};
14
List Class
• List class contains two pointers
– head: a pointer to the first node in the list
– tail: a pointer to the last node in the list
– Since the list is empty initially, head and tail are set to NULL
class List {
public:
List(void) { head = NULL; tail = NULL; } //
constructor
~List(void); //
destructor
. . .
private:
Node* head;
Node* tail;
};
15
Adding First Node
hea a tail
d
16
Inserting a Node in Doubly Linked List (1)
• To add a new item after the linked list node pointed by
current
x a c
hea tail
d current
17
Inserting a Node in Doubly Linked List (2)
• To add a new item after the linked list node pointed by
current
x a c
hea b
d tail
current
18
Inserting a Node in Doubly Linked List (3)
• To add a new item after the linked list node pointed by
current
x a c
hea b tail
d current
19
Inserting a Node in Doubly Linked List (3)
• To add a new item after the linked list node pointed by
current
x a c
hea b tail
d current
20
Inserting a Node in Doubly Linked List (3)
• To add a new item after the linked list node pointed by
current
x a c
hea b
d current tail
21
Inserting a Node in Doubly Linked List (3)
• To add a new item after the linked list node pointed by
current
x a c
hea b tail
d current
22
Inserting a Node in Doubly Linked List (3)
• To add a new item after the linked list node pointed by
current
x a c
hea b tail
d current
newNode = new DoublyLinkedListNode
newNode->prev = current;
newNode->next = current->next;
newNode->prev->next = newNode;
newNode->next->prev = newNode;
current = newNode;
23
Deleting a Node From Doubly Linked List
• Suppose current points to the node to be deleted from
the list
x a c
hea b
d current
oldNode = current;
oldNode->prev->next = oldNode->next;
oldNode->next->prev = oldNode->prev;
current = oldNode->prev;
delete oldNode;
24
Deleting a Node From Doubly Linked List
• Suppose current points to the node to be deleted from the
list
x a c
hea b
d current
oldNode
oldNode = current;
oldNode->prev->next = oldNode->next;
oldNode->next->prev = oldNode->prev;
current = oldNode->prev;
delete oldNode;
25
Deleting a Node From Doubly Linked List
• Suppose current points to the node to be deleted from the
list
x a c
hea b
d current
oldNode
oldNode = current;
oldNode->prev->next = oldNode->next;
oldNode->next->prev = oldNode->prev;
current = oldNode->prev;
delete oldNode;
26
Deleting a Node From Doubly Linked List
• Suppose current points to the node to be deleted from the
list
x a c
hea b
d current
oldNode
oldNode = current;
oldNode->prev->next = oldNode->next;
oldNode->next->prev = oldNode->prev;
current = oldNode->prev;
delete oldNode;
27
Deleting a Node From Doubly Linked List
• Suppose current points to the node to be deleted from the
list
x a c
hea b
d current
oldNode
oldNode = current;
oldNode->prev->next = oldNode->next;
oldNode->next->prev = oldNode->prev;
current = oldNode->prev;
delete oldNode;
28
Deleting a Node From Doubly Linked List
• Suppose current points to the node to be deleted from the
list
x a c
hea
d current
oldNode = current;
oldNode->prev->next = oldNode->next;
oldNode->next->prev = oldNode->prev;
current = oldNode->prev;
delete oldNode;
29
Any Question So Far?
33