-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
123 lines (100 loc) · 1.72 KB
/
queue.c
File metadata and controls
123 lines (100 loc) · 1.72 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <stdio.h>
#include <stdlib.h>
#define sint(x) scanf("%d", &x)
#define MAX 10
int queue_arr[MAX], front = -1, rear = -1;
// using array
int isEmpty();
int isFull();
void enqueue(int value);
void dequeue();
int peek();
void display();
int main()
{
int value;
while (1)
{
int sc;
printf("enter 1 -> enqueue\n");
printf("enter 2 -> dequeue\n");
printf("enter 3 -> peek\n");
printf("enter 4 -> display\n");
sint(sc);
switch (sc)
{
case 1:
sint(value), enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
printf("%d\n", peek());
break;
case 4:
display();
break;
default:
exit(1);
}
}
return 0;
}
int isEmpty()
{
if (front == -1) // initial stage
return 1;
else if (front == rear + 1)
return 1;
else
return 0;
}
int isFull()
{
if (rear == MAX - 1)
return 1;
else
return 0;
}
void enqueue(int value)
{
if (isFull())
printf("QUEUE OVERFLOW\n");
else
{
if (front == -1) // first element
front = 0;
queue_arr[++rear] = value;
}
}
void dequeue()
{
if (isEmpty())
printf("QUEUE UNDERFLOW\n");
else
{
front++;
}
}
int peek()
{
if (isEmpty())
printf("queue is empty\n");
else
{
return queue_arr[front];
}
return -1;
}
void display()
{
printf("queue contents are : ");
if (front == -1)
return;
for (int i = front; i <= rear; i++)
{
printf("%d ", queue_arr[i]);
}
printf("\n");
}