17. Program for
implementing circular linked list?
Program
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
}*last=NULL,*newnode,*temp,*prev;
void ins();
void inse();
void insb();
void insbt();
void del();
void dele();
void delb();
void delbt();
void disp();
void create();
void createnode();
void main()
{
char ch;
int c;
clrscr();
create();
do
{
printf("MENU\n1.INSERT\n2.DELETE\n");
printf("Enter your choice\n");
scanf("%d",&c);
switch(c)
{
case 1:
ins();
break;
case 2:
del();
break;
default:
printf("Invalid choice");
}
disp();
printf("\n\nPress any key to continue or 'n' to exit..\n");
scanf(" %c",&ch);
}
while(ch!='n');
getch();
}
/*------Creating a node------*/
/*------Creating a node------*/
void createnode()
{
int item;
printf("Enter the element\n");
scanf("%d",&item);
newnode=(struct node*) malloc(sizeof(struct node));
newnode->data=item;
newnode->link=NULL;
}
/*-----Creating the linked list-----*/
/*-----Creating the linked list-----*/
void create()
{
int n,i=0;
last=NULL;
printf("Enter no: of nodes\n");
scanf("%d",&n);
while(i<n)
{
createnode();
if(last==NULL)
{
last=newnode;
last->link=last;
}
else
{
newnode->link=last->link;
last->link=newnode;
last=newnode;
}
i++;
}
}
/*---------Insert operations---------*/
/*---------Insert operations---------*/
void ins()
{
int c;
printf("\nMENU\n1.INSERT AT BEGINING\n2.INSERT AT END\n3.INSERT
BETWEEN NODES\n");
printf("Enter your choice\n");
scanf("%d",&c);
switch(c)
{
case 1:
insb();
break;
case 2:
inse();
break;
case 3:
insbt();
break;
default:
printf("Invalid choice");
}
}
/*--------Inserting at beginning--------*/
void insb()
{
createnode();
if(last==NULL)
{
last=newnode;
last->link=last;
}
else
{
newnode->link=last->link;
last->link=newnode;
}
}
/*--------Inserting at the end--------*/
/*--------Inserting at the end--------*/
void inse()
{
createnode();
if(last==NULL)
{
last=newnode;
last->link=last;
}
else
{
newnode->link=last->link;
last->link=newnode;
last=newnode;
}
}
/*--------Inserting in between the nodes--------*/
/*--------Inserting in between the nodes--------*/
void insbt()
{
int pos,k=1;
createnode();
printf("Enter position of insertion\n");
scanf("%d",&pos);
temp=last->link;
while(k<pos)
{
prev=temp;
temp=temp->link;
k++;
}
prev->link=newnode;
newnode->link=temp;
}
/*---------Delete operations-----------*/
/*---------Delete operations-----------*/
void del()
{
int c;
printf("\nMENU\n1.DELETION FROM BEGINING\n2.DELETION FROM
END\n3.DELETION IN BETWEEN NODES\n");
printf("Enter your choice\n");
scanf("%d",&c);
switch(c)
{
case 1:
delb();
break;
case 2:
dele();
break;
case 3:
delbt();
break;
default:
printf("Invalid choice");
}
}
/*---------Deletion from beginning--------*/
/*---------Deletion from beginning--------*/
void delb()
{
if(last==NULL)
printf("List is empty");
else if(last==last->link)
{
temp=last->link;
last=NULL;
printf("Deleted elemnt is %d",temp->data);
free(temp);
}
else
{
temp=last->link;
last->link=temp->link;
free(temp);
}
}
/*---------Deletion from end--------*/
/*---------Deletion from end--------*/
void dele()
{
if(last==NULL)
printf("List is empty");
else
{
temp=last->link;
while(temp->link!=last)
{
temp=temp->link;
}
temp->link=last->link;
printf("Deleted elemnt is %d",last->data);
free(last);
last=temp;
}
}
/*---------Deletion in between nodes--------*/
void delbt()
{
int pos,k=0;
if(last==NULL)
printf("List is empty");
else
{
printf("Enter position of deletion\n");
scanf("%d",&pos);
temp=last->link;
while(k<pos-1)
{
prev=temp;
temp=temp->link;
k++;
}
prev->link=temp->link;
printf("Deleted elemnt is %d",last->data);
free(temp);
}
}
/*----Displaying the Circular Linked list----*/
/*----Displaying the Circular Linked list----*/
void disp()
{
if(last==NULL)
printf("\nList is empty");
else
{
temp=last->link;
printf("\nList is\n");
while(temp!=last)
{
printf("%d\n",temp->data);
temp=temp->link;
}
printf("%d\n",temp->data);
}
}
Output
Enter no: of nodes
2
Enter the element
34
Enter the element
89
MENU
1.INSERT
2.DELETE
Enter your choice
1
MENU
1.INSERT AT BEGINING
2.INSERT AT END
3.INSERT BETWEEN NODES
Enter your choice
1
Enter the element
55
List is
55
34
89
Press any key to continue or 'n' to exit..
y
MENU
1.INSERT
2.DELETE
Enter your choice
1
MENU
1.INSERT AT BEGINING
2.INSERT AT END
3.INSERT BETWEEN NODES
Enter your choice
2
Enter the element
99
List is
55
34
89
99
MENU
1.INSERT
2.DELETE
Enter your choice
1
MENU
1.INSERT AT BEGINING
2.INSERT AT END
3.INSERT BETWEEN NODES
Enter your choice
3
Enter the element
44
Enter position of insertion
3
List is
55
34
44
89
99
Press any key to continue or 'n' to exit..
y
MENU
1.INSERT
2.DELETE
Enter your choice
2
MENU
1.DELETIION FROM BEGINING
2.DELETION FROM END
3.DELETION IN BETWEEN NODES
Enter your choice
1
Deleted elemnt is 55
List is
34
44
89
99
Press any key to continue or 'n' to exit..
y
MENU
1.INSERT
2.DELETE
Enter your choice
2
MENU
1.DELETIION FROM BEGINING
2.DELETION FROM END
3.DELETION IN BETWEEN NODES
Enter your choice
2
Deleted elemnt is 99
List is
34
44
89
Press any key to continue or 'n' to exit..
y
MENU
1.INSERT
2.DELETE
Enter your choice
2
MENU
1.DELETIION FROM BEGINING
2.DELETION FROM END
3.DELETION IN BETWEEN NODES
Enter your choice
3
Enter position of deletion
2
Deleted elemnt is 89
List is
34
89
Press any key to continue or 'n' to exit..
n
No comments:
Post a Comment