-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimple_queue.c
More file actions
70 lines (64 loc) · 1.49 KB
/
simple_queue.c
File metadata and controls
70 lines (64 loc) · 1.49 KB
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
#include<stdio.h>
#define N 5
int front = -1,rear= -1;
void enqueue(int q[],int val){
if(rear == N-1){
printf("queue is full so %d is not inserted\n",val);
}
else{
q[++rear] = val;
if(front == -1)
front=0;
}
}
void dequeue(int q[]){
if(front == -1){
printf("Queue is empty\n");
}
else if(front == rear){
printf("%d is removed\n",q[front]);
front = rear = -1;
}
else{
printf("%d is removed\n",q[front]);
front++;
}
}
void display(int q[]){
int i=front;
if(i==-1){
printf("The queue is empty\n");
}
else{
for(i;i<=rear;i++)
printf("%d ",q[i]);
}
}
int main(){
int q[N],val,n;
printf("Enter 1 for enqueue:\n");
printf("Enter 2 for dequeue:\n");
printf("Enter 3 for display:\n");
printf("Enter 4 for EXIT\n");
scanf("%d",&n);
while(n!=4){
switch(n){
case 1:
printf("Enter the value you want to be inserted\n");
scanf("%d",&val);
enqueue(q,val);
break;
case 2:
dequeue(q);
break;
case 3:
display(q);
break;
default:
printf("Enter value between 1 to 3\n");
}
printf("Enter 1/2/3 as given above\n");
scanf("%d",&n);
}
return 0;
}