Tuesday 26 July 2016

Double Link List Complete Program

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

/* ************************************
Girfa Student Help
Double Link List Program
for More DS Program Visit : http://girfahelp.blogspot.in/p/data.html
Programmer Name : Mritunjoy Sengupta
   ***************************************
*/

typedef struct n
{
int data;
struct n *pre,*next;
}node;
node* start=NULL;

node* create(int);
void addfirst(int);
void addlast(int);
void addbefore(int);
void addafter(int);
void del(int);
void print();



void main()
{
int opt,n,i;
do
{
clrscr();
printf("\n\t1. Add at first\n\t2. Add at last\n\t3. Add before a no.\n\t4. Add after a no.\n\t5. Delete a no.\n\t6. Print the List\n\t0. Exit\n\n\tEnter your choice>> ");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("\n\tEnter the no. you want to add begin>> ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter %d'st number>> ",i);
scanf("%d",&opt);
addfirst(opt);
}
break;
case 2:
printf("\n\tEnter the no. you want to add Last>> ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter %d'st number>> ",i);
scanf("%d",&opt);
addlast(opt);
}
break;
case 3:
if(start==NULL)
printf("\n\tThe list is empty");
else
{
printf("\n\tEnter the no. to be searched>> ");
scanf("%d",&n);
addbefore(n);
}
break;
case 4:
if(start==NULL)
{
printf("\n\tThe list is empty");
getch();
}
else
{
printf("\n\tEnter the no. to be searched>> ");
scanf("%d",&n);
addafter(n);
}
break;
case 5:
if(start==NULL)
{
   printf("\n\tThe List is Empty,enter some values");
   getch();
}
else
{
   printf("\n\tEnter the no. you want to delete>> ");
   scanf("%d",&n);
   del(n);
}
break;
case 6:
if(start==NULL)
{
printf("\n\tThe list is Empty,enter some values");
getch();
}
else
   print();
break;
case 0:
break;
default:
printf("\n\tYou have entered the wrong choice");
getch();
}
}while(opt!=0);
}

node* create(int n)
{
node* nw;
nw=(node*) malloc(sizeof(node));
nw->data=n;
nw->next=NULL;
nw->pre=NULL;
return nw;
}
void addfirst(int s)
{
node *nw;
nw=create(s);
if(start==NULL)
start=nw;
else
{
nw->next=start;
start->pre=nw;
start=nw;
}
}
void addlast(int s)
{
node *nw,*pt;
nw=create(s);
if(start==NULL)
start=nw;
else
{
for(pt=start;pt->next!=NULL;pt=pt->next);
pt->next=nw;
nw->pre=pt;
}
}
void addbefore(int s)
{
node *nw,*pt;
int n;
for(pt=start;pt!=NULL;pt=pt->next)
{
if(pt->data==s)
break;
}
if(pt==NULL)
{
printf("\n\tSearch value not found");
getch();
}
else
{
printf("\n\tSearch value found\n\tEnter the no. to be added>> ");
scanf("%d",&n);
nw=create(n);
if(pt==start)
{
nw->next=start;
start=nw;
}
else
{
nw->next=pt;
nw->pre=pt->pre;
pt->pre->next=nw;
}
}
}
void addafter(int s)
{
node *nw,*pt;
int n;
for(pt=start;pt!=NULL;pt=pt->next)
{
if(pt->data==s)
break;
}
if(pt==NULL)
{
printf("\n\tSearch value not found");
getch();
}
else
{
printf("\n\tSearch value found\n\tEnter the no. you want to add>> ");
scanf("%d",&n);
nw=create(n);
if(pt->next==NULL)
{
nw->pre=pt;
pt->next=nw;
}
else
{
nw->next=pt->next;
nw->pre=pt;
pt->next->pre=nw;
pt->next=nw;
}
}
}
void del(int s)
{
node *pt;
for(pt=start;pt!=NULL;pt=pt->next)
{
if(pt->data==s)
break;
}
if(pt==NULL)
{
printf("\n\tNo. to be deleted not found");
getch();
}
else
{
if(pt->pre==NULL && pt->next==NULL)
{
start=NULL;
free(pt);
printf("\n\tDeleted the last node");
getch();
}
else if(pt->next==NULL)
{
pt->pre->next=NULL;
free(pt);
}
else if(pt->pre==NULL)
{
start=pt->next;
pt->next->pre=NULL;
}
else
{
pt->next->pre=pt->pre;
pt->pre->next=pt->next;
free(pt);
}
}
}
void print()
{
node *pt;
printf("\t");
for(pt=start;pt!=NULL;pt=pt->next)
{
printf("[%d] ",pt->data);
}
getch();
}

No comments:

Post a Comment