-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram3.c
More file actions
114 lines (95 loc) · 2.59 KB
/
program3.c
File metadata and controls
114 lines (95 loc) · 2.59 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
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
// Structure to represent the queue
struct Queue {
int arr[MAX];
int front, rear;
};
// Function to initialize the queue
void initialize(struct Queue *q) {
q->front = -1;
q->rear = -1;
}
// Function to check if the queue is empty
int isEmpty(struct Queue *q) {
return q->front == -1;
}
// Function to check if the queue is full
int isFull(struct Queue *q) {
return (q->rear + 1) % MAX == q->front;
}
// Function to insert an element into the queue
void insert(struct Queue *q, int value) {
if (isFull(q)) {
printf("Queue Overflow! Cannot insert element %d\n", value);
} else {
if (isEmpty(q)) {
q->front = 0;
}
q->rear = (q->rear + 1) % MAX;
q->arr[q->rear] = value;
printf("Element %d inserted into the queue\n", value);
}
}
// Function to delete an element from the queue
void delete(struct Queue *q) {
if (isEmpty(q)) {
printf("Queue Underflow! Cannot delete from an empty queue\n");
} else {
printf("Element %d deleted from the queue\n", q->arr[q->front]);
if (q->front == q->rear) {
// If the queue becomes empty after deletion
initialize(q);
} else {
q->front = (q->front + 1) % MAX;
}
}
}
// Function to display the status of the queue
void display(struct Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty\n");
} else {
printf("Queue status: ");
int i = q->front;
do {
printf("%d ", q->arr[i]);
i = (i + 1) % MAX;
} while (i != (q->rear + 1) % MAX);
printf("\n");
}
}
int main() {
struct Queue queue;
initialize(&queue);
int choice, element;
do {
printf("\nMenu:\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display Queue\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter element to insert: ");
scanf("%d", &element);
insert(&queue, element);
break;
case 2:
delete(&queue);
break;
case 3:
display(&queue);
break;
case 4:
printf("Exiting program\n");
exit(0);
default:
printf("Invalid choice! Please enter a valid option.\n");
}
} while (1); // Infinite loop for the menu
return 0;
}