#include “stdio.h”
#include “conio.h”
struct node
{
int data;
struct node *link;
};
struct node *head=NULL,*newnode,*last,*prev,*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\n”);
printf(“5.view\n6.Count\n7.Exit”);
printf("\n Enter Your Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
createlist();
break;
case 2:
printf("\nInsert Menu");
printf("\n1.Insert First\n2.Insert Middle\n3.Insert Last");
printf("\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("\nDelete Menu");
printf("\n1.Delete First\n2.Delete Middle\n3.Delete Last");
printf("\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->link=NULL;
}
void createlist()
{
char ch3;
do
{
getnode();
if(head==NULL)
{
head=newnode;
last=head;
}
else
{
last->link=newnode;
last=newnode;
}
printf("\n\nDo You Want To Continue press (y/n):");
scanf("%c",&ch);
}while(ch=='y'ch=='Y');
}
void ifirst()
{
getnode();
if(head==NULL)
{
printf("\n SLL Is Empty");
head=newnode;
}
else
{
newnode->link=head;
head=newnode;
}
}
void imiddle()
{
int insdata;
if(head==NULL)
{
printf("\n SLL Is Empty");
getnode();
head=newnode;
}
else
{
printf("\n Enter the data after which data to be added:");
scanf("%d",&insdata);
last=head;
do
{
if(last->data==insdata)
{
getnode();
newnode->link=last->link;
last->link=newnode;
f1++;
}
last=last->link;
}while(last!=NULL);
if(f1==0)
printf("\n The data is not found in list");
}
}
void ilast()
{
getnode();
if(head==NULL)
{
printf("\n SLL Is Empty");
head=newnode;
}
else
{
last=head;
while(last->link!=NULL)
last=last->link;
last->link=newnode;
last=newnode;
}
}
void modify()
{
int moddata;
if(head==NULL)
printf("\n SLL Is Empty");
else
{
printf("\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->link;
}while(last!=NULL);
if(f2==0)
printf("\n The data is not found in list");
}
}
void dfirst()
{
if(head==NULL)
printf("\n SLL Is Empty");
else
{
delnode=head;
head=head->link;
printf("\n The deleted data is:%d",delnode->data);
free(delnode);
}
}
void dmiddle()
{
int deldata;
if(head==NULL)
printf("\n SLL 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->link;
prev=head;
while(last!=NULL)
{
if(last->data==deldata)
{
delnode=last;
prev->link=last->link;
printf("\n The deleted data is:%d",delnode->data);
free(delnode);
f2++;
}
else
{
last=last->link;
prev=prev->link;
}
}
if(f2==0)
printf("\n The data is not found in list");
}
}
}
void dlast()
{
if(head==NULL)
printf("\n SLL Is Empty");
else if(head->link==NULL)
{
delnode=head;
printf("\nThe deleted data is:%d",delnode->data);
head=NULL;
}
else
{
last=head;
while(last->link!=NULL)
{
prev=last;
last=last->link;
}
delnode=last;
prev->link=NULL;
last=prev;
printf("\n The deleted data is:%d",delnode->data);
free(delnode);
}
}
void view()
{
if(head==NULL)
printf("\n SLL Is Empty ");
else
{
last=head;
printf("\n List Values:\n");
while(last!=NULL)
{
printf("%d ",last->data);
last=last->link;
}
}
}
void count()
{
int count=0;
if(head==NULL)
printf("\n SLL Is Empty ");
else
{
last=head;
do
{
count++;
last=last->link;
}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: 5
Do You Want To Continue press (y/n):y
Enter The Data To Node: 6
Do You Want To Continue press (y/n):y
Enter The Data To Node: 7
Do You Want To Continue press (y/n):n
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: 4
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: 3
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: 9
1.Create
2.Insertion
3.Modify
4.Delete
5.view
6.Count
7.Exit
Enter Your Choice:5
List Values:
4
3
5
6
7
9
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: 4
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:
3
5
7
9
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: 9
1.Create
2.Insertion
3.Modify
4.Delete
5.view
6.Count
7.Exit
Enter Your Choice: 5
List Values:
3
5
7
1.Create
2.Insertion
3.Modify
4.Delete
5.view
6.Count
7.Exit
Enter Your Choice: 6
Number Of node in the list: 3
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