DATA STRUCTURES
Dr. P. Subathra
subathrakishore@yahoo.com
Professor
Dept. of Information Technology
KAMARAJ College of Engineering & Technology
(AUTONOMOUS)
Madurai
Tamil Nadu
India
CS8391 – DATA STRUCTURES
ONLINE CLASSES – CLASS NO. 9
08.09.2020
(02:00 PM – 03:00 PM)
UNIT 1
DOUBLY LINKED LIST
WHAT IS WRONG WITH
CIRCULAR SINGLY LINKED LIST…??
VISIT THE PREVIOUS NODES WITH EASE ..???!!!
BACKWARD TRAVERSAL…..???!!!
WHAT IS WRONG WITH
CIRCULAR SINGLY LINKED LIST…??
• Is that Annoying….???
• No Worries MAN…!!!
WHAT IS WRONG WITH CIRCULAR SINGLY
LINKED LIST …??
DOUBLY LINKED LIST….!!!
Motivation
• Doubly linked lists are useful for playing video
and sound files with “rewind” and “instant
replay”
• They are also useful for other linked data
which require “rewind” and “fast forward” of
the data
Node of a Doubly Linked List
• NODE
– Data Field
– Link / Pointer Fields
• HEAD
• TAIL
• NULL Pointers
DOUBLY LINKED LIST
MEMORY REPRESENTATION
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
1003
head
1030
tail
SN Operation Description
1 Insertion at beginning Adding the node into the linked list at beginning.
2 Insertion at end Adding the node into the linked list to the end.
3 Insertion after
specified node
Adding the node into the linked list after the specified
node.
4 Deletion at beginning Removing the node from beginning of the list
5 Deletion at the end Removing the node from end of the list.
6 Deletion of the node
having given data
Removing the node which is present just after the node
containing the given data.
7 Searching Comparing each node data with the item to be searched
and return the location of the item in the list if the item
found else return null.
8 Traversing Visiting each node of the list at least once in order to
perform some specific operation like searching, sorting,
display, etc.
Operations on a Doubly Linked List
NODE CREATION
EMPTY LIST
struct node * head = NULL;
struct node * head = NULL;
Operations on a Doubly Linked List
NULL
head tail
NULL
CREATING A LIST
• Creating the FIRST Node and Attaching it to
the List
struct node * temp = (node *) malloc (sizeof (struct node));
tempnext = NULL;
temprev=NULL;
Operations on a Doubly Linked List
Data fieldPrev Link
temp
1005
Next Link
NULL NULL
head
NULL
tail
NULL
CREATING A LIST
• Creating the FIRST Node and Attaching it to
the List
struct node * temp = (node *) malloc (sizeof (struct node));
tempnext = NULL;
temprev=NULL;
head = temp;
tail = temp;
Operations on a Doubly Linked List
Data fieldPrev Link
temp
1005
Next Link
NULL NULL
1005
head
1005
tail
CREATING A LIST
• Creating the FIRST Node and Attaching it to
the List
struct node * temp = (node *) malloc (sizeof (struct node));
tempnext = NULL;
temprev=NULL;
head = temp;
tail = temp;
tempdata=777;
Operations on a Doubly Linked List
Data fieldPrev Link
temp
1005
Next Link
NULL 777 NULL
1005
head
1005
tail
https://www.youtube.com/watch?v=DsjigShQY_
c&list=PLqer4ixaRRQLvGA1yvg2trDJ4B_fbmDzs&
index=31
OPERATIONS ON A DOUBLY LINKED LIST :
CREATING A LIST
CODE
• https://www.studytonight.com/data-
structures/doubly-linked-list
• https://www.javatpoint.com/doubly-linked-
list
struct node
{
int data; // Data
node *prev; // A reference to the
//previous node
node *next; // A reference to the next
//node
};
node *head; // points to first node
node *tail; // points to first last node
head = NULL;
tail = NULL;
OPERATIONS ON A DOUBLY LINKED LIST :
CREATING A LIST (code)
INSERTION OPERATIONS
INSERT FIRST
https://www.youtube.com/watch?v=I9FyNeaBs
0w
OPERATIONS ON A DOUBLY LINKED LIST :
INSERT FIRST
void insertFirst(int d)
{
// Creating new node
node *temp;
temp = new node();
temp->data = d;
temp->prev = NULL;
temp->next = front;
// List is empty
if(front == NULL)
end = temp;
else
front->prev = temp;
front = temp;
}
OPERATIONS ON A DOUBLY LINKED LIST :
INSERT FIRST
INSERT LAST
https://www.youtube.com/watch?v=u0gAg1aeX
nw&list=PLqer4ixaRRQLvGA1yvg2trDJ4B_fbmDz
s&index=29
OPERATIONS ON A DOUBLY LINKED LIST :
INSERT LAST
void insertLast(int d)
{
// create new node
node *temp;
temp = new node();
temp->data = d;
temp->prev = end;
temp->next = NULL;
// if list is empty
if(end == NULL)
front = temp;
else
end->next = temp;
end = temp;
}
OPERATIONS ON A DOUBLY LINKED LIST :
INSERT LAST
INSERT MIDDLE
• Insert a node New before Cur (not at front
or rear)
10 7020 55
40Head
New
Cur
New->next = Cur;
New->prev = Cur->prev;
Cur->prev = New;
(New->prev)->next = New;
Tail
OPERATIONS ON A DOUBLY LINKED LIST :
INSERT BEFORE
• Insert a node New after Cur (not at front or
rear)
10 7020 55
40Head
New
Cur
New->prev = Cur;
New->next = Cur->next;
Cur->next = New;
(New->next)->prev = New;
Tail
OPERATIONS ON A DOUBLY LINKED LIST :
INSERT AFTER
https://www.youtube.com/watch?v=h3u1sQgKT
G4&list=PLqer4ixaRRQLvGA1yvg2trDJ4B_fbmDz
s&index=30
OPERATIONS ON A DOUBLY LINKED LIST :
INSERT MIDDLE
DELETION OPERATIONS
DELETE FIRST
OPERATIONS ON A DOUBLY LINKED LIST :
DELETE FIRST
https://www.youtube.com/watch?v=fYOAk4QL3
Lo&list=PLqer4ixaRRQLvGA1yvg2trDJ4B_fbmDzs
&index=32
OPERATIONS ON A DOUBLY LINKED LIST :
DELETE FIRST
DELETE LAST
https://www.youtube.com/watch?v=tt3v2u6fIs
U&list=PLqer4ixaRRQLvGA1yvg2trDJ4B_fbmDzs
&index=33
OPERATIONS ON A DOUBLY LINKED LIST :
DELETE LAST
DELETE MIDDLE
OPERATIONS ON A DOUBLY LINKED LIST :
DELETE MIDDLE
Deleting a Node
• Delete a node Cur (not at front or rear)
(Cur->prev)->next = Cur->next;
(Cur->next)->prev = Cur->prev;
delete Cur;
10 7020 5540
Head
Cur
Tail
https://www.youtube.com/watch?v=VL3-
SVonQz0&list=PLqer4ixaRRQLvGA1yvg2trDJ4B_f
bmDzs&index=34
OPERATIONS ON A DOUBLY LINKED LIST :
DELETE MIDDLE
void Doubly_Linked_List :: delete_node(node *n)
{
// if node to be deleted is first node of list
if(n->prev == NULL)
{
front = n->next; //the next node will be front of list
front->prev = NULL;
}
// if node to be deleted is last node of list
else if(n->next == NULL)
{
end = n->prev; // the previous node will be last of list
end->next = NULL;
}
else
{
//previous node's next will point to current node's next
n->prev->next = n->next;
//next node's prev will point to current node's prev
n->next->prev = n->prev;
}
//delete node
delete(n);
}
OPERATIONS ON A DOUBLY LINKED LIST :
DELETE MIDDLE
OPERATIONS ON A DOUBLY LINKED LIST :
FORWARD TRAVERSAL
void forwardTraverse()
{
node *trav;
trav = front;
while(trav != NULL)
{
cout<<trav->data<<endl;
trav = trav->next;
}
}
OPERATIONS ON A DOUBLY LINKED LIST :
BACKWARD TRAVERSAL
void backwardTraverse()
{
node *trav;
trav = end;
while(trav != NULL)
{
cout<<trav->data<<endl;
trav = trav->prev;
}
}
DOUBLY LINKED LIST
https://www.youtube.com/watch?v=sGScT8YW
0ns
END
OF
DOUBLY LINKED LIST ….!!!

1. 6 doubly linked list