-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue Example.c
61 lines (46 loc) · 1.11 KB
/
Queue Example.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
#include <stdio.h>
#define MAX_SIZE 100
int queue[MAX_SIZE];
int front = -1;
int rear = -1;
int isEmpty() {
return front == -1 && rear == -1;
}
int isFull() {
return rear == MAX_SIZE - 1;
}
void enqueue(int value) {
if (isEmpty()) {
front = 0;
}
if (isFull()) {
printf("Queue is full. Cannot insert %d.\n", value);
return;
}
rear++;
queue[rear] = value;
printf("Inserted %d into the queue.\n", value);
}
void displayQueue() {
if (isEmpty()) {
printf("Queue is empty.\n");
return;
}
printf("Queue elements: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
int main() {
int numInsertions, value;
printf("Enter how many times you want to insert data into the queue: ");
scanf("%d", &numInsertions);
for (int i = 0; i < numInsertions; i++) {
printf("Enter a value to insert into the queue: ");
scanf("%d", &value);
enqueue(value);
}
displayQueue();
return 0;
}