Saturday, 23 December 2017

Write a C Program to perform following operations on Doubly Linked List ADT :
i. Create
ii. Insert
iii. Delete
iv. Display
v. Exit

Implementation of the above problem:
#include <stdio.h>
#include <malloc.h>
#include<process.h>
typedef struct DList_tag
{
int data;
struct DList_tag *rlink, *llink;
}node;
/**************Function Declaration Begin**********/
node *DLcreation(node **);
void DLinsertion(node **, node **, int, int);
void DLdeletion(node **, node**);
void DLdisplay(node *, node *);
/**************Function Declaration End**********/
void main()
{
node *left=NULL,*right;
int item,pos,ch;
printf("\n\t\tProgram for doubly linked list\n");
do
{
printf("\n\t\tMenu");
printf("\n\t\t1.Create");
printf("\n\t\t2.Insert");
printf("\n\t\t3.Delete");
printf("\n\t\t4.Display");
printf("\n\t\t5.Exit");
printf("\n\t\tEnter choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
left = DLcreation(&right);
break;
case 2:
scanf("%d",&item);
do
{
printf("\nEnter position of insertion :");
scanf("%d",&pos);
}while(pos < 1);
DLinsertion(&left,&right,item,pos);
break;
case 3:
DLdeletion(&left,&right);
break;
case 4:
printf("\n\t**** Doubly linked list *****\n");
DLdisplay(left,right);
break;
case 5:
exit(0);
default:
printf("\n Wrong Choice");
}
}while(ch!=5);
printf("\n");
}
/********** Creating of double linked list MENU **********/
/********** Function Definition begins **********/
node *DLcreation( node **right )
{
node *left, *new_node;
int item,ch;
*right = left = NULL;
do
{
printf("\n\t\tMenu");
printf("\n\t\t1.Add node");
printf("\n\t\t2.Quit");
printf("\n\t\tEnter choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n Enter data:");
scanf("%d",&item);
new_node = (node *)malloc(sizeof(node));
new_node->data = item;
new_node->rlink = NULL;
if(left == NULL)
{
new_node->llink = NULL;
left = new_node;
}
else
{
new_node->llink = (*right);
(*right)->rlink = new_node;
}
(*right) = new_node;
if(left != NULL)
(*right) = new_node;
break;
case 2:
break;
default:
printf("\n Wrong Choice");
}
}while(ch!=2);
return left;
}
/********** Function Definition ends **********/
/********** Insertion of node in double linked list **********/
/********** Function Definition begins **********/
void DLinsertion(node **start, node **right,int item, int pos)
{
node *new_node, *temp;
int i;
if((pos == 1) ||((*start) == NULL))
{
new_node = (node *)malloc(sizeof(node));
new_node->data = item;
new_node->rlink = *start;
new_node->llink = NULL;
if((*start) != NULL)
(*start)->llink = new_node;
else
(*right) = new_node;
*start = new_node;
}
else
{
temp = *start;
i = 2;
while((i < pos) && (temp->rlink != NULL))
{
temp = temp->rlink;
++i;
}
new_node = (node *)malloc(sizeof( node));
new_node->data = item;
new_node->rlink = temp->rlink;
if(temp->rlink != NULL)
temp->rlink->llink = new_node;
new_node->llink = temp;
temp->rlink = new_node;
}
if(new_node->rlink == NULL)
*right = new_node;
}
/********** Function Definition ends **********/
/********** Deletion of node in linked list **********/
/********** Function Definition begins **********/
void DLdeletion( node **start, node **right)
{
node *temp, *prec;
int item;
printf("\nElement to be deleted :");
scanf("%d",&item);
if(*start != NULL)
{
if((*start)->data == item)
{
if((*start)->rlink == NULL)
*start = *right = NULL;
else
{
*start = (*start)->rlink;
(*start)->llink = NULL;
}
}
else
{
temp = *start;
prec = NULL;
while((temp->rlink != NULL) && (temp->data != item))
{
prec = temp;
temp = temp->rlink;
}
if(temp->data != item)
printf("\n Data in the list not found\n");
else
{
if(temp == *right)
*right = prec;
else
temp->rlink->llink = temp->llink;
prec->rlink = temp->rlink;
}
}
}
else
printf("\n!!! Empty list !!!\n");
return;
}
/********** Function Definition ends **********/
/********** Displaying nodes of double linked list **********/
/********** Function Definition begins **********/
void DLdisplay(node *start, node *right)
{
printf("\n***** Traverse in Forward direction *****\n left->");
while(start != NULL)
{
printf("%d-> ",start->data);
start = start->rlink;
}
printf("right");
printf("\n***** Traverse in Backward direction *****\n right->");
while(right != NULL)
{
printf("%d-> ",right->data);
right = right->llink;
}
printf("left");
}
/********** Function Definition ends **********/


Thanks
Mukesh Rajput

No comments:

Post a Comment

Thanks
Mukesh Rajput