WRITE A MENU DRIVEN PROGRAM IN "C" FOR DOUBLY LINK LIST WITH ALL OPERATIONS USING SWITCH CASE.

















Doubly Linked List | Set 1 (Introduction and Insertion)


We strongly recommend to refer following post as a prerequisite of this post.
Doubly Linked List (DLL) contains an extra pointer, typically called previous pointer, together with next pointer and data which are there in singly linked list.

             PROGRAM ON DOUBLY LINKLIST

#include<stdio.h>
#include<conio.h>

typedef struct node
 {
  struct node *prev;
  int data;
  struct node *next;
}node;

node *root=NULL;

void insert()
{
  node *temp;
  temp=(node*)malloc(sizeof(node));
  printf("Enter node data (rollno)\n");
  scanf("%d",&temp->data);
  temp->prev=NULL;
  temp->next=NULL;
if(root==NULL)
  {
   root=temp;
  }
else
 {
  node *p;
  p=root;
while(p->next!=NULL)
   {
     p=p->next;
   }
    p->next=temp;
    temp->prev=p;
 }
}
void insertf()
{
node *temp;
temp=(node*)malloc(sizeof(node));
printf("Enter node data (rollno)\n");
scanf("%d",&temp->data);
temp->next=NULL;
temp->prev=NULL;
if(root==NULL)
 {
   root=temp;
 }
else
 {
  temp->next=root;
  root->prev=temp;
  root=temp;
 }
}

int length()
{
 node *temp;
 int a=0;
 temp=root;
 while(temp!=NULL)
 {
  a++;
  temp=temp->next;
 }
return a;
}

void display()
{
node *temp;
temp=root;
if(temp==NULL)
 {
  printf("List is empty\n");
 }
else
 {
  while(temp!=NULL)
    {
      printf("%3d",temp->data);
      temp=temp->next;
    }
 }
}

void insertp()
{
 int pos,len,i=1;
 node *temp,*p;
 printf("Enter location to add\n");
 scanf("%d",&pos);
 len=length();
  if(pos>len)
   {
     printf("Invalid location\n");
     printf("List contain %d data\n",len);
   }
   else
   {
     temp=(node*)malloc(sizeof(node));
     printf("Enter node data(rollno)\n");
     scanf("%d",&temp->data);
     temp->next=NULL;
     temp->prev=NULL;
     p=root;
   while(i<pos)
      {
 p=p->next;
 i++;
      }
 temp->next=p->next;
 p->next->prev=temp;
 temp->prev=p;
 p->next=temp;
}
}

void deleted()
{
node *temp,*p,*q;
int pos,len,i=1;
printf("\nEnter the node position to delete\n");
scanf("%d",&pos);
len=length();
 if(pos>len)
  {
    printf("Invalid location\n");
    printf("List contain %d data\n",len);
  }
else if(pos==1)
   {
     temp=root;
     root=root->next;
     root->prev=NULL;
     free(temp);
   }
else if(pos>1&&pos!=len)
    {
      p=root;
while(i<pos-1)
   {
    p=p->next;
    i++;
   }
 q=p->next;
 p->next=q->next;
 q->next->prev=p;
 q->prev=NULL;
 q->next=NULL;
 free(q);
}
else if(pos==len&&length()>1)
 {
   i=1;
   len=length();
   printf("Length %d\n", len);
   p=root;
while(i!=len)
  {
    p=p->next;
    i++;
  }
 p->prev->next=NULL;
 free(p);
}
}

void modify()
{
   int a,b=0;
   node *p;
   p=root;
   printf("\nEnter the data to be modified \n");
   scanf("%d",&a);
while(p!=NULL)
 {
  if(p->data==a)
   {
     printf("\nEnter the data  ");
     scanf("%d",&p->data);
     b=1;
     break;
   }
   p=p->next;
 }
if(b==0)
 printf("Data not found");
}

void displayrev()
{
  int a[20]={0},i=1,j;
  node *temp;
  temp=root;
if(temp==NULL)
  {
   printf("List is empty\n");
  }
else
  {
while(temp!=NULL)
  {
   a[i]=temp->data;
   temp=temp->next;
   i++;
  }
i--;
for(j=i;j>0;j--)
  {
    printf("%3d",a[j]);
  }
  }
}

int menu()
{
int a;
printf("\n*****menu*****");
printf("\nEnter 1 to Insert a record.");
printf("\nEnter 2 Insert a record at first");
printf("\nEnter 3 Insert a record at particular position");
printf("\nEnter 4 to Delete a record.");
printf("\nEnter 5 to Modify a record.");
printf("\nEnter 6 to Display a record.");
printf("\nEnter 7 to dispaly a record in reverse.");
printf("\nEnter 8 to Exit.");
printf("\nEnter your choice : ");
scanf("%d",&a);
return a;
}
int main()
{
int r;
clrscr();
do
{
switch(r=menu())
{
 case 1:insert();
 break;
 case 2:insertf();
 break;
 case 3:insertp();
 break;
 case 4:deleted();
 break;
 case 5:modify();
 break;
 case 6:display();
 break;
 case 7:displayrev();
 break;
 case 8:printf("\n****Thankyou****");
 break;
}
}while(r!=8);
getch();
return 0;
}

No comments:

Post a Comment

If you have any problems related to solutions or any concept please let me know.

Copyright (c) 2020 Custom Programs All Right Reserved

Pages