Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
25 views

Module 2.1 Doubly Linked List

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

Module 2.1 Doubly Linked List

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

DOUBLY LINKED LISTS

• Single linked list = one-way list


• List can be traversed in one direction only

• Double linked list = Two-way list


• List can be traversed in two directions
DOUBLY LINKED LISTS
• A doubly linked list or a two-way linked list is a more complex type of
linked list which contains a pointer to the next as well as the previous
node in the sequence.

• Therefore, it consists of three parts—data, a pointer to the next node, and


a pointer to the previous node leftlink data rlink

• Data field contains the data value.

• leftlink field contains the pointer to the previous node in the list

• rlink field contains the pointer to the next node in the list
• The structure of a doubly linked list
• The leftlink field of the start node and the
can be given as:
rlink field of the last node will contain
struct node
{ NULL.

struct node *leftlink;


int data;
struct node *rlink;
};
• The leftlink field is used to store the address of the preceding node,
which enables to traverse the list in the backward direction.
How a doubly linked list is maintained in the memory
• A variable START is used to store the address of the
start node.
• In this example, START = 1, so the start data is stored
at address 1, which is H. Since this is the start node, it
has no previous node and hence stores NULL or –1 in
the PREV field.
• It will traverse the list until we reach a position where
the NEXT entry contains –1 or NULL. This denotes
the end of the linked list.
• When we traverse the DATA and NEXT in this
manner, we will finally see that the linked list in the
above example stores characters that when put
together form the word HELLO.
Operations on a doubly linked list 3. Add nodes in different locations of the
list:
• At the beginning
1. Define a Structure: • At the end
struct node • At a position
• After a particular data
{
• Before a particular data
struct node *leftlink;
int data; 4. Delete nodes from different locations of
struct node *rlink; the list:
• From the beginning
} *NODE;
• From the end
• From a position
2. Create a node by allocating memory: • After a particular data
temp = (NODE *) malloc(sizeof(struct node)); • Before a particular data
• Delete a key node
5. Display the list
6. Search a key in the list
Inserting a New Node in a Doubly Linked List
In this section, we will see how a new node is added into a doubly linked list.
Case 1: The new node is inserted at the beginning.
Case 2: The new node is inserted at the end.
Case 3: The new node is inserted after a given node.
Case 4: The new node is inserted before a given node
Case 5: The new node is inserted at a particular position

Overflow is a condition that occurs when AVAIL = NULL or no free memory


