-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue.c
81 lines (77 loc) · 1.23 KB
/
queue.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
//c programto perform queues in data structures
#include <stdio.h>
#include <stdlib.h>
int queue[5];
int front=-1;
int rear=-1;
void enqueue(){
int x;
printf("enter data ");
scanf("%d",&x);
if(rear==4){
printf("overflow ");
}
else if(front==-1 && rear==-1){
front=0;
rear=0;
queue[rear]=x;
}
else{
rear++;
queue[rear]=x;
}
}
void dequeue(){
if(front==-1 && rear==-1){
printf("underflow ");
}
else if(front==rear){
front=rear=-1;
}
else{
printf("dequeued element is %d",queue[front]);
front++;
}
}
void display(){
int i;
if(front==-1&&rear==-1){
printf("Nothing to display in the screen");
}
else{
for(i=front;i<=rear;i++){
printf("%d ",queue[i]);
}
}
}
void peek(){
if(front==-1&&rear==-1){
printf("empty queue ");
}
else
printf("%d ",queue[front]);
}
void main(){
while(1){
int opt;
printf("enter your options to choose\n");
printf("1:enqueue\n2:dequeue\n3:peek\n4:display\n5:exit\n");
scanf("%d",&opt);
switch(opt){
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
exit(0);
}
}
}