0% found this document useful (0 votes)
17 views

Week 4 Doubly Linked Lists

Uploaded by

Muneeb Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Week 4 Doubly Linked Lists

Uploaded by

Muneeb Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Doubly Linked Lists

CSC-211 Data Structures and Algorithms


Lecture outline
• Circular Singly Linked Lists
• Introduction to the Doubly Linked Lists
• Memory representation of a Doubly Linked List
• Inserting a new node in the Doubly Linked List
• Inserting a node at the end
• Inserting a node at the start
• Inserting a node at an index
• Deleting a node from the Doubly Linked List
• Deleting the last node
• Deleting the first node
• Deleting a node at an index
• Searching for a record in the Doubly Linked List
• Circular Doubly Linked Lists 2
Circular Linked List
• Linked list in which the first element points to the last element
and the last element points to the first element. Both Singly
Linked List and Doubly Linked List can be made into a circular
linked list.
Singly Linked List as Circular

In singly linked list, the next pointer of the last node points to
the first node.

3
Circular Linked Lists

• In a circular linked list, the last


node contains a pointer to the
first node of the list.
• The last node can be detected
by the fact that it points to the
first node.
• An application can be the list of
recommendations in Google
search bar 4
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.

5
Introduction to Doubly Linked Lists

• A linked list that contains a


struct node
pointer to the next as well as to
{
the previous node in the list.
struct node * prev;
• Requires more storage space int data;
• Makes searching twice as fast struct node * next;
};
• Removal of the tail is fast too
6
Memory Representation of circular linked list:
• In the following image, memory representation
of a circular linked list containing marks of a
student in 4 subjects.
• However, the image shows a glimpse of how
the circular list is being stored in the memory.
The start or head of the list is pointing to the
element with the index 1 and containing 13
marks in the data part and 4 in the next part.
• Which means that it is linked with the node that
is being stored at 4th index of the list.
• However, due to the fact that we are
considering circular linked list in the memory
therefore the last node of the list contains the
address of the first node of the list.
7
Basic Operations
• Following are the important operations supported by a
circular list.

• insert − Inserts an element at the start of the list.


• delete − Deletes an element from the start of the list.
• display − Displays the list.

8
Insertion Operation

Algorithm
1. START
2. Check if the list is empty
3. If the list is empty, add the node and point the head to this node
4. If the list is not empty, link the existing head as the next node to the new node.
5. Make the new node as the new head.
6. END

9
http://www.btechsmartclass.com/data_structures/circular-linked-list.html
Circular Linked List: [ (6,56) (5,40) (4,1) (3,30) (2,20)
]
Insertion Example //display the list
void printList(){
struct node *ptr = head;
//insert link at the printf("\n[
first location ");
void insertFirst(int //start
key, intfrom
data)the beginning
#include <stdio.h> { if(head != NULL) {
#include <string.h> //create a link while(ptr->next != ptr) {
struct node *link = (struct node*)
printf("(%d,%d) ",ptr->key,ptr->data);
#include <stdlib.h>
malloc(sizeof(struct ptr
node));
= ptr->next; } }
#include <stdbool.h> link->key = key;
printf(" ]"); }
struct node { int data; int key;
link->data = data; void main(){
struct node *next; if (isEmpty()) insertFirst(1,10);
}; { insertFirst(2,20);
struct node *head = NULL;
head = link; insertFirst(3,30);
head->next
struct node *current = NULL; = head; insertFirst(4,1);
bool isEmpty(){ } else { insertFirst(5,40);
return head == NULL; //point it to old first node
insertFirst(6,56);
link->next = head; printf("Circular Linked List: ");
}
//point first to new //print
first nodelist
head = link; } } printList(); } 10
Advance Operation in Inserting nodes in the
list
Nodes can be inserted in a list in a variety of ways:
• Inserting node at the start of the list
• Inserting node at a certain index in the list
• Inserting node at the end of a list
• Inserting node before/after a certain value in the list

11
Inserting nodes at the start of a list

Pseudocode
Vertex vtx = new Vertex(v)
vtx.next = head
if (head != null)
head.prev = vtx
head = vtx
12
Inserting nodes at an index/value in a list

Pseudocode
Vertex pre = head
for (k = 0; k < i-1; k++)
pre = pre.next
Vertex aft = pre.next
Vertex vtx = new Vertex(v)
vtx.next = aft, aft.prev = vtx
pre.next = vtx, vtx.prev = pre 13
Inserting nodes at the end of a list

