-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray_Queue.c
More file actions
167 lines (144 loc) · 4.18 KB
/
Array_Queue.c
File metadata and controls
167 lines (144 loc) · 4.18 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <stdio.h>
#define MAX 5 // Maximum number of elements in the queue
int queue[MAX]; // Array to hold the queue elements
int front = -1, rear = -1; // Initialize front and rear pointers
// Function to add an element to the queue (enqueue)
void enqueue(int value) {
if (rear == MAX - 1) {
printf("Queue is full! Cannot enqueue %d\n", value);
} else {
if (front == -1) {
front = 0; // First element in the queue
}
rear++;
queue[rear] = value;
printf("Enqueued %d into the queue\n", value);
}
}
// Function to remove an element from the queue (dequeue)
int dequeue() {
if (front == -1 || front > rear) {
printf("Queue is empty! Nothing to dequeue.\n");
return -1;
} else {
int dequeuedValue = queue[front];
front++;
printf("Dequeued %d from the queue\n", dequeuedValue);
return dequeuedValue;
}
}
// Function to view the element at the front of the queue
int peek() {
if (front == -1 || front > rear) {
printf("Queue is empty! No element at the front.\n");
return -1;
} else {
return queue[front];
}
}
// Function to display the elements of the queue
void displayQueue() {
if (front == -1 || front > rear) {
printf("Queue is empty!\n");
} else {
printf("Queue: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
}
int main() {
int choice, value;
while (1) {
printf("\nChoose an option:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Show Front (Peek)\n");
printf("4. Display Queue\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
value = peek();
if (value != -1) {
printf("Front element is: %d\n", value);
}
break;
case 4:
displayQueue();
break;
case 5:
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice! Please try again.\n");
}
}
}
/**
Explanation of Functions:
enqueue(int value): Adds an element to the queue if it’s not full. If the queue is empty (i.e., front == -1), it initializes the front to 0.
dequeue(): Removes the front element from the queue and shifts the front pointer. If the queue is empty, it prints an error message.
peek(): Displays the element at the front of the queue without removing it.
displayQueue(): Displays all the elements in the queue from front to rear.
Main Loop: Continuously prompts the user to choose between enqueueing, dequeueing, checking the front element, displaying the queue, or exiting.
Example Output:
Choose an option:
1. Enqueue
2. Dequeue
3. Show Front (Peek)
4. Display Queue
5. Exit
Enter your choice: 1
Enter value to enqueue: 10
Enqueued 10 into the queue
Choose an option:
1. Enqueue
2. Dequeue
3. Show Front (Peek)
4. Display Queue
5. Exit
Enter your choice: 1
Enter value to enqueue: 20
Enqueued 20 into the queue
Choose an option:
1. Enqueue
2. Dequeue
3. Show Front (Peek)
4. Display Queue
5. Exit
Enter your choice: 4
Queue: 10 20
Choose an option:
1. Enqueue
2. Dequeue
3. Show Front (Peek)
4. Display Queue
5. Exit
Enter your choice: 2
Dequeued 10 from the queue
Choose an option:
1. Enqueue
2. Dequeue
3. Show Front (Peek)
4. Display Queue
5. Exit
Enter your choice: 4
Queue: 20
Real-World Analogy:
Imagine a line of people waiting to buy tickets at a counter.
The first person in line (front of the queue) will be served first.
When new people arrive, they go to the end of the line (rear of the queue).
As people are served (dequeued), the front of the line moves forward.
The process of adding and removing people from the queue follows this FIFO (First In, First Out) rule.
**/