How to write Program from insert, delete using
circular queue
#include<stdio.h>
#include<conio.h>
#define max 5
typedef struct queue
{
int front,rear;
int item[max];
}queue;
queue q1;
void insert()
{
int n;
if((q1.front==0 && q1.rear==max-1) || (q1.front==q1.rear+1))
printf("\nQueue is full");
else
{
if (q1.front==-1)
q1.rear=q1.front=0;
else
if(q1.rear==max-1)
q1.rear=0;
else
q1.rear++;
printf("\nenter the element for insertion in queue:- ");
scanf("%d", &n);
q1.item[q1.rear]=n;
}
}
void del()
{
if (q1.front==-1)
printf("\nQueue is empty");
else
{
printf("\n%d is deleted from queue:- ",q1.item[q1.front]);
if(q1.front==q1.rear)
q1.front=q1.rear=-1;
else
if(q1.front==max-1)
q1.front=0;
else
q1.front++;
}
}
void display()
{
int front_temp=q1.front,rear_temp=q1.rear;
if(q1.front==-1)
printf("\nQueue is empty");
else
{
printf("\nelement of the queue are:-\n");
if(front_temp<=rear_temp)
{
while(front_temp<=rear_temp)
printf("%d\t",q1.item[front_temp++]);
}
else
{
while(front_temp<=max-1)
printf("%d\t",q1.item[front_temp++]);
front_temp=0;
while(front_temp<=rear_temp)
printf("%d\t",q1.item[front_temp++]);
}
}
}
void main()
{
int ch;
clrscr();
q1.front=q1.rear=-1;
while(1)
{
printf("\n\n1:for insertion");
printf("\n2:for deletion");
printf("\n3:for exit");
printf("\nEnter your choice:- ");
scanf("%d",&ch);
switch(ch)
{
case 1 :
insert();
display();
break;
case 2 :
del();
display();
break;
case 3:
exit(0);
default:
printf("\nyou entered wrong choice");
}
}
}
/*
Output:-
1:for insertion
2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 1
1
1:for insertion
2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 2
element of the queue are:-
1 2
1:for insertion
2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 3
element of the queue are:-
1 2 3
1:for insertion
2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 4
element of the queue are:-
1 2 3 4
1:for insertion
2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 5
element of the queue are:-
1 2 3 4 5
1:for insertion
2:for deletion
3:for exit
Enter your choice:- 1
Queue is full
element of the queue are:-
1 2 3 4 5
1:for insertion
2:for deletion
3:for exit
Enter your choice:- 2
1 is deleted from queue:-
element of the queue are:-
2 3 4 5
1:for insertion
2:for deletion
3:for exit
Enter your choice:- 2
2 is deleted from queue:-
element of the queue are:-
3 4 5
1:for insertion
2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 1
element of the queue are:-
3 4 5 1
1:for insertion
2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 2
element of the queue are:-
3 4 5 1 2
1:for insertion
2:for deletion
3:for exit
Enter your choice:- 3
*/
circular queue
#include<stdio.h>
#include<conio.h>
#define max 5
typedef struct queue
{
int front,rear;
int item[max];
}queue;
queue q1;
void insert()
{
int n;
if((q1.front==0 && q1.rear==max-1) || (q1.front==q1.rear+1))
printf("\nQueue is full");
else
{
if (q1.front==-1)
q1.rear=q1.front=0;
else
if(q1.rear==max-1)
q1.rear=0;
else
q1.rear++;
printf("\nenter the element for insertion in queue:- ");
scanf("%d", &n);
q1.item[q1.rear]=n;
}
}
void del()
{
if (q1.front==-1)
printf("\nQueue is empty");
else
{
printf("\n%d is deleted from queue:- ",q1.item[q1.front]);
if(q1.front==q1.rear)
q1.front=q1.rear=-1;
else
if(q1.front==max-1)
q1.front=0;
else
q1.front++;
}
}
void display()
{
int front_temp=q1.front,rear_temp=q1.rear;
if(q1.front==-1)
printf("\nQueue is empty");
else
{
printf("\nelement of the queue are:-\n");
if(front_temp<=rear_temp)
{
while(front_temp<=rear_temp)
printf("%d\t",q1.item[front_temp++]);
}
else
{
while(front_temp<=max-1)
printf("%d\t",q1.item[front_temp++]);
front_temp=0;
while(front_temp<=rear_temp)
printf("%d\t",q1.item[front_temp++]);
}
}
}
void main()
{
int ch;
clrscr();
q1.front=q1.rear=-1;
while(1)
{
printf("\n\n1:for insertion");
printf("\n2:for deletion");
printf("\n3:for exit");
printf("\nEnter your choice:- ");
scanf("%d",&ch);
switch(ch)
{
case 1 :
insert();
display();
break;
case 2 :
del();
display();
break;
case 3:
exit(0);
default:
printf("\nyou entered wrong choice");
}
}
}
/*
Output:-
1:for insertion
2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 1
1
1:for insertion
2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 2
element of the queue are:-
1 2
1:for insertion
2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 3
element of the queue are:-
1 2 3
1:for insertion
2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 4
element of the queue are:-
1 2 3 4
1:for insertion
2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 5
element of the queue are:-
1 2 3 4 5
1:for insertion
2:for deletion
3:for exit
Enter your choice:- 1
Queue is full
element of the queue are:-
1 2 3 4 5
1:for insertion
2:for deletion
3:for exit
Enter your choice:- 2
1 is deleted from queue:-
element of the queue are:-
2 3 4 5
1:for insertion
2:for deletion
3:for exit
Enter your choice:- 2
2 is deleted from queue:-
element of the queue are:-
3 4 5
1:for insertion
2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 1
element of the queue are:-
3 4 5 1
1:for insertion
2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 2
element of the queue are:-
3 4 5 1 2
1:for insertion
2:for deletion
3:for exit
Enter your choice:- 3
*/
0 comments: