-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
52 lines (47 loc) · 899 Bytes
/
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
#include <stdio.h>
#include <stdlib.h>
int stack[10];
int num_items = 0;
int push(int value) {
if (num_items == 10) {
return 1;
}
else {
stack[num_items] = value;
num_items += 1;
return 0;
}
}
int pop(int *value) {
if (num_items == 0) {
return 1;
}
else{
int pop_num = stack[num_items - 1];
num_items -= 1;
*value = pop_num;
return 0;
}
}
void printStack(int value) {
int i;
printf ("Stack: ");
if (value == 0) {
for (i=0; i < num_items; i++) {
printf("%d ", stack[i]);
}
printf("\n");
}
else if (value == 1){
for (i=0; i < num_items; i++) {
printf("%x ", stack[i]);
}
printf("\n");
}
else {
for (i=0; i < num_items; i++) {
printf("%c ", stack[i]);
}
printf("\n");
}
}