-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProg8.c
125 lines (113 loc) · 2.88 KB
/
Prog8.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
121
122
123
124
125
/* ---------------PROBLEM STATEMENT ---------------
Implement operations (enqueue, dequeue) on a queue using arrays.
Check the status of the queue whether it is empty or full.
*/
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
int queue[SIZE];
int front = -1;
int rear = -1;
int isFull() {
if (front == 0 && rear == SIZE - 1) return 1;
return 0;
}
int isEmpty(){
if(front == -1 && rear == -1)
return 1;
else
return 0;
}
void display(){
if(isEmpty()){
printf("Queue is empty.\n");
}
else{
for(int i = front; i <= rear; i++){
printf("%d ", queue[i]);
}
}
}
void Enqueue(){
int x;
printf("Enter the data : ");
scanf("%d",&x);
if(isEmpty()){
front++;
}
else if(rear == SIZE-1){
printf("\nOVERFLOW!!\n");
return;
}
rear++;
queue[rear] = x;
}
void Dequeue(){
if(front == -1){
printf("\nUNDERFLOW!!\n");
return;
}
if(front == rear){
printf("Dequeued Element : %d\n", queue[front]);
front = -1;
rear = -1;
}
else{
printf("Dequeued Element : %d\n", queue[front]);
front++;
}
}
int main(){
int choice;
char c;
while(1){
printf("\n************* MAIN MENU ****************\n");
printf("\nPerform operations on the Queue:");
printf("\n1. Enqueue the element\n2. Dequeue the element\n3. Check if Queue is empty\n4. Check if Queue is Full\n5. Show all elements of Queue\n6. Exit");
printf("\n\nEnter the choice: ");
scanf("%d", &choice);
switch(choice){
case 1:
Enqueue();
printf("\nQueue after Enqueue : ");
display();
break;
case 2:
Dequeue();
printf("\nQueue after Dequeue : ");
display();
break;
case 3:
if(isEmpty()){
printf("Queue is empty\n");
}
else{
printf("Queue is not empty\n");
}
break;
case 4:
if(isFull()){
printf("Queue is Full\n");
}
else{
printf("Queue is not Full\n");
}
break;
case 5:
printf("Elements in Queue : ");
display();
break;
case 6:
printf("\nAre you sure you want to exit program? (y/n) : ");
scanf(" %c", &c);
if (c == 'y')
exit(0);
else if (c != 'n')
printf("\nInvalid Input!\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}