Google Ads

Monday, May 25, 2009

Circular Linked List

#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();
printf("\n\t\tCIRCULAR LINKED LIST");
do
{
printf("\n1.Create\n2.Insertion \n3.Modify\n4.Delete\n5.view\n6.Count\n7.Exit”);
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:");
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(ch1==1)
ifirst();
else if(ch1==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=newnode;
}
void createlist()
{
char ch;
do
{
getnode();
if(head==NULL)
{
head=newnode;
last=head;
}
else
{
last->link=newnode;
newnode->link=head;
}
last=last->link;
fflush(stdin);
printf("\n\nDo You Want To Continue press (y/n):");
scanf("%c",&ch);
}while(ch=='y'ch=='Y');

}

void ifirst()
{
getnode();
if(head==NULL)
{
head=newnode;
}
else
{
last=head;
while(last->link!=head)
last=last->link;
last->link=newnode;
newnode->link=head;
head=newnode;
}
}
void imiddle()
{
int insdata;
if(head==NULL)
{
printf("\n\n CLL Is Empty");
getnode();
head=newnode;
}
else
{
printf("\n\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!=head);
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->link!=head)
last=last->link;
last->link=newnode;
newnode->link=head;
}
}
void modify()
{
int moddata;
if(head==NULL)
printf("\n CLL 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->link;
}while(last!=head);
if(f2==0)
printf("\n The data is not found in list");
}
}
void dfirst()
{
if(head==NULL)
printf("\n CLL Is Empty");
delnode=head;
if(head->link==head)
head=NULL;
else
{
while(last->link!=head)
last=last->link;
head=head->link;
last->link=head;
}
printf("\n The deleted data is:%d",head->data);
free(delnode);
}
void dmiddle()
{
int deldata;
if(head==NULL)
printf("\n CLL Is Empty");
else
{
printf("\n Enter the data to be deleted:");
scanf("%d",&deldata);
last=head;
if(head->data==deldata)
{
delnode=head;
if(head->link==head)
head=NULL;
else
{
last=head;
while(last->link!=head)
last=last->link;
head=head->link;
last->link=head;
}
}
else
{
prev=head;
last=head->link;
while(last!=head)
{
if(last->data==deldata)
{
delnode=last;
prev->link=last->link;
break;
}
prev=prev->link;
last=last->link;
}
if(last==head)
printf("\nDelete node not found");
}
printf("\n The deleted data is:%d",delnode->data);
free(delnode);
}

}
void dlast()
{
if(head==NULL)
printf("\n CLL Is Empty");
else
{
if(head->link==head)
{
delnode=head;
head=NULL;
}
else
{
last=head;
while(last->link!=head)
{
prev=last;
last=last->link;
}
delnode=last;
prev->link=head;
}
printf("\n The deleted data is:%d",delnode->data);
free(delnode);
}
}
void view()
{
if(head==NULL)
printf("\n CLL Is Empty ");
else
{
last=head;
printf("\n List Values: ");
do
{
printf("%d\n",last->data);
last=last->link;
}while(last!=head);
}
}
void count()
{
int count=0;
if(head==NULL)
printf("\n CLL Is Empty ");
else
{
last=head;
do
{
count++;
last=last->link;
}while(last!=head);
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 (y/n):y

Enter The Data To Node:4


Do You Want To Continue press (y/n):y

Enter The Data To Node:5


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: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 modifed: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

No comments:

Post a Comment