-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz2-stack.c
153 lines (139 loc) · 2.12 KB
/
quiz2-stack.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
// QUIZ 3 / Stack Operations
// Submitted by Ronald T. Tolentino
// TUP / CS203
/*
Instructions
Write the c programs for the Stack and Queue operations
stack operations
Menu
Push
Pop
display
Exit
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 3
typedef struct stack
{
int S[MAX];
int top;
} STACK;
STACK S;
void makenull();
void push(int x);
void pop();
void makenull();
int isfull();
int isempty();
void display();
int menu();
int main()
{
int x;
makenull();
while (1)
{
switch (menu())
{
case 1:
system("cls");
printf("Push Mode\n");
printf("Input x: ");
scanf("%d", &x);
push(x);
break;
case 2:
system("cls");
printf("Pop Mode\n");
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\n1-4 only!\n");
system("pause");
}
}
return 0;
}
void makenull()
{
S.top = MAX;
}
void push(int x)
{
if (isfull())
{
printf("Stack Overflow.\n");
}
else
{
S.top--;
S.S[S.top] = x;
}
}
void pop()
{
if (isempty())
{
printf("Stack is empty.\n");
}
else
{
printf("Top element removed.\n");
S.top++;
}
}
int isfull()
{
return (S.top == 0);
}
int isempty()
{
return (S.top == MAX);
}
void display()
{
int i;
if (!isempty())
{
printf("The stocks contains...\n");
for (i = S.top; i < MAX; i++)
{
printf("%d.)%d\n", i + 1, S.S[i]);
}
}
else
{
printf("No data to display.\n");
}
}
int locate(int x)
{
int i;
for (i = 0; i <= S.top; i++)
{
if (x == S.S[i])
{
return i;
}
}
return -1;
}
int menu()
{
int op;
printf("MENU\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("\nSelect(1-4): ");
scanf("%d", &op);
return (op);
}