Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Data Structures

Double Linked Lists


Dr. G. Rama Mohan Babu
Professor, HoD., Dept. of CSE (AI & ML).
RVR & JC College of Engineering
Guntur – 522 019
Introduction
• A Doubly Linked List (DLL) contains an extra pointer, typically
called previous pointer, together with next pointer and data which are
there in singly linked list.
• Disadvantages over singly linked list
• Every node of DLL Require extra space for an previous pointer.
• All operations require an extra pointer previous to be maintained. For
example, in insertion, we need to modify previous pointers together
with next pointers.
Implementation
struct DNode{
int data;
struct DNode *next;
struct DNode *prev;
}
Insert a node at front

p = getDNode(d);
if(list==NULL)
list = p;
else{
list->prev = p;
p->next = list;
list = p;
}
Insert a node after a given node

p->prev = list;
p->next = list->next;
list->next->prev = p;
list->next = p;
Add a node at the end

p = getDNode(d);
if(list == NULL){
return p;
}
for(q=list; q->next!=NULL; q=q->next);
q->next= p;
p->prev = q;
Delete A Node
if(list->data==k){
Case 1: Delete First Node: p = list;
(1) List has only one node if(list->next==NULL){
(2) List has more than one node list = NULL;
} else{
list->next->prev=NULL;
list = list->next;
}
free(p);
return list;
}
Delete A Node q = list;
while(q->next != NULL){
if(q->next->data == k)
Case 2: Delete after first Node: break;
(1) Except last node q = q->next;
(2) Last Node }

p = q->next;
q->next = q->next->next;
if(q->next!=NULL)
q->next->prev = q;

free(p);

You might also like