forked from akij17/Data-Structures-in-Pure-C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackQueue.c
132 lines (121 loc) · 3.37 KB
/
stackQueue.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
//
// Created by Akshay on 11/14/2018.
//
#include "stackQueue.h"
#include "stack_al.h"
#include "main.h"
#include <stdio.h>
#include "queue_ll.h"
void stackOptions();
void queueOptions();
int sqtype = 0;
char *sq1 = "Stack";
char *sq2 = "Queue";
void stackQueue(){
printf("\nWhich data structure would you like to work on ?\n"
"1. Stack\n"
"2. Queue\n"
"99. Return to main menu\n"
"Enter your option: ");
int x; scanf("%d", &x);
switch (x){
case 1:
stackOptions();
break;
case 2:
queueOptions();
break;
case 99:
main();
break;
default:
printf("Invalid entry. Going to main menu");
main();
break;
}
}
void stackOptions(){
stack_al *stack = newStack_al(10);
beginStack:
printf("\n"
"Enter your choice from options below\n"
"1. Push items in stack\n"
"2. Pop items from stack\n"
"3. Seek the top item in the stack\n"
"4. Back to main menu\n"
"Enter your choice: "
);
int c1; scanf("%d", &c1);
switch(c1){
case 1:
printf("\nEnter the element to push: ");
int x; scanf("%d", &x);
push_stack_al(stack, x);
goto beginStack;
case 2:
printf("\nPopped item from stack: ");
int item = pop_stack_al(stack);
if(item == -1){
printf("\nStack underflow!\n");
}else {
printf("%d\n",item);
} goto beginStack;
case 3:
printf("\nPopped item from stack: ");
int itm = seek_stack_al(stack);
if(itm == -1){
printf("\nStack underflow!\n");
}else {
printf("%d\n",itm);
}
goto beginStack;
case 4:
stackQueue();
break;
default:
printf("invalid entry");
goto beginStack;
}
}
void queueOptions(){
queue_ll *queue = new_queue_ll();
beginQueue:
printf("\n"
"Enter your choice from options below\n"
"1. Enqueue items in queue\n"
"2. Dequeue items from queue\n"
"3. Front of the queue\n"
"4. Back to main menu\n"
"Enter your choice: "
);
int c1; scanf("%d", &c1);
switch(c1){
case 1:
printf("Enter element to enqueue: ");
int x; scanf("%d", &x);
enqueue_queue_ll(queue, x);
goto beginQueue;
case 2:
printf("Dequeue item: ");
int item = dequeue_queue_ll(queue);
if(item == -1){
printf("Queue is empty\n");
}else
printf("%d\n", item);
goto beginQueue;
case 3:
printf("Front item: ");
int itm = front_queue_ll(queue);
if(itm == -1){
printf("Queue is empty\n");
}else
printf("%d\n", itm);
goto beginQueue;
case 4:
stackQueue();
break;
default:
printf("invalid entry");
goto beginQueue;
}
}