-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.c
67 lines (63 loc) · 1.44 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
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
void enqueue(int, int queue[], int *front, int *rear);
int dequeue(int queue[], int *rear, int *front);
void displayQueue(int queue[], int front, int rear);
int main()
{
int queue[MAX], value;
int front = -1, rear = -1;
printf("\nQueue Operations:\n");
enqueue(100, queue, &front, &rear);
enqueue(200, queue, &front, &rear);
enqueue(300, queue, &front, &rear);
displayQueue(queue, front, rear);
dequeue(queue, &front, &rear);
displayQueue(queue, front, rear);
return 0;
}
void enqueue(int value, int queue[MAX], int *front, int *rear)
{
if (*rear == MAX - 1)
{
printf("Queue overflow\n");
}
else
{
if (*front == -1)
*front = 0;
}
queue[++(*rear)] = value;
printf("%d enqueued to Queue\n", value);
}
int dequeue(int queue[], int *rear, int *front)
{
if (*front == -1 || front > rear)
{
printf("Queue underflow\n");
return -1;
}
else
{
int value = queue[*front++];
printf("%d dequeued from the Queue\n", value);
return value;
}
}
void displayQueue(int queue[], int front, int rear)
{
if (front == -1 || front > rear)
{
printf("\nQueue is Empty");
}
else
{
printf("\nQueue Elements: ");
for (int i = front; i <= rear; i++)
{
printf("%d ", queue[i]);
}
printf("\n");
}
}