Pseudocode
Vertex vtx = new Vertex(v)
tail.next = vtx
vtx.prev = tail
tail = vtx

14
Deletion Operation
• The Deletion operation in a Circular linked list removes a certain node
from the list. The deletion operation in this type of lists can be done at
the beginning, or a given position, or at the ending.

Algorithm

1. START
2. If the list is empty, then the program is returned.
3. If the list is not empty, we traverse the list using a current pointer that is set to the head pointer and create another
pointer previous that points to the last node.
4. Suppose the list has only one node, the node is deleted by setting the head pointer to NULL.
5. If the list has more than one node and the first node is to be deleted, the head is set to the next node and the
previous is linked to the new head.
6. If the node to be deleted is the last node, link the preceding node of the last node to head node.
7. If the node is neither first nor last, remove the node by linking its preceding node to its succeeding node.
8. END 15
//display the list
} void printList(){
Example struct node *ptr = head;
//insert link at the first location
//start from the beginning
void insertFirst(int key, int data){ if(head != NULL) {
//delete first item while(ptr->next != ptr) {
//create a link
struct node * deleteFirst(){ printf("(%d,%d) ",ptr->key,ptr->data);
#include <stdio.h> struct node *link = (struct node*) malloc(sizeof(struct node));ptr = ptr->next;
}
#include <string.h> link->key = key; //save reference to first}link
#include <stdlib.h> link->data = data; struct node *tempLink} = head;
#include <stdbool.h> if (isEmpty()) {
if(head->next == head)void
{ main(){
struct node { head = link;
head = NULL; insertFirst(1,10);
int data; head->next = head; insertFirst(2,20);
return tempLink;
int key; } else { insertFirst(3,30);
}
struct node *next; insertFirst(4,1);
}; //point it to old first node insertFirst(5,40);
//mark next to first link as first
link->next = head; insertFirst(6,56);
struct node *head = NULL; head = head->next; printf("Circular Linked List: ");
struct node *current = NULL;
bool isEmpty(){ //point first to new first node
//return the deleted link//print list
return head == NULL; head = link;
return tempLink; printList();
} } deleteFirst();
}
} printf("\nList after deleting the first item: ");
Output
printList(); }
Circular Linked List: (6,56) (5,40) (4,1) (3,30) (2,20) 16
List after deleting the first item: (5,40) (4,1) (3,30) (2,20)
Deleting nodes from the list

Like insertion, nodes can be deleted from a list in a variety of ways:


• Deleting the first node of a list
• Deleting the last node from a list
• Deleting node at a certain index in the list
• Deleting a node with a certain value in the list

17
Deleting the first node of a list

Pseudocode
if empty, do nothing RETURN
temp = head
head = head.next
delete temp
if (head != null) head.prev = null
18
Deleting the last node of a list

Pseudocode
if empty, do nothing
temp = tail
tail = tail.prev
tail.next = null
delete temp

19
Deleting node with index/value in the list

Pseudocode
if empty, do nothing
Vertex pre = head
for (k = 0; k < i-1; k++)
pre = pre.next
Vertex del = pre.next, aft = del.next
pre.next = aft, aft.prev = pre
delete del
20
Searching a linked list for a record

Pseudocode
if empty, return NOT_FOUND
index = 0, temp = head
while (temp.item != v)
index++, temp = temp.next
if temp == null
return NOT_FOUND
return index
21
Doubly Linked List
• A node in a doubly linked list is an object which contains a data, a
reference to the previous node of the list, and a reference to the next
node of the list.
• A doubly linked list consists of two node pointers next and prev,
which point to the next and previous nodes, respectively.
• Unlike the singly linked list, we can traverse in both directions in a
doubly-linked list with the help of these next and prev pointers.

22
Circular Doubly Linked Lists

23
Circular Doubly Linked List
• Circular doubly linked list is a more complexed type of data structure
in which a node contain pointers to its previous node as well as the
next node.
• Circular doubly linked list doesn't contain NULL in any of the node.
• The last node of the list contains the address of the first node of the
list. The first node of the list also contain address of the last node in its
previous pointer.

24
Memory Management of Circular Doubly linked list

• The variable head contains the address of the first