cell is present in the system. When this condition occurs, the program must
give an appropriate message.
#include <stdio.h>
while(1) case 3: printf(" Enter data to insert\n");
#include <stdlib.h>
{ scanf("%d",&num);
struct node printf("Enter your choice\n"); scanf(“%d”,&k);
{ printf("1. Insert at front\n“); start= i_afterkey(start,num,k);
printf("2. Insert at last"); break;
struct node *leftlink; printf(“3. Insert after data"); case 4: printf(" Enter data to insert\n");
int data; printf(“4. Insert before data\n"); scanf("%d",&num);
struct node *rlink; printf(“5. Insert at a position\n"); scanf(“%d”,&k);
}; printf(“6. Display\n"); start= i_beforekey(start,num,k);
typedef struct node NODE; printf(“7. Stop\n"); break;
scanf("%d", &choice);
case 5: printf(" Enter data to insert\n");
NODE* insertfront(NODE *, int); switch(choice)
scanf("%d",&num);
NODE* insertend(NODE *, int); {
scanf(“%d”,&p;
NODE* i_afterkey(NODE *, int,int); case 1: printf(" Enter data to insert\n");
scanf("%d",&num); start= i_pos(start,num,p);
NODE* i_beforekey(NODE *, int,int);
start= insertfront(start,num); break;
NODE* i_pos(NODE *, int,int);
break; case 6: display(start);
void display(NODE *); case 2: printf(" Enter data to insert\n"); break;
scanf("%d",&num); case 7: exit(0);
void main() start= insertend(start, num); }
{ break; }
NODE *start=NULL; }
int num,choice;
void display(NODE *start)
{
Display Doubly linked List NODE *ptr;
ptr = start;
if (start == NULL)
{
printf("List is empty\n");
}
else
{
printf("The list data are \n");
while(ptr != NULL)
{
printf("%d\n", ptr-
>data);
ptr = ptr->rlink;
}
}
}
Searching for a Value in a Linked List
NODE* searchkey(NODE *start, int key) else
{ { // Traverse till key is found or till last node
NODE *ptr, *current; ptr = start;
while((ptr != NULL)&&(ptr->data !=key))
if (start== NULL) {
{ ptr = ptr->rlink;
printf(“List Empty\n”); }
return start; if(ptr == NULL) // Searched till end of list
} {
printf(“Key not found\n”);
}
else
{
printf(“Key %d found\n", ptr->data);
}
return start;
}
1. Inserting a new node at the front of a DLL
NODE* insertfront(NODE *start, int n)
{
NODE *new_node;
new_node = (NODE *)malloc(sizeof(struct node));
new_node ->data = n;
new_node ->leftlink=NULL;
new_node ->rlink=NULL;
if(start==NULL)
{
start=new_node;
}
else
{
new_node ->rlink = start;
start->leftlink= new_node;
start = new_node;
}
return start;
}
2. Inserting a node at NODE* insertend(NODE *start, int n)
{
the end of a DLL NODE *new_node, *ptr;
new_node = (NODE *) malloc(sizeof(struct node));
new_node ->data = n;
new_node -> leftlink =NULL
new_node ->rlink = NULL;
if (start == NULL)
{
start = new_node;
}
else
{
ptr = start;
while(ptr -> rlink != NULL){
ptr = ptr -> rlink;}
ptr-> rlink = new_node;
new_node -> leftlink = ptr;
}
return start;
}
3. Inserting a node after a if(ptr == NULL)
given node in a DLL {
printf(“Invalid key\n”);
NODE* i_afterkey(NODE *start, int n, int key) }
{ else if(ptr->data == key && ptr ->rlink == NULL)
NODE *new_node, *ptr; { //Inserting after the last node
new_node = (NODE *) malloc(sizeof(struct node)); new_node->rlink =NULL;
new_node->data= n; new_node->leftlink=ptr;
if (start == NULL) ptr->rlink=new_node;
{ }
printf(“List Empty\n”); else
} {
else new_node->rlink = ptr->rlink;
{ (ptr->rlink)->leftlink = new_node;
ptr= start; new_node ->leftlink = ptr;
while((ptr != NULL) && (ptr->data!=key)) ptr->rlink = new_node;
{ }
ptr =ptr->rlink; }
} return start;
}
4. Inserting a node before a
else
given node in a DLL { // Traverse till key is found or till last node
ptr= start;
NODE* i_beforekey(NODE *start, int n, int key) while((ptr != NULL) && (ptr->data!=key))
{ {
NODE * new_node , * temp, * ptr; temp = ptr;
new_node = (NODE *) malloc(sizeof(struct node)); ptr = ptr->rlink;
}
new_node ->data = n;
if(ptr == NULL) // Searched till end of list
if (start== NULL)
printf(“Invalid key\n”);
{
else
printf(“List Empty, cannot insert\n”); {
} new_node ->rlink = ptr;
if(start ->data==key ) // if start node is the key ptr->leftlink = new_node ;
{ new_node ->leftlink = temp;
new_node ->rlink = start; temp->rlink = new_node ;
start ->leftlink = new_node ; }
new_node->leftlink = NULL; }
start = new_node ; return start;
} }
5. Insert at a position else
{
count =1;
NODE* i_pos(NODE *start, int n, int position) ptr = start;
{ while((ptr != NULL) && (count !=position))
NODE * new_node, *temp, *ptr; {
int count; temp = ptr;
new_node= (NODE *) malloc(sizeof(struct node)); ptr = ptr ->rlink;
new_node->data= n; count++;
if ((start == NULL) && (position ==1)) }
{ if(ptr == NULL)
start = new_node; printf(“Invalid postion\n”);
new_node->rlink = NULL; else
new_node->leftlink = NULL; {
} new_node->rlink = ptr;
else if ((start == NULL) && (position >1)) ptr ->leftlink = new_node;
{ new_node->leftlink = temp;
printf(“Invalid postion\n”); temp->rlink = new_node;
}
}
}
return start; }
Deleting a Node from a Doubly Linked List

Case 1: Deleting the start Node from a DLL


Case 2: Deleting the Last Node from a DLL
Case 3: Delete at a position
Case 4: Delete a key node
Case 5: Delete after a key number
Case 6: Delete before a key number
#include <stdio.h> while(1)
#include <stdlib.h> {
printf("Enter your choice\n"); case 4: printf(" Enter the key value");
struct node printf("1. Delete from front\n“);
{ scanf("%d",&k);
printf("2. Delete from last");
start= deletekey(start,k);
struct node *leftlink; printf(“3. Delete at a position\n");
break;
int data; printf(“4. Delete the key node\n");
case 5: printf(" Enter the key value");
printf(“5. Delete after the key\n");
struct node *rlink; printf(“6. Delete before key\n"); scanf("%d",&k);
}; printf(“7. Display\n"); start= deleteafterkey(start,k);
typedef struct node NODE; printf(“8. Stop\n"); break;
scanf("%d", &choice); case 6: printf(" Enter the key value");
NODE* deletefront(NODE *); switch(choice) scanf("%d",&k);
NODE* deleteend(NODE *); { start= deletebeforekey(start,k);
NODE* deleteposition(NODE *, int); case 1: break;
NODE* deletekey(NODE *, int); start= deletefront(start); case 7: display(start);
NODE* deleteafterkey(NODE *, int); break; break;
NODE* deletebeforekey(NODE *, int); case 2: case 8: exit(0);
start= deleteend(start); }
void display(NODE *); break; }
case 3: printf(" Enter the position \n"); }
void main() scanf(“%d”,&pos);
{ start= deleteposition(start,pos);
NODE *start=NULL; break;
int k,pos,choice;
1. Deleting the front node from a DLL
NODE* deletefront(NODE *start)
else
{
{
NODE *ptr;
ptr = start;
if(start == NULL)
start = start ->rlink;
{
start ->leftlink = NULL;
printf("List is empty\n" );
printf(“Item deleted is %d\n" , ptr ->data);
return NULL;
free(ptr);
}
return start;
else if(start -> rlink == NULL)
}
{
}
printf(“Item deleted is %d\n" , start-
>data); Check below conditions
free(start); 1. List is empty
return NULL; 2. List with single node
} 3. List with multiple nodes
2. Deleting the last node from a DLL
NODE* deleteend(NODE *start ) else
{ {
NODE *ptr, *temp; ptr= start ;
while(ptr -> rlink != NULL)
if (start == NULL) {
{ temp = ptr;
printf(“List Empty\n”); ptr = ptr -> rlink;
return NULL; }
} temp -> rlink = NULL;
else if (start ->rlink == NULL)//one node printf(“deleted item: %d\n", ptr->data);
{ free(ptr);
printf(“deleted item: %d\n", start ->data); }
free(start ); return start ;
return NULL; }
} Check below conditions
1. List is empty
2. List with single node
3. List with multiple nodes
NODE* deleteafterkey(NODE *start, int key)
{
3. Delete after a key number
NODE *ptr, *temp; else if((ptr->rlink == NULL)&&(ptr->data ==key))
if (start == NULL) {
{ printf("Key is in the last node, deletion not possible\n");
printf("List Empty\n"); }
return start; else
} {
else ptr->rlink = temp->rlink;
{// Traverse till key is found or till last node printf("Item deleted: %d\n",temp->data);
ptr = start; if(temp->rlink!=NULL)
temp = start->rlink; {
while((ptr->rlink!=NULL)&&(ptr->data !=key)) (temp->rlink)->leftlink = ptr;
{ }
ptr= ptr->rlink; free(temp);
temp = temp ->rlink; }
return start; Check below conditions
}
if(ptr->rlink==NULL&&(ptr->data !=key)) } 1. List is empty
2. Key is not present in the list
{
printf("Key is not present in the list\n"); 3. Key is present in the last node

} 4. Key is in other nodes rather than last and 2 nd last


5. Key is present in the 2nd last node
else
4. Delete before a key number {// Traverse till key is found or till last node
prev = start;
NODE* deletebeforekey(NODE *start, int key)
temp = start->rlink;
{
ptr= (start->rlink)->rlink;
NODE *temp, *ptr, *prev;
while((ptr!= NULL)&&(ptr->data !=key))
if (start == NULL)
{
printf(“List Empty\n”);
prev = prev->rlink;
return start;
temp = temp->rlink;
if(start->data == key ) // if 1st node is the key
ptr= ptr->rlink;
{
}
printf(“Delete not possible\n");
if(ptr== NULL) // Searched till end of list
return start;
printf(“Invalid key\n”);
}
else
// if 2nd node is the key then delete the start and return ptr
{
as start
prev->rlink = ptr;
ptr= start->rlink;
ptr->leftlink=prev;
if ((ptr->data== key)
printf(“Item deleted: %d\n",temp->data);
{
free(temp);
printf(“Item deleted: %d\n", start->data);
}
}
free(start);
return start;
start=ptr;
}
}
5. Delete at a position
• Check below conditions
1. List is empty
2. Single node with position 1
3. Multiple Nodes with position 1
a) Invalid Position
b) position rather than last and first
c) Position is at the last node
NODE* deleteposition(NODE *start, int pos)
{
NODE *temp, *ptr; else
5. Delete at a position
int count=1; {
ptr = start;
if (start == NULL) //List is empty while((ptr!= NULL) && (count !=pos))
{ {
printf(“List Empty\n”); temp = ptr;
return NULL; ptr = ptr ->rlink;
} count++;
if((start ->rlink== NULL) && (pos == 1)) }
{ //Single node with position 1 if(ptr == NULL )
printf(“Item deleted: %d\n", start ->data); printf(“Invalid postion\n”);
else
free(start); {
return NULL; temp ->rlink = ptr ->rlink;
} if(ptr->rlink!=NULL)
else if((start ->rlink!=NULL) && (pos == 1)) {
{ (ptr ->rlink)->leftlink = temp;
ptr=start; }
printf(“%d”,ptr->data); printf(" Item deleted: %d\n", ptr ->data);
start=start->rlink; free(ptr);
start->leftlink=NULL; }
free(ptr); return start; } }
return(start);
else
6. Delete a key node {// Traverse till key is found or till last node
ptr = start;
NODE* deletekey(NODE *start, int key) while((ptr!= NULL)&&(ptr->data!=key))
{ {
NODE *ptr, *prev; temp = ptr;
if (start == NULL) // No Nodes ptr= ptr->rlink;
{ }
printf(“List Empty\n”); if(ptr == NULL) // Searched till end of list
} printf(“Invalid key number\n”);
else if(start->data==key ) //first node is key else
{ {
ptr=start; temp->rlink = ptr->rlink;
start = start->rlink; if(ptr->rlink !=NULL)
printf(“Item Deleted: %d\n“, ptr->data); {
free(ptr); (ptr->rlink)->leftlink = temp;
} }
Check below conditions printf(“Item deleted: %d\n",ptr->data);
1. List is empty free(ptr);
2. First node is key
3. Invalid key
}
4. key is present in other nodes rather than last }
5. key is at the last node
return start; }

You might also like