#include “stdio.h”
#include “conio.h”
#define NULL 0
struct node
{
int data;
struct node *flink,*blink;
};
struct node *head=NULL,*newnode,*last,*delnode;
int f1=0,f2=0;
void createlist();
void getnode();
void modify();
void ifirst();
void imiddle();
void ilast();
void view();
void dfirst();
void dmiddle();
void dlast();
void modify();
void count();
void main()
{
int ch,ch1,ch2;
clrscr();
do
{
printf("\n1.Create\n2.Insertion \n3.Modify\n4.Delete\n5.view\n6.Coun
printf("\n\n Enter Your Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
createlist();
break;
case 2:
printf("\n\nInsert Menu");
printf("\n1.Insert First\n2.Insert Middle\n3.Insert Last");
printf("\n\nEnter your choice:");
scanf("%d",&ch1);
if(ch1==1)
ifirst();
else if(ch1==2)
imiddle();
else
ilast();
break;
case 3:
modify();
break;
case 4:
printf("\n\nDelete Menu");
printf("\n1.Delete First\n2.Delete Middle\n3.Delete Last");
printf("\n\nEnter your choice:");
scanf("%d",&ch2);
if(ch2==1)
ifirst();
else if(ch2==2)
imiddle();
else
ilast();
break;
case 5:
view();
break;
case 6:
count();
break;
case 7:
exit(0);
break;
}
}while(ch!=7);
getch();
}
void getnode()
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("\n Enter The Data To Node:");
scanf("%d",&newnode->data);
newnode->flink=NULL;
newnode->blink=NULL;
}
void createlist()
{
int ch;
do
{
getnode();
if(head==NULL)
{
head=newnode;
last=head;
}
else
{
last->flink=newnode;
newnode->blink=last;
last=newnode;
}
printf("\n\nDo You Want To Continue press (1):");
scanf("%d",&ch);
}while(ch==1);
}
void ifirst()
{
getnode();
if(head==NULL)
{
head=newnode;
}
else
{
newnode->flink=head;
head->blink=newnode;
head=newnode;
}
}
void imiddle()
{
int insdata;
if(head==NULL)
{
printf("\n\n DLL Is Empty");
getnode();
head=newnode;
}
else
{
printf("\n\n Enter the data after which data to be added:");
scanf("%d",&insdata);
last=head;
while(last!=NULL)
{
if(last->data==insdata)
{
getnode();
newnode->flink=last->flink;
last->flink->blink=newnode;
last->flink=newnode;
newnode->blink=last;
f1++;
}
last=last->flink;
}
if(f1==0)
printf("\n The data is not found in list");
}
}
void ilast()
{
getnode();
if(head==NULL)
{
head=newnode;
last=head;
}
else
{
last=head;
while(last->flink!=NULL)
last=last->flink;
last->flink=newnode;
newnode->blink=NULL;
}
}
void modify()
{
int moddata;
if(head==NULL)
printf("\n DLL Is Empty");
else
{
printf("\n\n Enter the data to be modifed:");
scanf("%d",&moddata);
last=head;
do
{
if(last->data==moddata)
{
printf("\nEnter new data:");
scanf("%d",&last->data);
f2++;
}
last=last->flink;
}while(last!=NULL);
if(f2==0)
printf("\n The data is not found in list");
}
}
void dfirst()
{
if(head==NULL)
printf("\n DLL Is Empty");
else
{
delnode=head;
head=head->flink;
head->blink=NULL;
printf("\n The deleted data is:%d",delnode->data);
free(delnode);
}
}
void dmiddle()
{
int deldata;
if(head==NULL)
printf("\n DLL Is Empty");
else
{
printf("\n Enter the data to be deleted:");
scanf("%d",&deldata);
if(head->data==deldata)
{
delnode=head;
printf("\n The deleted data is:%d",delnode->data);
free(delnode);
}
else
{
last=head;
while(last!=NULL)
{
if(last->data==deldata)
{
delnode=last;
last->flink->blink=last->blink;
last->blink->flink=last->flink;
printf("\n The deleted data is:%d",delnode->data);
free(delnode);
f2=1;
}
else
last=last->flink;
}
}
if(f2==0)
printf("\n The delete node is not found");
}
}
void dlast()
{
if(head==NULL)
printf("\n DLL Is Empty");
else
{
if(head->flink==NULL)
{
delnode=head;
printf("\n The deleted data is:%d",delnode->data);
head=NULL;
}
else
{
last=head;
while(last->flink!=NULL)
last=last->flink;
delnode=last;
last->blink->flink=NULL;
printf("\n The deleted data is:%d",delnode->data);
free(delnode);
}
}
}
void view()
{
if(head==NULL)
{
printf("\n DLL Is Empty ");
return;
}
else
{
last=head;
printf("\n List Values:");
do
{
printf(" %d",last->data);
last=last->flink;
}while(last!=NULL);
}
}
void count()
{
int count=0;
if(head==NULL)
printf("\n DLL Is Empty ");
else
{
last=head;
do
{
count++;
last=last->flink;
}while(last!=NULL);
printf("\n Number Of node in the list:%d",count);
}
}
Output will be
1.Create
2.Insertion
3.Modify
4.Delete
5.view
6.Count
7.Exit
Enter Your Choice:1
Enter The Data To Node:3
Do You Want To Continue press (1):1
Enter The Data To Node:4
Do You Want To Continue press (1):1
Enter The Data To Node:5
Do You Want To Continue press (1):0
1.Create
2.Insertion
3.Modify
4.Delete
5.view
6.Count
7.Exit
Enter Your Choice:2
Insert Menu
1.Insert First
2.Insert Middle
3.Insert Last
Enter your choice:1
Enter The Data To Node:2
1.Create
2.Insertion
3.Modify
4.Delete
5.view
6.Count
7.Exit
Enter Your Choice:5
List Values:
2
3
4
5
1.Create
2.Insertion
3.Modify
4.Delete
5.view
6.Count
7.Exit
Enter Your Choice:2
Insert Menu
1.Insert First
2.Insert Middle
3.Insert Last
Enter your choice: 2
Enter the data after which data to be added: 4
Enter The Data To Node: 6
1.Create
2.Insertion
3.Modify
4.Delete
5.view
6.Count
7.Exit
Enter Your Choice:2
Insert Menu
1.Insert First
2.Insert Middle
3.Insert Last
Enter your choice:3
Enter The Data To Node:7
1.Create
2.Insertion
3.Modify
4.Delete
5.view
6.Count
7.Exit
Enter Your Choice:5
List Values:
2
3
4
6
5
7
1.Create
2.Insertion
3.Modify
4.Delete
5.view
6.Count
7.Exit
Enter Your Choice:3
Enter the data to be modified: 6
Enter new data: 1
1.Create
2.Insertion
3.Modify
4.Delete
5.view
6.Count
7.Exit
Enter Your Choice: 4
Delete Menu
1.Delete First
2.Delete Middle
3.Delete Last
Enter your choice: 1
The deleted data is: 2
1.Create
2.Insertion
3.Modify
4.Delete
5.view
6.Count
7.Exit
Enter Your Choice:4
Delete Menu
1.Delete First
2.Delete Middle
3.Delete Last
Enter your choice:2
Enter the data to be deleted:1
The deleted data is:1
1.Create
2.Insertion
3.Modify
4.Delete
5.view
6.Count
7.Exit
Enter Your Choice:5
List Values:
2
3
4
5
7
1.Create
2.Insertion
3.Modify
4.Delete
5.view
6.Count
7.Exit
Enter Your Choice:4
Delete Menu
1.Delete First
2.Delete Middle
3.Delete Last
Enter your choice:3
The deleted data is:7
1.Create
2.Insertion
3.Modify
4.Delete
5.view
6.Count
7.Exit
Enter Your Choice:5
List Values:
2
3
4
5
1.Create
2.Insertion
3.Modify
4.Delete
5.view
6.Count
7.Exit
Enter Your Choice:6
Number Of node in the list:4
1.Create
2.Insertion
3.Modify
4.Delete
5.view
6.Count
7.Exit
Enter Your Choice:7
Google Ads
Monday, May 25, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment