Blogger news

Followers

Wednesday, November 13, 2013
Design,Develop and execute a program in C to implement a doubly linked list where each node consists of integers.The program should support the following operations:
 1.Create a doubly linked list by adding each node at the front.
2.Insert a new node to the left of the node whose key value is read as an input.
3.Delete the node of a given data if it’s found, otherwise display appropriate message
4.Display the contents of the list.


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

 typedef struct node *NODE;
 struct node
 {
   int info;
   NODE left,right;
 };

 NODE first;
 int empty();
 void create();
 void insert();
 void delete();
 void display();

 NODE getnode()
 {
   return((struct node*)malloc(sizeof(struct node)));
 }

 int empty()
 {
  return(first==NULL?1:0);
 }

 void create()
 {
  NODE new;
  int ch;
  first=NULL;

  do
  {
  new=getnode();
  printf("\n enter element to be inserted:");
  scanf("%d",&new->info);
  new->left=new->right=NULL;
  if(first==NULL)
  {
    first=new;
  }
  else
  {
    new->right=first;
    first->left=new;
    first=new;
  }
  printf("\n continue?1\0");
  scanf("%d",&ch);
 }
 while(ch!=0);
}

void insert()
{
 NODE new,curr,prev;
 int x,f=0;
 printf("\n enter the key value of a node\n");
 scanf("%d",&x);
 if(x==first->info)
 {
  new=getnode();
  printf("\n enter a value for new node to be inserted:\n");
  scanf("%d",&new->info);
  new->left=new->right=NULL;
  new->right=first;
  first=new;
 }
 else
 {
  curr=first->right;
  while(curr!=NULL)
  {
   if(x==curr->info)
   {
    f=1;
    break;
   }
   curr=curr->right;
 }
 if(f)
 {
  new=getnode();
  printf("\n enter a value to be inserted \n");
  scanf("%d",&new->info);
  new->left=new->right=NULL;
  prev=curr->left;
  new->right=curr;
  curr->left=new;
  prev->right=new;
  new->left=prev;
 }
 else
 {
 printf("\n invalid key value \n");
}
}
}
void delete()
{
 NODE curr,prev;
 int f=0,x;
 printf("\n enter info value of node you want to delete \n");
 scanf("%d",&x);
 if(x==first->info)
 {
  curr=first;
  first=first->right;
  first->left=NULL;
  printf("\n deleted item %d",curr->info);
  free(curr);
 }
 else
 {
  curr=first->right;
  while(curr!=NULL)
  {
   if(x==curr->info)
   {
    f=1;
    break;
  }
  curr=curr->right;
  }
  if(f)
  {
  prev=curr->left;
  prev->right=curr->right;
  curr->right->left=prev;
  free(curr);
 }
 else
 printf("\n not valid node!! not found \n");
}
}


void display()
{
 NODE curr;
 printf("\n elements of the list \n");
 printf("\n NULL<-");
 for(curr=first;curr!=NULL;curr=curr->right)
 {
  printf("%d<-->",curr->info);
 }
 printf("NULL \n");
}
void main()
{
 int ch;
 clrscr();
 printf("\n \n doubly linked list will be created \n");
 create();
 display();
 printf("\n\n doubly linked list will be created \n");
 printf("\t1->insert\n");
 printf("\t2->delete\n");
 printf("\t3->display\n");
 printf("\t4->exit\n");
 while(1)
 {
  printf("enter your choice \n");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:
     if(first==NULL)
     {
     printf("empty list \n");
     printf("insert left not possible \n");
   }
   else
   {
   insert();
   display();
   }
   break;
  case 2:
    if(empty())
      printf("\n empty list \n");
    else
    {
     delete();
     if(!empty())
     display();
    }
    break;
    case 3:
    if(empty())
    printf("\n empty list \n");
    else
    {
    display();
    }
    break;
    case 4:
      exit(0);
    default:
    printf("\n invalid choice!!");
  }
 }
  getch();
 }



Program end






0 comments: