Saturday, August 9, 2014

Program for various operations with doubly link list

Program for the various operations with a doubly link list. Comment below if you need help

#include<stdio.h>

#include<stdlib.h>

struct node

{

 int data;

 struct node *next,*prev;

}*start=NULL,*newn,*temp,*ptr;

int num,pos,ele;

void insert_beg();

void insert_beg();

void insert_end();

void insert_aftr();

void insert_pos();

void delete_beg();

void delete_end();

void delete_ele();

void display();

main()

{

 int ch,c,ch1,p,ch2,q;

 do

 {

 printf("\n DLL MENU\n1.Ins\n2.Del\n3.Disp");

 printf("\nEnter the number corresponding to your operation:");

 scanf("%d",&ch);

 switch(ch)

 {

 case 1:do

 {

 printf("\nINSERT MENU\n1.beginning\n2.End\n3.After a node\n4.at a

position\n5.Display");

 printf("\nEnter your choice:");

 scanf("%d",&ch1);

 switch(ch1)

 {

 case 1:insert_beg();

 break;

 case 2:insert_end();

 break;

 case 3:insert_aftr();

 break;

 case 4:insert_pos();

 break;

 case 5:display();

 break;

 default:printf("\nWrong Choice\n");

 break;

 }

 printf("\nDo you want to continue-Insert Menu?(1.Yes;2.No)");

 scanf("%d",&p);

 }while(p!=2);

 break;

 case 2:do

 {

 printf("\nDELETE MENU\n1.Front\n2.End\n3.A specific node\n4.Display");

 printf("\nEnter your choice:");

 scanf("%d",&ch2);

 switch(ch2)

 {

 case 1:delete_beg();

 break;

 case 2:delete_end();

 break;

 case 3:delete_ele();

 break;

 case 4:display();

 break;

 default:printf("\nWrong Choice\n");

 break;

 }

 printf("\nDo you want to continue-Delete Menu?(1.Yes;2.No):");

 scanf("%d",&q);

 }while(q!=2);

 break;

 case 3:display();

 break;

 default:printf("\nWrong Choice\n");

 break;

 }

 printf("\nDo you want to continue-Main Menu?(1.Yes;2.No):");

 scanf("%d",&c);

 }while(c!=2);

}

void display()

{

 if(start==NULL)

 printf("\nLink List is empty\n");

 else

 {

 temp=start;

 while(temp->next!=NULL)

 {

 printf("%d<->",temp->data);

 temp=temp->next;

}

printf("%d",temp->data);

 }

}

void insert_beg()

{

 printf("\nEnter the element to be inserted:");

 scanf("%d",&num);

 newn=(struct node*)malloc(sizeof(struct node));

 newn->data=num;

 if(start==NULL)

 {

 newn->next=NULL;

 newn->prev=NULL;

 }

 else

 {

 newn->prev=NULL;

 newn->next=start;

 start->prev=newn;

 }

 start=newn;

}

void insert_end()

{

 printf("\nEnter the element to be inserted:");

 scanf("%d",&num);

 newn=(struct node*)malloc(sizeof(struct node));

 newn->data=num;

 if(start==NULL)

 {

 newn->next=NULL;

 newn->prev=NULL;

 start=newn;

 }

 else

 {

 temp=start;

 while(temp->next!=NULL)

 {

 temp=temp->next;

 }

 temp->next=newn;

 newn->prev=temp;

 newn->next=NULL;

 }

}

void insert_aftr()

{

 int flag=0;

 if(start==NULL)

 printf("\nLink List is empty\n");

 else

 {

 printf("\nEnter the element after which insertion must be done:");

 scanf("%d",&ele);

 printf("\nEnter the element to be inserted:");

 scanf("%d",&num);

 newn=(struct node*)malloc(sizeof(struct node));

 newn->data=num;

 temp=start;

 while(temp!=NULL)

 {

 if(temp->data==ele)

 {

 if(temp->next==NULL)

 insert_end();

 else

{ newn->next=temp->next;

 newn->prev=temp;

 temp->next->prev=newn;

 temp->next=newn;

 }

 flag=1;

 break;

 }

 temp=temp->next;

 }

 if(flag==0)

 printf("\nElement not found\n");

 }

}

void insert_pos()

{

 int k=1,flag=0;

 printf("\nEnter the position for insertion:");

 scanf("%d",&pos);

 if(pos==1)

 insert_beg();

 else

 {

 printf("\nEnter the element to be inserted:");

 scanf("%d",&num);

 newn=(struct node*)malloc(sizeof(struct node));

 newn->data=num;

 temp=start->next;

 while(temp!=NULL)

 {

 k++;

 if(k==pos)

 {

 newn->next=temp;

 temp->prev->next=newn;

 newn->prev=temp->prev;

 temp->prev=newn;

 flag=1;

 break;

 }

 temp=temp->next;

 }

 if(flag==0)

 printf("\nPosition not found\n");

 }

}

void delete_beg()

{

 if(start==NULL)

 printf("\nLL is empty\n");

 else

{

 if(start->next==NULL)

{

free(start);

start=NULL;

}

else

 {

 temp=start;

 start=start->next;

 start->prev=NULL;

 free(temp);

 }

}

}

void delete_end()

{

 if(start==NULL)

 printf("\nLL is empty\n");

 else if(start->next==NULL)

 delete_beg();

 else

 {ptr =temp;

 temp=start;

 while(temp->next!=NULL)

 {

 temp=temp->next;

 }

 temp->prev->next=NULL;

 free(temp);

 }

}

void delete_ele()

{

 int flag=0;

 if(start==NULL)

 printf("\nLL is empty\n");

 else

 {

 printf("\nEnter the element to be deleted:");

 scanf("%d",&num);

 temp=start;

 while(temp!=NULL)

 {

 if(start->data==num)

 {

 delete_beg();

 flag=1;

 temp=start;

 break;

 }

 else if(temp->data==num)

 {

 if(temp->next==NULL)

 {

 delete_end();

 flag=1;

 break;

 }

 else

 {

 ptr=temp;

 temp->prev->next=temp->next;

 temp->next->prev=temp->prev;

 free(ptr);

 flag=1;

 break;

 }

 }

 temp=temp->next;

 }

 if(flag==0)

 printf("\nElement not found\n");

 }

}

No comments:

Post a Comment