In a double linked list, we perform the following operations...
- Insertion
- Deletion
- Display
In a double linked list, the insertion operation can be performed in three ways as follows...
- Inserting At Beginning of the list
- Inserting At End of the list
- Inserting At Specific location in the list
We can use the following steps to insert a new node at beginning of the double linked list...
- Step 1 - Create a node ptr with given value and ptr -> prev as NULL.
- Step 2 - Check whether list is Empty (head == NULL)
- Step 3 - If it is Empty then, assign NULL to ptr -> next and ptr to head.
- Step 4 - If it is not Empty then, assign head to ptr -> next and ptr to head.
We can use the following steps to insert a new node at end of the double linked list...
- Step 1 - Create a node ptr with given value and ptr -> next as NULL.
- Step 2 - Check whether list is Empty (head == NULL)
- Step 3 - If it is Empty, then assign NULL to ptr -> prev and ptr to head.
- Step 4 - If it is not Empty, then, define a node pointer temp and initialize with head.
- Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list (until temp -> next is equal to NULL).
- Step 6 - Assign ptr to temp -> next , temp to ptr ->prev and ptr->next = NULL .
We can use the following steps to insert a new node after a node in the double linked list...
- Step 1 - Create a node ptr with given value.
- Step 2 - Check whether list is Empty (head == NULL)
- Step 3 - If it is Empty then, assign NULL to both ptr -> previous & ptr -> next and set ptr to head.
- Step 4 - If it is not Empty then, define node pointers temp and initialize temp with head.
- Step 5 - Keep moving the temp to its next node until it reaches to the node after which we want to insert the newNode (until temp -> data is equal to location, here location is the node value after which we want to insert the newNode).
- Step 6 - Every time check whether temp is reached to the last node. If it is reached to the last node then display 'Given node is not found in the list!!! Insertion not possible!!!' and terminate the function. Otherwise move the temp1 to next node.
- Step 7 - Assign ptr->next = temp->next, ptr -> prev = temp, temp->next = ptr,
temp->next->prev=ptr;
In a double linked list, the deletion operation can be performed in three ways as follows...
- Deleting from Beginning of the list
- Deleting from End of the list
- Deleting a Specific Node
We can use the following steps to delete a node from beginning of the double linked list...
- Step 1 - Check whether list is Empty (head == NULL)
- Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
- Step 3 - If it is not Empty then, define a Node pointer 'ptr' and initialize with head.
- Step 4 - Check whether list is having only one node (head -> previous is equal to head -> next)
- Step 5 - If it is TRUE, then set head to NULL and delete head.
- Step 6 - If it is FALSE, then assign temp → next to head, NULL to head → previous and delete temp.
We can use the following steps to delete a node from end of the double linked list...
- Step 1 - Check whether list is Empty (head == NULL)
- Step 2 - If it is Empty, then display 'List is Empty!!! Deletion is not possible' and terminate the function.
- Step 3 - If it is not Empty then, define a Node pointer 'ptr' and initialize with head.
- Step 4 - Check whether list has only one Node (head -> prev and head -> next both are NULL)
- Step 5 - If it is TRUE, then assign NULL to head and delete head.
- Step 6 - If it is FALSE, then keep moving ptr until it reaches to the last node in the list. (until ptr -> next is equal to NULL)
- Step 7 - Assign NULL to ptr -> prev -> next and delete ptr.
We can use the following steps to delete a specific node from the double linked list...
- Step 1 - Check whether list is Empty (head == NULL)
- Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
- Step 3 - If it is not Empty, then define a Node pointer 'ptr' and initialize with head.
- Step 4 - Keep moving the ptr until it reaches to the exact node to be deleted or to the last node.
- Step 5 - If it is reached to the last node, then display 'Given node not found in the list! Deletion not possible!!!.
- Step 6 - If it is reached to the exact node which we want to delete, then check whether list is having only one node or not
- Step 7 - If list has only one node and that is the node which is to be deleted then set head to NULL and delete head .
- Step 8 - If list contains multiple nodes, then check whether ptr is the first node in the list (ptr == head).
- Step 9 - If ptr is the first node, then move the head to the next node (head = head -> next), set head of previous to NULL (head -> prev = NULL) and delete ptr.
- Step 10 - If ptr is not the first node, then check whether it is the last node in the list (ptr -> next == NULL).
- Step 11 - If ptr is the last node then set ptr of previous of next to NULL (ptr -> prev -> next = NULL) and delete ptr (free(ptr)).
- Step 12 - If ptr is not the first node and not the last node, then set ptr of previous of next to ptr of next (ptr-> previous -> next = ptr -> next), ptr of next of previous to temp of previous (ptr -> next -> prev = ptr -> prev) and delete ptr (free(ptr)).
We can use the following steps to display the elements of a double linked list...
- Step 1 - Check whether list is Empty (head == NULL)
- Step 2 - If it is Empty, then display 'List is Empty!!!' and terminate the function.
- Step 3 - If it is not Empty, then define a Node pointer 'ptr' and initialize with head.
- Step 4 - Display 'NULL <--- '.
- Step 5 - Keep displaying ptr -> data with an arrow (<===>) until ptr reaches to the last node
- Step 6 - Finally, display ptr -> data with arrow pointing to NULL (ptr -> data ---> NULL).
#include<stdlib.h>
struct node
{
struct node *prev;
struct node *next;
int data;
};
struct node *head;
void insertion_beginning();
void insertion_last();
void insertion_specified();
void deletion_beginning();
void deletion_last();
void deletion_specified();
void display();
void main ()
{
int choice =0;
while(choice != 8)
{
printf("\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n4.Delete from Beginning\n5.Delete from last\n6.Delete the node after the given data\n7.Show\n8.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
insertion_beginning();
break;
case 2:
insertion_last();
break;
case 3:
insertion_specified();
break;
case 4:
deletion_beginning();
break;
case 5:
deletion_last();
break;
case 6:
deletion_specified();
break;
case 7:
display();
break;
case 8:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void insertion_beginning()
{
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter Item value");
scanf("%d",&item);
if(head==NULL)
{
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
}
else
{
ptr->data=item;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}
printf("\nNode inserted\n");
}
}
void insertion_last()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value");
scanf("%d",&item);
ptr->data=item;
if(head == NULL)
{
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
}
else
{
temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = ptr;
ptr ->prev=temp;
ptr->next = NULL;
}
}
printf("\nnode inserted\n");
}
void insertion_specified()
{
struct node *ptr,*temp;
int item,loc,i;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\n OVERFLOW");
}
else
{
temp=head;
printf("Enter the location");
scanf("%d",&loc);
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\n There are less than %d elements", loc);
return;
}
}
printf("Enter value");
scanf("%d",&item);
ptr->data = item;
ptr->next = temp->next;
ptr -> prev = temp;
temp->next = ptr;
temp->next->prev=ptr;
printf("\nnode inserted\n");
}
}
void deletion_beginning()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
head = head -> next;
head -> prev = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_last()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
if(ptr->next != NULL)
{
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_specified()
{
struct node *ptr, *temp;
int val;
printf("\n Enter the data after which the node is to be deleted : ");
scanf("%d", &val);
ptr = head;
while(ptr -> data != val)
ptr = ptr -> next;
if(ptr -> next == NULL)
{
printf("\nCan't delete\n");
}
else if(ptr -> next -> next == NULL)
{
ptr ->next = NULL;
}
else
{
temp = ptr -> next;
ptr -> next = temp -> next;
temp -> next -> prev = ptr;
free(temp);
printf("\nnode deleted\n");
}
}
void display()
{
struct node *ptr;
printf("\n printing values...\n");
ptr = head;
while(ptr != NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
No comments:
Post a Comment