Ch5.2 - List
Ch5.2 - List
Lists-PartII
Previous class
• Singly linked lists hold a reference only to the next
node.
▪ A doubly linked list is one in which all nodes are linked together by
multiple links
– Each node points to not only successor but to the predecessor as well.
▪ There are two NULL: at the first and last nodes in the list
▪ Every nodes in the doubly linked list has minimum of three fields:
1. LeftPointer(previous): Point to the prevous node
2. RightPointer(next): Points to the next node in the list
3. DATA: Actual data the node stores
A B C
▪ Advantage:
– Given a node, it is easy to visit its predecessor.
– Convenient to traverse lists backwards
Head
11/29/2021 3
DLL Operations
11/29/2021 4
Empty List
11/29/2021 5
11/29/2021 6
Insertion
11/29/2021 7
Inserting At Beginning of the list
11/29/2021 8
11/29/2021 9
Inserting At End of the list
11/29/2021 10
11/29/2021 11
Inserting At Specific location in the
list (After a Node)
11/29/2021 12
Cont…
11/29/2021 15
Deleting from Beginning of the list
▪ We can use the following steps to delete a node from end of the
doubly linked list
– Step 1: Check whether list is Empty (head == NULL)
– Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not
possible' and terminate the function.
– Step 3: If it is Not Empty then, define a Node pointer 'temp' and initialize
with tail.
– Step 4: Check whether list is having only one node (temp →
previous == NULL)
– Step 5: If it is TRUE then set head = NULL , tail = NULL and
delete temp (Setting Empty list conditions)
– Step 6: If it is FALSE then set tail = temp → previous, (temp-> previous)-
>next=NULL and delete temp. 11/29/2021 18
11/29/2021 19
Deleting a Specific Node from the list
▪ We can use the following steps to delete a specific node from the
doubly linked list...
– Step 1: Check whether list is Empty (head == NULL)
– Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not
possible' and terminate the function.
– Step 3: If it is Not Empty then, define Node pointers 'temp' initialize
with head.
– Step 4: Keep moving the temp until it reaches to the exact node to be
deleted or to the last node. And every time check whether temp is reached
to last node or not. If it is reached to last node then display 'Given node not
found in the list! Deletion not possible!!!'. And terminate the function.
– Step 6: If it is reached to the exact node which we want to delete, then check
whether list is having only one node or not
11/29/2021 20
Cont…
– Step 7: If list has only one node and that is the node to be deleted, then
set head = NULL, tail = NULL and delete temp.
– Step 8: If list contains multiple nodes, then check whether temp is the first
node in the list (temp == head).
– Step 9: If temp is the first node then move the head to the next node (head
= head → next) , (temp->next)->previous=NULL and delete temp.
– Step 10: If temp is not first node then check whether it is last node in the list
(temp == tail).
– Step 11: If temp is last node (tail= temp→ previous) , (temp->previous)-
>next =NULL and delete temp.
– Step 12: If temp is not first node and not last node then set (temp-
>previous)->next=temp->next, (temp->next)->previous=temp->previous
and delete temp.
11/29/2021 21
11/29/2021 22
Display
11/29/2021 23
Displaying forward
11/29/2021 24
11/29/2021 25
Displaying Backward
11/29/2021 26
11/29/2021 27
Singly vs Doubly Linked Lists
• Singly linked lists only hold a reference to the next node. In the
implementation you always maintain a reference to the head to the linked list
and optionally, a reference to the tail node for quick additions/removals.
• With a doubly linked list each node holds a reference to the next and previous
node. In the implementation you always maintain a reference to the head and
the tail of the doubly linked list to do quick additions/removals from both ends
of your list.
11/29/2021 28
DLLs compared to SLLs
▪ Advantages: ▪ Disadvantages:
– Can be traversed in either direction – Requires more space
(may be essential for some programs) – List manipulations are slower
– Some operations, such as deletion and (because more links must be
inserting before a node, become easier changed)
– Greater chance of having bugs
(because more links must be
manipulated)
11/29/2021 29
Complexity
Singly Linked Doubly Linked
Search O(n) O(n)
Remove in
O(n) O(n)
middle
31
Circular List
11/29/2021 32
Introduction
10/23/2017 33
Singly Linked List as Circular List
10/23/2017 34
Doubly Linked List as Circular
▪ In doubly linked list, the next pointer of the last node points to the
first node and the previous pointer of the first node points to the
last node making the circular in both directions.
Note:
The last node's next points to the first node of the list in both cases of singly
as well as doubly linked list.
The first node's previous points to the last node of the list in case of doubly
linked list.
10/23/2017 35
Application of Circular Linked List
▪ Multitasking OS
– All the running applications are kept in a circular linked list and the OS gives a
fixed time slot to all for running. The Operating System keeps on iterating
over the linked list until all the applications are completed.
▪ Multiplayer games.
– All the Players are kept in a Circular Linked List and the pointer keeps on
moving forward as a player's chance ends.
▪ Circular Queue.
– In a Queue we have to keep two pointers, FRONT and REAR in memory all
the time, where as in Circular Linked List, only one pointer is required.
11/29/2021 36
Implementation
▪ To implement a circular singly linked list, we use a pointer that points to
the last node of the list.
– If we have a pointer last pointing to the last node, then last -> next will point to the
first node.
▪ Why we use a pointer that points to the last node instead of first node ?
– Because insertion in the beginning or at the end of the list takes constant time
irrespective of the length of the list i.e O(1).
– However, if we use head pointer instead, we need to traverse the whole list i.e O(n).
10/23/2017 37
Operations
11/29/2021 38
Insertion
11/29/2021 39
Inserting At Beginning of the list
▪ We can use the following steps to insert a new node at beginning of the circular
linked list...
▪ Step 1 - Create a newNode with given value.
▪ Step 2 - Check whether list is Empty (last == NULL)
▪ Step 3 - If it is Empty then, set last = newNode and newNode→next = last .
▪ Step 4 - If it is Not Empty then,
– Set 'newNode → next =last->next',
– last->next = newNode'
11/29/2021 40
Sample code to add node to front the
list
11/29/2021 41
Inserting At End of the list
We can use the following steps to insert a new node at end of the circular
linked list...
▪ Step 1 - Create a newNode with given value.
▪ Step 2 - Check whether list is Empty (head == NULL).
▪ Step 3 - If it is Empty then, set last = newNode and newNode →
next = last.
▪ Step 4 - If it is Not Empty then, Set
– newNode → next = last->next.
– Last->next = newNode and
– Last= newNode
11/29/2021 42
Sample code to add node to the rear of
CSL
11/29/2021 43
Inserting At Specific location in the
list (After a Node)
We can use the following steps to insert a new node after a node in the circular linked list...
▪ Step 1 - Create a newNode with given value.
▪ Step 2 - define a node pointer temp and make it point to the 1st node (temp= last->next.)
▪ Step 3 - Keep moving the temp to its next node until it reaches to the node after which we
want to insert the newNode (until temp → data is equal to location, here location is the
node value after which we want to insert the newNode).
– In each iteration, check whether temp is reached to the last node or location is found.
▪ Step 5- If temp is reached to the exact node after which we want to insert the newNode
– newNode → next = temp->next
– temp → next = newNode
▪ Step 6 -Then check whether location is at the last node. If so move last pointer to the new
last node that is newNode
– last == newNode.
11/29/2021 44
Sample code: addAfter(Location)
11/29/2021 45
Displaying a circular Linked List
We can use the following steps to display the elements of a circular linked
list...
▪ Step 1 - Check whether list is Empty (last == NULL)
▪ Step 2 - If it is Empty, then display 'List is Empty!!!' and terminate the
function.
▪ Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize
with last.
▪ Step 4 - Keep displaying temp → data with an arrow (--->)
until temp reaches to the last node
▪ Step 5 - Finally display temp → data with arrow pointing to head → data.
11/29/2021 46
Sample Code: Display CLL
11/29/2021 47
Deletion
11/29/2021 48
Delete first
11/29/2021 49
Delete last
11/29/2021 50
Delete specific node
11/29/2021 51
Linked Lists Benefits & Drawbacks
▪ Benefits
– Easy to insert and delete in O(1) time (front and end)
– Don’t need to estimate total memory needed
▪ Drawbacks
– Hard to search in less than O(n) time(e.g. binary search doesn’t
work)
– Hard to jump to the middle
▪ Skip Lists and Self-organizing lists
– Tries to address these drawbacks
11/29/2021 52
Next Time!!
Ch6 – stack and queue