-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram7.cpp
More file actions
102 lines (84 loc) · 2.21 KB
/
Program7.cpp
File metadata and controls
102 lines (84 loc) · 2.21 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
// Program to create a queue and implement enqueue and dequeue operations on it.
#include <stdio.h>
#define MAX 10 // Maximum size of the queue
int queue[MAX];
int front = -1, rear = -1;
// Function to check if the queue is empty
int isEmpty() {
return (front == -1);
}
// Function to check if the queue is full
int isFull() {
return (rear == MAX - 1);
}
// Function to insert an element into the queue
void enqueue(int item) {
if (isFull()) {
printf("Queue Overflow\n");
} else {
if (isEmpty()) {
front = 0;
}
rear = rear + 1;
queue[rear] = item;
}
}
// Function to delete an element from the queue
int dequeue() {
int item;
if (isEmpty()) {
printf("Queue Underflow\n");
return -1; // Return an error value
} else {
item = queue[front];
if (front == rear) {
front = -1;
rear = -1;
} else {
front = front + 1;
}
return item;
}
}
int main() {
int choice, item;
printf("Name: Umesh Patel\n");
printf("Enrollment No: 0126AL231140\n");
while (1) {
printf("\n1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the element to be inserted: ");
scanf("%d", &item);
enqueue(item);
break;
case 2:
item = dequeue();
if (item != -1) {
printf("Dequeued element: %d\n", item);
}
break;
case 3:
if (isEmpty()) {
printf("Queue is empty\n");
} else {
printf("Queue elements:\n");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
break;
case 4:
return 0;
default:
printf("Invalid choice\n");
}
}
return 0;
}