-
Notifications
You must be signed in to change notification settings - Fork 0
/
circularqueue.c
99 lines (96 loc) · 2.25 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <stdio.h>
#define MAXSIZE 10
void enqueue(int queue[], int *front, int *rear, int value);
int dequeue(int queue[], int *front, int *rear);
void displayQueue(int queue[], int front, int rear);
int main()
{
int queue[MAXSIZE], front = -1, rear = -1, choice, value;
printf("\n1.Enqueue \n2.Dequeue \n3.DisplayQueue \n4.Exit \nEnter Your choice \n");
scanf("%d", &choice);
while (1)
{
switch (choice)
{
case 1:
printf("Enter the Value: ");
scanf("%d", &value);
enqueue(queue, &front, &rear, value);
break;
case 2:
value = dequeue(queue, &front, &rear);
if (value != -1)
{
printf("Dequeued value: %d\n", value);
}
break;
case 3:
displayQueue(queue, front, rear);
break;
case 4:
return 0;
break;
default:
printf("Invalid choice! please enter again\n");
}
}
return 0;
}
void enqueue(int queue[], int *front, int *rear, int value)
{
// check if the queue is full
if ((*rear + 1) % MAXSIZE == *front)
{
printf("The Queue is full\n");
return 0;
}
// check if this is the first element being added
if (*front == -1)
{
*front = 0;
}
// Move rear pointer and insert element
*rear = (*rear + 1) % MAXSIZE;
queue[*rear] = value;
printf("Enqueued : %d\n");
}
int dequeue(int queue[], int *front, int *rear)
{
// check if the queue is empty
if (*front == -1)
{
printf("The Queue is Empty\n");
return -1;
}
int value = (*front);
// if queue has only one element reset front and rear
if (*front == *rear)
{
*front == -1;
*rear == -1;
}
else
{
// Move front to the next position
*front = (*front + 1) % MAXSIZE;
}
return value;
}
void display(int queue[], int front, int rear)
{
if (front == -1)
{
printf("Queue is Empty\n");
return 0;
}
printf("Queue Elements: ");
int i = front;
while (1)
{
printf("%d", queue[i]);
if (i == rear)
break;
i = (i + 1) % MAXSIZE;
}
printf("\n");
}