-
Notifications
You must be signed in to change notification settings - Fork 1k
/
circularqueue.c
120 lines (105 loc) · 4.74 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//headers
#include <stdio.h>
#include <stdbool.h>
//defining the size of the queue we are taking here
#define size 5
//defining a structure
typedef struct
{
// creating the queue structure with data, front and rear nodes pointing to front and the last(behind) node
int data[5];
//a node containg the value of a pointer/address pointing to the front node
int front;
//a node containing the value of a pointer/address pointing to the back/behind/rear node
int rear;
} mainf;
//the name of the structure
mainf queuestructure;
//In the circular queue, in the last node, the front pointer points to the first node.
int enque(int number)
{
//adding a value to the queue
//if the queue is full user cant add anything more
if (queuestructure.front == queuestructure.rear + 1)
{
printf("Queue is full!\n");
}
else if (queuestructure.rear == size - 1)
{
queuestructure.rear = 0;
printf("Rear resetted to one.\n");
}
else
{
queuestructure.rear++;
//adding the value to the queue
queuestructure.data[queuestructure.rear] = number;
printf("%d -> Added! \n", queuestructure.data[queuestructure.rear]);
}
}
void dequeue()
//removing a specific node from the queue
{
//the element which is being removed
int out;
queuestructure.front++;
out = queuestructure.data[queuestructure.front];
printf("%d -> Removed! \n", out);
}
int main(void)
{
int user, value;
//setting the front pointer to null
queuestructure.front = -1;
//setting the rear pointer to null
queuestructure.rear = -1;
//this means that our queue is empty at the beginning
bool out = true;
printf("Enter 1 to enqueue queue.\n");
printf("Enter 2 to dequeue queue.\n");
printf("Enter 3 to exit.\n");
while (out = true)
{
scanf("%d", &user);
if (user == 1)
{
printf("Enter a value to enqueue : ");
scanf("%d", &value);
//entering an element at the back
enque(value);
out = true;
}
else if (user == 2)
{
//to remove the first element
dequeue();
out = true;
}
else
{
out = false;
return 0;
}
}
}
/*
Output:
Enter 1 to enqueue queue.
Enter 2 to dequeue queue.
Enter 3 to exit.
1
Enter a value to enqueue : 11
11 -> Added!
1
Enter a value to enqueue : 12
12 -> Added!
1
Enter a value to enqueue : 14
14 -> Added!
1
Enter a value to enqueue : 13
13 -> Added!
2
11 -> Removed!
3
*/