PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
typedef struct node
{
int data;
struct node *next;
}node;
node *head=NULL,*newn,*temp,*t;
void insert();
void del();
void display();
void main()
{
int ch;
clrscr();
printf("\n\n$$$ CIRCULARLY LINKED LIST $$$\n\n");
while(1)
{
printf("\n\nCIRCULARLY LINKED LIST OPERATIONS ARE\n\n");
printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter Ur Choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:insert();break;
case 2:del();break;
case 3:display();break;
case 4:exit(0);
default:printf("Enter the correct option\n");
break;
}
}
}
void insert()
{
int no,op;
newn=(node *)malloc(sizeof(node));
printf("Enter the element to be inserted:\t");
scanf("%d",&newn->data);
printf("1.Insert at First\n2.Insert at Middle\n3.Insert at Last\n");
printf("Enter the option:\t");
scanf("%d",&op);
switch(op)
{
case 1:
if(head==NULL)
{
head=newn;
newn->next=newn;
}
else
{
temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=newn;
newn->next=head;
head=newn;
}
break;
case 2:
if(head->next==NULL)
{
printf("Single node entry so insertion is not allowed in the middle\n");
}
else
{
printf("Enter the data after which the element to be inserted:\t");
scanf("%d",&no);
temp=head;
while(temp->data!=no)
{
temp=temp->next;
}
newn->next=temp->next;
temp->next=newn;
}
break;
case 3:
if(head==NULL)
{
head=newn;
newn->next=newn;
}
else
{
temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=newn;
newn->next=head;
}
break;
}
}
void del()
{
int op,no;
if(head==NULL)
{
printf("List is empty\n");
}
else
{
printf("1.Deletion at first\n2.Deletion at middle\n3.Deletion at last\n");
printf("Enter your choice:\t");
scanf("%d",&op);
switch(op)
{
case 1:
if(head->next==head)
{
printf("The deleted element is: %d",head->data);
head=NULL;
}
else
{
temp=head;
printf("The deleted element is: %d",temp->data);
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=head->next;
head=head->next;
free(temp);
}
break;
case 2:
if(head->next==head)
{
printf("Single node entry cannot perform deletion in the middle\n");
}
else
{
printf("Enter the element to be deleted:\t");
scanf("%d",&no);
temp=head;
while(temp->data!=no)
{
t=temp;
temp=temp->next;
}
printf("The deleted element is: %d",temp->data);
t->next=temp->next;
free(temp);
}
break;
case 3:
if(head->next==head)
{
printf("The deleted element is: %d",head->data);
head=NULL;
}
else
{
temp=head;
while(temp->next!=head)
{
t=temp;
temp=temp->next;
}
printf("The deleted element is: %d",temp->data);
t->next=head;
free(temp);
}
break;
}
}
}
void display()
{
if(head==NULL)
{
printf("List is empty\n");
}
else
{
temp=head;
printf("The elements in a list are:\n");
do
{
printf("%d\n",temp->data);
temp=temp->next;
}while(temp!=head);
}
}
OUTPUT:
$$$ CIRCULARLY LINKED LIST $$$
CIRCULARLY LINKED LIST OPERATIONS ARE
1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice: 1
Enter the element to be inserted: 11
1.Insert at First
2.Insert at Middle
3.Insert at Last
Enter the option: 1
CIRCULARLY LINKED LIST OPERATIONS ARE
1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice: 1
Enter the element to be inserted: 33
1.Insert at First
2.Insert at Middle
3.Insert at Last
Enter the option: 3
CIRCULARLY LINKED LIST OPERATIONS ARE
1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice: 1
Enter the element to be inserted: 22
1.Insert at First
2.Insert at Middle
3.Insert at Last
Enter the option: 2
Enter the data after which the element to be inserted: 11
CIRCULARLY LINKED LIST OPERATIONS ARE
1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice: 3
The elements in a list are:
11
22
33
CIRCULARLY LINKED LIST OPERATIONS ARE
1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice: 2
1.Deletion at first
2.Deletion at middle
3.Deletion at last
Enter your choice: 2
Enter the element to be deleted: 22
The deleted element is: 22
CIRCULARLY LINKED LIST OPERATIONS ARE
1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice: 3
The elements in a list are:
11
33
CIRCULARLY LINKED LIST OPERATIONS ARE
1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice: 4
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
typedef struct node
{
int data;
struct node *next;
}node;
node *head=NULL,*newn,*temp,*t;
void insert();
void del();
void display();
void main()
{
int ch;
clrscr();
printf("\n\n$$$ CIRCULARLY LINKED LIST $$$\n\n");
while(1)
{
printf("\n\nCIRCULARLY LINKED LIST OPERATIONS ARE\n\n");
printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter Ur Choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:insert();break;
case 2:del();break;
case 3:display();break;
case 4:exit(0);
default:printf("Enter the correct option\n");
break;
}
}
}
void insert()
{
int no,op;
newn=(node *)malloc(sizeof(node));
printf("Enter the element to be inserted:\t");
scanf("%d",&newn->data);
printf("1.Insert at First\n2.Insert at Middle\n3.Insert at Last\n");
printf("Enter the option:\t");
scanf("%d",&op);
switch(op)
{
case 1:
if(head==NULL)
{
head=newn;
newn->next=newn;
}
else
{
temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=newn;
newn->next=head;
head=newn;
}
break;
case 2:
if(head->next==NULL)
{
printf("Single node entry so insertion is not allowed in the middle\n");
}
else
{
printf("Enter the data after which the element to be inserted:\t");
scanf("%d",&no);
temp=head;
while(temp->data!=no)
{
temp=temp->next;
}
newn->next=temp->next;
temp->next=newn;
}
break;
case 3:
if(head==NULL)
{
head=newn;
newn->next=newn;
}
else
{
temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=newn;
newn->next=head;
}
break;
}
}
void del()
{
int op,no;
if(head==NULL)
{
printf("List is empty\n");
}
else
{
printf("1.Deletion at first\n2.Deletion at middle\n3.Deletion at last\n");
printf("Enter your choice:\t");
scanf("%d",&op);
switch(op)
{
case 1:
if(head->next==head)
{
printf("The deleted element is: %d",head->data);
head=NULL;
}
else
{
temp=head;
printf("The deleted element is: %d",temp->data);
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=head->next;
head=head->next;
free(temp);
}
break;
case 2:
if(head->next==head)
{
printf("Single node entry cannot perform deletion in the middle\n");
}
else
{
printf("Enter the element to be deleted:\t");
scanf("%d",&no);
temp=head;
while(temp->data!=no)
{
t=temp;
temp=temp->next;
}
printf("The deleted element is: %d",temp->data);
t->next=temp->next;
free(temp);
}
break;
case 3:
if(head->next==head)
{
printf("The deleted element is: %d",head->data);
head=NULL;
}
else
{
temp=head;
while(temp->next!=head)
{
t=temp;
temp=temp->next;
}
printf("The deleted element is: %d",temp->data);
t->next=head;
free(temp);
}
break;
}
}
}
void display()
{
if(head==NULL)
{
printf("List is empty\n");
}
else
{
temp=head;
printf("The elements in a list are:\n");
do
{
printf("%d\n",temp->data);
temp=temp->next;
}while(temp!=head);
}
}
OUTPUT:
$$$ CIRCULARLY LINKED LIST $$$
CIRCULARLY LINKED LIST OPERATIONS ARE
1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice: 1
Enter the element to be inserted: 11
1.Insert at First
2.Insert at Middle
3.Insert at Last
Enter the option: 1
CIRCULARLY LINKED LIST OPERATIONS ARE
1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice: 1
Enter the element to be inserted: 33
1.Insert at First
2.Insert at Middle
3.Insert at Last
Enter the option: 3
CIRCULARLY LINKED LIST OPERATIONS ARE
1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice: 1
Enter the element to be inserted: 22
1.Insert at First
2.Insert at Middle
3.Insert at Last
Enter the option: 2
Enter the data after which the element to be inserted: 11
CIRCULARLY LINKED LIST OPERATIONS ARE
1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice: 3
The elements in a list are:
11
22
33
CIRCULARLY LINKED LIST OPERATIONS ARE
1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice: 2
1.Deletion at first
2.Deletion at middle
3.Deletion at last
Enter your choice: 2
Enter the element to be deleted: 22
The deleted element is: 22
CIRCULARLY LINKED LIST OPERATIONS ARE
1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice: 3
The elements in a list are:
11
33
CIRCULARLY LINKED LIST OPERATIONS ARE
1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice: 4
No comments:
Post a Comment