Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

DATA STRUCTURES

AND ALGORITHMS

DOUBLY LINKED LIST

Momina Moetesum
Dept of CS
Bahria University, Islamabad

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

predecessor data successor

next

first 9 17 22 26 34

last

3
Node of a doubly linked
list
*Previous Data *Next

A SINGLY LINKED LIST


Head

A DOUBLY LINKED LIST


Head 4
INTRODUCTION

 Linked list
class Node {
int data;
Node* next;
};

 Doubly linked list


class Node {
Node *previous;
int data;
Node *next;
};
5
class Node
DOUBLY LINKED LIST : INSERTION {
predptr public:
DataType data;
Node * next;
Node * prev;
15 20
};

void insert(Node * predPtr, Data data)


{
17
Node* newptr = new Node;
newprt->data = data;
newptr->prev = predptr;
newptr newptr->next = predptr->next;
predptr->next->prev = newptr;
predptr->next = newptr;
6
}
DOUBLY LINKED LIST : DELETION
ptr

15 17 20

void delete(Node * ptr)


free
{
ptr->next->prev = ptr->prev;
ptr->prev->next = ptr->next;
delete ptr;
7
}
C++ IMPLEMENTATION class node
{
class DoublyList public:
int data;
{ node *next;
public: node *prev;
};
node * start;
DoublyList(){start = 0;}
void add_end(int value);
void add_begin(int value);
void add_after(int value, int position);
void delete_element(int value);
void display_dlist();
void reverse();
}; 8
void DoublyList::add_end(int value){
node *s, *temp;
temp = new node;
temp->data = value;
temp->next = NULL;
// If list has no elements
if (start == NULL)
{
temp->prev = NULL;
start = temp;
}
// List already has element(s)
else
{
s = start;
while (s->next != NULL)
s = s->next;
s->next = temp;
temp->prev = s; 9
}
}
void DoublyList::add_begin(int value){

node *temp;
temp = new node;
temp->prev = NULL;
temp->data = value;
// If list has no elements
if (start == NULL)
{
start = temp;
temp->next=NULL;
return;
}
// List has element(s)
temp->next = start;
start->prev = temp;
start = temp;
}
10
void DoublyList::add_after(int value, int position){
// List is Empty
if (start == NULL)
{
cout<<"First Create the list."<<endl;
return;
}
node *tmp, *q;
int i;
q = start;
//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; 11
}
}
//Create a new node
tmp = new node;
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;

} 12

}
void DoublyList::delete_element(int value){
Node *p, *q;
q =0;
p = start;
while (p!=0 && p->data != value){
q = p;
p = p->next;
}
if (p == 0){
cout << "ERROR: Value sought not found.";
return;
}
if (q == 0){ //First Node to be deleted

start = start->next;
start->prev = NULL;
delete p;
return; 13
}
//Last node to be deleted
if(p->next==0){
q->next=0;
delete p;
return;
}

//Node in between other nodes to be deleted

q->next = p->next;
p->next->prev = q;
delete p;
}

14
void DoublyList::reverse(){
Node *p1, *p2;
p1 = start;
p2 = p1->next;
p1->next = NULL;
p1->prev = p2;

while (p2 != NULL)


{
p2->prev = p2->next;
p2->next = p1;
p1 = p2;
p2 = p2->prev;
}
start = p1;
cout<<"List Reversed"<<endl;
} 15
RECURSIVE PRINTING FOR SINGLY LINK
LIST

void print(Node *curr)


{
if(curr==0)
return;
else
{
cout<<curr->data;
print(curr->next);
}
}
16
RECURSIVE REVERSING FOR SINGLY LIST
Node* reve(Node *curr)
{
Node *rev;
if(curr==0 || curr->next==0)
return curr;
else
{
rev= reve(curr->next);
curr->next->next=curr;
curr->next=0;
return rev;
}
} 17

You might also like