WAP to create double linked list : C Application

/*WAP to create double linked list*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
 int data;
 struct node *llink;
 struct node *rlink;
};
typedef struct node node;
void create(node *);
void display(node *);
void search(node *);
void insert_any(node *);
void delete_any(node *);

void main()
{
 node *header=NULL;
 int n;
 char c='y';
 clrscr();
 while(c=='y')
 {
  printf("\n1.Create\n2.Search\n3.display\n4.insert any\n5.Delete\n6.exit\nEnter your choice:");
  scanf("%d",&n);
  switch(n)
  {
   case 1:
   {
    header=(node*)malloc(sizeof(node));
    header->data=NULL;
    header->llink=NULL;
    header->rlink=NULL;
    create(header);
    break;
   }
   case 2:
   {
    search(header);
    break;
   }
   case 3:
   {
    display(header);
    break;
   }
   case 4:
   {
    insert_any(header);
    break;
   }
   case 5:
   {
    delete_any(header);
    break;
   }
   case 6:
   {
    exit(0);
   }
  }
 }
 getch();
}
void create(node *temphead)
{
 node *temp;
 char a='y';
 while(a=='y')
 {
  temp=(node *)malloc(sizeof(node));
  printf("Enter data:");
  scanf("%d",&temp->data);
  temp->rlink=NULL;
  temphead->llink=temphead;
  temphead->rlink=temp;
  printf("Do you want to create more??????");
  fflush(stdin);
  scanf("%c",&a);
  temphead=temp;
 }
}
void display(node *temphead)
{
 while(temphead!=NULL)
 {
  printf("\n%d",temphead->data);
  temphead=temphead->rlink;
 }
}
void search(node *temphead)
{
 int key,found=0;
 printf("Enter number you want to find:");
 scanf("%d",&key);
 while(temphead!=NULL)
 {

  if(temphead->data==key)
  {
   printf("\nSearch is sucessfull");
   found+=1;
   break;
  }
  temphead=temphead->rlink;
 }
 if(found==0)
 {
  printf("Search is unsucessfull");
 }
}
void insert_any(node *temphead)
{
 int key;
 node *temp;
 node *temp1;
 printf("After which number you want to insert?????");
 scanf("%d",&key);
 while(temphead->rlink!=NULL && temphead->data!=key)
 {
  temphead=temphead->rlink;
 }
 if(temphead->data!=key)
 {
  printf("Value not found insertion not possible");
 }
 else
 {
  temp=(node *)malloc(sizeof(node));
  printf("Enter data:");
  scanf("%d",&temp->data);
  temp1=temphead->rlink;
  temp->llink=temphead;
  temp->rlink=temp1;
  temphead->rlink=temp;
  printf("Insert sucessfully\n");
  if(temp1!=NULL)
  {
   temphead->llink=temp;
  }
 }
}



void delete_any(node *temphead)
{
 node *temp;
 node *p;
 int key;
 printf("Enter the value which you want to delete:");
 scanf("%d",&key);
 while(temphead->rlink!=NULL && temphead->data!=key)
 {
  temp=temphead;
  temphead=temphead->rlink;
 }
 if(temphead->data==key)
 {
  node *temp1;
  temp1=temphead->rlink;
  temp->rlink=temp1;
  temp1->llink=temp;
  free(temphead);
 }
 else
 {
  printf("Value not found......");
 }
}

No comments:

Post a Comment