element of the list i.e. 1 hence the starting node of
the list contains data A is stored at address 1.
• Since, each node of the list is supposed to have
three parts therefore, the starting node of the list
contains address of the last node i.e. 8 and the next
node i.e. 4.
• The last node of the list that is stored at address 8
and containing data as 6, contains address of the
first node of the list as shown in the image i.e. 1.
• In circular doubly linked list, the last node is
identified by the address of the first node which is
stored in the next part of the last node therefore the
node which contains the address of the first node, is
actually the last node of the list.

25
https://www.javatpoint.com/circular-doubly-linked-list
Structure of Node in a Doubly Circular
Linked List
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
struct Node *prev;
};

26
Transversal Example void display(struct Node* start) /* Driver program to test above functions*/
// Function//toFunction
insert Node
to
{ insert
at thenode with value as int
beginning main()
value1.
// If list is not {
// empty
of the List,
// The new node struct Node *temp = start; /*with
is inserted after the node Start with the empty list */
#include<stdio.h> void insertBegin(struct
// with value2 Node** start, int value) struct Node* start = NULL;
#include<stdlib.h> /* Find last { node */ void insertAfter(struct Node** start,
printf("\nTraversal int value1,
in forward direction \n");
struct Node struct Node *last = (*start)->prev;
// Pointer points to lastwhile Node(temp->next // Insert 5. So linked list becomes 5->NULL
int value2) != start) insertEnd(&start, 5);
{ struct Node
{ *last = (*start)->prev;
{
int data; // Create Node dynamically struct Node* new_node
printf("%d ", = (struct Node*)malloc(sizeof(struct
temp->data); // Insert 4 at the beginning. So linked
struct Node *next;struct Node*struct new_node
Node*
Node));=new_node
(struct Node*)malloc(sizeof(struct
= (struct
temp Node));
// list becomes 4->5
Node*)malloc(sizeof(struct
= temp->next; insertBegin(&start, 4);
struct Node *prev;new_node->data Node)); = value; new_node->data } = value1; // Inserting the data
}; new_node->data = value; // Inserting
printf("%d the data // Insert 7 at the end. So linked list
", temp->data);
// Start is going to be next of new_node
// Find node having value2 and next node // becomes
of it 4->5->7
insertEnd(&start, 7);
// Function to insert new_node->next
at the end // setting = *start;
upstruct
previous
Nodeand*tempnext=of *start;
new node
printf("\nTraversal in reverse direction \n");
void insertEnd(struct Node** start, new_node->next
int value)while (temp->data
= *start; != value2)
struct Node *last = start->prev;// Insert 8 at the end. So linked list
{ // Make newnew_node->prev
node previous tempof==start
last;
temp->next;
temp = last; // becomes 4->5->7->8
if (*start == NULL) (*start)->prev = new_node; struct Node while
*next(temp->prev
= temp->next; != last)
insertEnd(&start, 8);

{ // Update next and previous { pointers of start // Insert 6, after 5. So linked list
// Make last
struct Node* new_node = (struct previous of new node
// and last.// insert new_node between
printf("%d temp and //
", temp->data); next.
becomes 4->5->6->7->8
new_node->prev
Node*)malloc(sizeof(struct Node)); = last;
last->next =temp->next
(*start)->prev
= new_node;
= new_node;
temp = temp->prev; insertAfter(&start, 6, 5);
new_node->data = value; new_node->prev } = temp;
new_node->next//=Make new//node
new_node->prev Updatenext of old
=start
Output
last printf("%d
new_node->next
pointer
new_node; = next;
printf("Created circular doubly linked list is: ");
", temp->data);
Created circular doubly linked list is:display(start);
last->next
*start = new_node; = new_node;
*start = new_node;
next->prev } = new_node;
Traversal in forward direction 5 8 4 3return7 6 90;
return; } } }
Traversal in reverse direction 9 6 7} 3 4 8 5 27
}
Applications Of Circular Linked List
• Real-time application of circular linked list can be a multi-programming operating
system wherein it schedules multiple programs. Each program is given a dedicated
timestamp to execute after which the resources are passed to another program. This
goes on continuously in a cycle. This representation can be efficiently achieved
using a circular linked list.
• Games that are played with multiple players can also be represented using a circular
linked list in which each player is a node that is given a chance to play.
• We can use a circular linked list to represent a circular queue. By doing this, we can
remove the two pointers front and rear that is used for the queue. Instead, we can use
only one pointer.

28
https://www.prepbytes.com/blog/linked-list/circular-linked-list-introduction-and-applications/

You might also like