forked from sudhanshu-p/dsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp4.c
266 lines (245 loc) · 6.2 KB
/
p4.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
//Stack Operations:
int isEmptyStack(int* top);
int isFullStack(int* top);
void push(int *top, int *arr, int val) ;
int pop(int *top, int* arr);
int peek(int* top, int* arr);
void displayStack(int* top, int* arr);
//Queue Operations:
int isEmptyQueue(int* front, int* rear);
int isFullQueue(int* rear);
void enqueue(int* front, int* rear, int *arr, int val);
void dequeue(int* front, int* rear);
void displayQueue(int* front, int* rear, int* arr);
//Circular Queue Operations:
int isEmptyCirc(int* front, int* rear);
int isFullCirc(int* front, int* rear);
void makeCircle(int* front, int* rear);
void enqueueCirc(int* front, int* rear, int* arr, int val);
void dequeueCirc(int* front, int* rear);
void displayQueueCirc(int* front, int* rear, int* arr);
int main() {
int sc, oc, temp, top = -1, front = 0, rear = 0;
int* arr;
arr = (int*) calloc(MAX, sizeof(int));
while(1) {
front = 0;
rear = 0;
printf("Enter the type of data structure you want to implement: \n1: Stack\n2: Queue\n3: Circular Queue\n4: Exit\nYour Choice: ");
scanf("%d", &sc);
switch(sc) {
case 1:
//Code for Implementing Stack
while(1) {
printf("\n----------------\nEnter which functionality you'd like to use: \n1: Push\n2: Pop\n3: Check Top Value\n4: Print the entire Stack\n-1: go back to the data structure selection\nYour Choice: ");
scanf("%d", &oc);
if(oc == -1) {
printf("Returning to ds selection.\n");
break;
}
switch(oc) {
case 1:
//Code for stack push
printf("Enter the value to be pushed: ");
scanf("%d", &temp);
push(&top, arr, temp);
printf("Pushed successful\n");
break;
case 2:
//Code for stack pop
temp = pop(&top, arr);
if(temp == -1)
printf("Stack is Empty");
else
printf("Element popped: %d", temp);
// top--;
break;
case 3:
//Code for printing top value
temp = peek(&top, arr);
if(temp == -1)
printf("Stack is Empty");
else
printf("Top of the stack is: %d\n", temp);
break;
case 4:
//Code for printing stack
displayStack(&top, arr);
break;
default:
printf("Invalid Choice. Try again\n");
}
}
break;
case 2:
//Code for Implementing Queue
while(1) {
printf("\n----------------\nEnter which functionality you'd like to use: \n1: Enqueue\n2: Dequeue\n3: Display Queue\n-1: go back to the data structure selection\nYour Choice: ");
scanf("%d", &oc);
if(oc == -1) {
printf("Returning to ds selection.\n");
break;
}
switch(oc) {
case 1:
printf("Enter the value to be enqueued: ");
scanf("%d", &temp);
enqueue(&front, &rear, arr, temp);
// rear++;
printf("Enqueued successful\n");
break;
case 2:
dequeue(&front, &rear);
// front++;
break;
case 3:
displayQueue(&front, &rear, arr);
break;
default:
printf("Invalid Choice. Try again\n");
}
}
break;
case 3:
//Code for Implementing Circular Queue
while(1) {
printf("\n----------------\nEnter which functionality you'd like to use: \n1: Enqueue\n2: Dequeue\n3: Print the entire Circular Queue\n-1: go back to the data structure selection\nYour Choice: ");
scanf("%d", &oc);
if(oc == -1) {
printf("Returning to ds selection.\n");
break;
}
switch(oc) {
case 1:
printf("Enter the value to be enqueued: ");
scanf("%d", &temp);
enqueueCirc(&front, &rear, arr, temp);
break;
case 2:
dequeueCirc(&front, &rear);
break;
case 3:
displayQueueCirc(&front, &rear, arr);
break;
default:
printf("Invalid Choice. Try again\n");
}
}
break;
case 4: exit(0);
default:
printf("Invalid Choice. plese enter a valid choice.\n");
}
}
}
int isEmptyStack(int* top) {
if(*top == 0) return 1;
return 0;
}
int isFullStack(int* top) {
if(*top == MAX - 1) return 1;
return 0;
}
void push(int* top, int* arr, int val) {
if(isFullStack(top)) {
printf("Stack is full\n");
return;
}
*top += 1;
arr[*top] = val;
}
int pop(int* top, int* arr) {
if(isEmptyStack(top)) return -1;
int temp = arr[*top];
*top -= 1;
return temp;
}
int peek(int* top, int* arr) {
if(isEmptyStack(top)) return -1;
return arr[*top];
}
void displayStack(int *top, int* arr) {
if(isEmptyStack(top)) {
printf("Stack is empty\n");
return;
}
printf("Stack is: ");
for(int i = *top; i >= 0; i--)
printf("%d ", arr[i]);
}
int isEmptyQueue(int* front, int* rear) {
// printf("%d and %d\n", *front, *rear);
if(*front == *rear) return 1;
return 0;
}
int isFullQueue(int* rear) {
if(*rear == MAX-1) return 1;
return 0;
}
void enqueue(int* front, int* rear, int*arr, int val) {
if(isFullQueue(rear)) {
printf("Queue is full!\n");
return;
}
// printf("%d\n", *rear);
arr[*rear] = val;
*rear += 1;
// printf("%d\n", *rear);
}
void dequeue(int* front, int* rear) {
if(isEmptyQueue(front, rear)) {
printf("Queue is empty!\n");
return;
}
*front += 1;
}
void displayQueue(int* front, int* rear, int* arr) {
if(isEmptyQueue(front, rear)) {
printf("Queue is empty!\n");
return;
}
printf("Queue is: ");
for(int i = *front; i < *rear; i++)
printf("%d ", arr[i]);
}
int isEmptyCirc(int* front, int* rear) {
if(*rear == *front) return 1;
return 0;
}
int isFullCirc(int* front, int* rear) {
printf("%d and %d\n", *front, *rear);
if(*front == *rear - 1 || (*front == MAX && *rear == 0)) return 1;
return 0;
}
void enqueueCirc(int* front, int* rear, int* arr, int val) {
if(isFullCirc(front, rear)) {
printf("Too many elements. Can't add more\n");
return;
}
if(*front == MAX - 1)
*front = 0;
arr[*front] = val;
*front += 1;
}
void dequeueCirc(int* front, int* rear) {
if(isEmptyCirc(front, rear)) {
printf("Queue is empty!\n");
return;
}
if(*rear == MAX - 1)
*rear = 0;
*rear = *rear + 1;
}
void displayQueueCirc (int* front, int* rear, int* arr) {
for(int i = *rear; i < *front; i++) {
printf("%d ", arr[i]);
}
if(*front < *rear) {
for(int i = 0; i < *front; i++) {
printf("%d\n", arr[i]);
}
}
}