DATA STRUCTURES AND ALGORITHMS
Week 5: Doubly Linked
List
Momina Moetesum
Dept of CS
Bahria University, Islamabad
[email protected] 1
Introduction
• The singly linked list contains only one pointer field i.e.
every node holds an address of the next node.
• The singly linked list is uni-directional i.e. we can only
move from one node to its successor.
• Doubly linked list: Each node has two pointers, next and
previous
• Each node has one pointer to its successor (NULL if
there is none) and one pointer to its predecessor (NULL
if there is none). 2
Introduction
prev
data
predecessor successor
next
head 9 17 22 26 34
3
Introduction
• Linked list
class Node {
int data;
Node* next;
};
• Doubly linked list
class Node {
Node *prev;
int data;
Node *next;
4
};
predptr
Insertion class Node
{
public:
int data;
Node *next;
15 20 Node *prev;
};
void insert(Node * predPtr, int val)
{
17
Node* newptr = new Node;
newprt->data = val;
newptr->prev = predptr;
newptr newptr->next = predptr->next;
predptr->next->prev = newptr;
predptr->next = newptr;
5
}
ptr
deletion
15 17 20
void delete(Node *ptr)
free
{
ptr->next->prev = ptr->prev;
ptr->prev->next = ptr->next;
delete ptr;
6
}
Class implementation
class DoublyList
class node
{
{
public: public:
node *head; int data;
node *next;
DoublyList(){head = 0;}
node *prev;
void add_end(int value); };
void add_begin(int value);
void add_after(int value, int newVal);
void delete_element(int value);
void display_dlist();
void traverse_forward();
void traverse_backward(); 7
};
void DoublyList::add_begin(int value){
node *temp;
temp = new node;
temp->prev = NULL;
temp->next = NULL;
temp->data = value;
// If list has no elements
if (head == NULL)
{
head = temp;
}
// List has element(s)
temp->next = head;
head->prev = temp;
head = temp;
}
8
void DoublyList::add_end(int value){
node *s, *temp;
temp = new node;
temp->data = value;
temp->next = NULL;
temp->prev = NULL;
// If list has no elements
if (head == NULL)
{
head = temp;
}
// List already has element(s)
else
{
s = head;
while (s->next != NULL)
s = s->next;
s->next = temp;
temp->prev = s; 9
}
}
void DoublyList::add_after(int value, int position){
// List is Empty
if (head == NULL)
{ cout<<“List is empty”<<endl;
return; }
node *q=head;
int i;
//Take the pointer to desired index
for (i = 1;i < position;i++)
{
q = q->next;
if (q == NULL)
{
cout<<"There are less than ";
cout<<position<<" elements."<<endl;
return;
}
}
//Create a new node
node *tmp = new node;
10
tmp->data = value;
//If inserting at the end
if (q->next == NULL)
{
q->next = tmp;
tmp->next = NULL;
tmp->prev = q;
}
//Insertion at an arbitrary node
else
{
tmp->next = q->next;
tmp->prev = q;
tmp->next->prev = tmp;
q->next = tmp;
}
}
11
void DoublyList::delete_element(int value){
Node *p=head;
while (p!=NULL && p->data != value){
p = p->next;
}
if (p == NULL){
cout << "ERROR: Value sought not found.";
return;
}
if (p->prev == NULL)
{ //First Node to be deleted
head = head->next;
head->prev = NULL;
delete p;
return;
}
12
//Last node to be deleted
if(p->next==NULL){
p->prev->next=NULL;
delete p;
return;
}
//Node in between other nodes to be deleted
p->prev->next = p->next;
p->next->prev = p->prev;
delete p;
}
13
void reverse(Node *head)
{
Node *p1, *p2;
p1 = head;
p2 = p1->next;
p1->next = NULL;
p1->prev = p2;
while (p2 != NULL)
{
p2->prev = p2->next;
p2->next = p1;
p1 = p2;
p2 = p2->prev;
}
head = p1;
}
14