Circular
Circular
h>
#include <conio.h>
#include <malloc.h>
struct node
{
int val;
struct node*next;
};
struct node*head=NULL;
struct node*tail=NULL;
struct node*new=NULL;
void create();
void insertbeg();
void insertend();
void insertsp();
void deletebeg();
void deleteend();
void deletesp();
void display();
void main()
{
int ch;
printf("\nMEIT ADKAR\n");
do{
printf("\n 1.Create\n 2.Insert at beginning\n 3.Insert at End\n
4.Insert at specific position\n 5.Delete at beginning\n 6.Delete at end\n 7.Delete
at specific position\n 8.Display\n 9.End\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch (ch)
{
case 1: create();
break;
case 2: insertbeg();
break;
case 3: insertend();
break;
case 4: insertsp();
break;
case 5: deletebeg();
break;
case 6: deleteend();
break;
case 7: deletesp();
break;
case 8: display();
break;
case 9: printf("Terminated\n");
break;
default: printf("Invalid choice\n");
}
}while(ch!=9);
getch();
}
void create()
{
int ch;
do{
printf("\n1.Create\n2.Stop\n");
printf("Enter your choice: ");
scanf("%d",&ch);
if (ch==1)
{
new=(struct node*)malloc(sizeof(struct node));
printf("Enter value: ");
scanf("%d",&(new->val));
new->next=head;
if (head==NULL)
{
head=new;
tail=new;
new->next=head;
}
else
{
tail->next=new;
tail=new;
}
}
}while(ch!=2);
}
void insertbeg()
{
new=(struct node*)malloc(sizeof(struct node));
printf("Enter value: ");
scanf("%d",&(new->val));
new->next=head;
if (head==NULL)
{
head=new;
tail=new;
}
else
{
head=new;
tail->next=head;
}
}
void insertend()
{
new=(struct node*)malloc(sizeof(struct node));
printf("Enter value: ");
scanf("%d",&(new->val));
new->next=head;
if (head==NULL)
{
head=new;
tail=new;
}
else
{
tail->next=new;
tail=new;
}
}
void insertsp()
{
if (head==NULL)
{
printf("No list available\n");
}
else
{
int pos,i,count;
count=1;
struct node*temp=head;
while((temp->next)!=head)
{
count++;
temp=temp->next;
}
printf("Enter position: ");
scanf("%d",&pos);
if (pos==1)
{
insertbeg();
}
else if(pos==(count+1))
{
insertend();
}
else if(pos>(count+1))
{
printf("Invalid position\n");
}
else
{
new=(struct node*)malloc(sizeof(struct node));
printf("Enter value: ");
scanf("%d",&(new->val));
new->next=head;
struct node*temp=head;
struct node*temp2=head;
for (i=1 ; i<=pos-1 ; i++)
{
temp2=temp;
temp=temp->next;
}
temp->next=new;
new->next=temp;
}
}
}
void deletebeg()
{
if (head==NULL)
{
printf("No list available\n");
}
else if (head==tail)
{
free (head);
head=NULL;
tail=NULL;
}
else
{
struct node*temp;
temp=head;
head=head->next;
free (temp);
}
}
void deleteend()
{
if (head==NULL)
{
printf("No list available\n");
}
else if (head==tail)
{
free (head);
head=NULL;
tail=NULL;
}
else
{
struct node*temp;
while((temp->next)!=tail)
{
temp=temp->next;
}
free (tail);
tail=temp;
tail->next=head;
}
}
void deletesp()
{
if (head==NULL)
{
printf("No list available\n");
}
else
{
int pos,i,count;
count=1;
struct node*temp=head;
while((temp->next)!=head)
{
count++;
temp=temp->next;
}
printf("Enter position: ");
scanf("%d",&pos);
if (pos==1)
{
deletebeg();
}
else if (pos==count)
{
deleteend();
}
else if (pos>count)
{
printf("Invalid choice\n");
}
else
{
struct node*temp=head;
struct node*temp2=head;
for (i=1 ; i<=pos-1 ; i++)
{
temp2=temp;
temp=temp->next;
}
temp2->next=temp->next;
free(temp);
}
}
}
void display()
{
if (head==NULL)
{
printf("No list available\n");
}
else
{
struct node*temp;
temp=head;
while(temp!=head)
{
printf("%d\t",(temp->val));
temp=temp->next;
}
}
}
/*******OUTPUT*******/
MEIT ADKAR
1.Create
2.Insert at beginning
3.Insert at end
4.Insert at specific position
5.Delete at beginning
6.Delete at end
7.Delete at specific position
8.Display
9.End
Enter your choice: 1
1.Create
2.Stop
Enter choice: 1
Enter value: 45
1.Create
2.Insert at beginning
3.Insert at end
4.Insert at specific position
5.Delete at beginning
6.Delete at end
7.Delete at specific position
8.Display
9.End
Enter your choice: 1
1.Create
2.Stop
Enter choice: 1
Enter value: 18
1.Create
2.Insert at beginning
3.Insert at end
4.Insert at specific position
5.Delete at beginning
6.Delete at end
7.Delete at specific position
8.Display
9.End
Enter your choice: 1
1.Create
2.Stop
Enter choice: 2
1.Create
2.Insert at beginning
3.Insert at end
4.Insert at specific position
5.Delete at beginning
6.Delete at end
7.Delete at specific position
8.Display
9.End
Enter your choice: 2
Enter value: 10
1.Create
2.Insert at beginning
3.Insert at end
4.Insert at specific position
5.Delete at beginning
6.Delete at end
7.Delete at specific position
8.Display
9.End
Enter your choice: 8
10 45 18
1.Create
2.Insert at beginning
3.Insert at end
4.Insert at specific position
5.Delete at beginning
6.Delete at end
7.Delete at specific position
8.Display
9.End
Enter your choice: 5
1.Create
2.Insert at beginning
3.Insert at end
4.Insert at specific position
5.Delete at beginning
6.Delete at end
7.Delete at specific position
8.Display
9.End
Enter your choice: 8
45 18
1.Create
2.Insert at beginning
3.Insert at end
4.Insert at specific position
5.Delete at beginning
6.Delete at end
7.Delete at specific position
8.Display
9.End
Enter your choice: 6
1.Create
2.Insert at beginning
3.Insert at end
4.Insert at specific position
5.Delete at beginning
6.Delete at end
7.Delete at specific position
8.Display
9.End
Enter your choice: 8
45