-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
68 lines (58 loc) · 933 Bytes
/
Copy pathstack.cpp
File metadata and controls
68 lines (58 loc) · 933 Bytes
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
#include <stdio.h>
#include <stdlib.h>
# define N 100
typedef struct StackType {
int top;
char stack[N];
}StackType;
void init(StackType* s) {
s->top=-1;
}
bool isEmpty(StackType* s) {
return s->top==-1;
}
bool isFull(StackType* s) {
return s->top==N-1;
}
void push(StackType* s, char c) {
if (isFull(s)) {
printf("Overflow\n");
} else {
s->top++;
s->stack[s->top] = c;
}
}
void print(StackType* s) {
for(int i=0; i<=s->top; i++) {
printf("%c\n", s->stack[i]);
}
}
char pop(StackType* s) {
if(isEmpty(s)) {
printf("Empty\n");
return '.';
} else {
char c = s->stack[s->top];
s->top--;
return c;
}
}
char peak(StackType* s) {
if(isEmpty(s)) {
printf("Empty\n");
return '.';
} else {
return s->stack[s->top];
}
}
int main() {
StackType s1;
init(&s1);
push(&s1, 'a');
push(&s1, 'b');
push(&s1, 'c');
print(&s1);
printf("%c\n", pop(&s1));
printf("%c\n", peak(&s1));
return 0;
}