-
Notifications
You must be signed in to change notification settings - Fork 0
/
circularqueue.c
85 lines (85 loc) · 1.78 KB
/
circularqueue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define Max 05
int q[Max];
int front=-1;
int rear=-1;
void push(int);
void pop();
void display();
void main()
{
int ch,val;
while(1)
{
printf("\npress 1 to push 2 to pop 3 to display and 4 to exit:");
printf("\n Enter the choice=");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the value:");
scanf("%d",&val);
push(val);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("Wrong choice:");
}
getch();
//return 0;
}
}
void push(int num)
{
if((rear+1)%Max==front)
printf("Queue is Full:");
else if(front==-1 && rear==-1)
{
front=rear=0;
q[rear]=num;
}
else{
rear=(rear+1)%Max;
q[rear]=num;
}
}
void pop()
{
if(front==-1 && rear==-1)
printf("\nQueue is empty:");
// int res=q[front];
if(front==rear)
{
printf("%d",q[front]);
front=rear=-1;
}
else{
printf("%d",q[front]);
front=(front+1)%Max;
}
}
void display()
{
int i=front;
if(front==-1 && rear==-1)
printf("\nQueue is empty:");
else
{
printf("Queue is:");
while(i!=rear){
printf("%d \t",q[i]);
i=(i+1)%Max;
}
printf("%d",q[rear]);
}
}