-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstack.cpp
More file actions
65 lines (59 loc) · 960 Bytes
/
stack.cpp
File metadata and controls
65 lines (59 loc) · 960 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int stack[10];
int* head = stack;
int* tail = head;
int maxStack = 5;
int getStackLength() {
int i = 0;
while(stack[i] != 0){
i++;
}
return i;
}
void push(int data) {
if(getStackLength() >= maxStack) {
printf("stack overflow\n");
}
else {
*tail = data;
tail++;
}
}
void pop() {
if(tail == head) {
printf("stack empty\n");
}
else {
tail--;
printf("pop %d\n", *tail);
*tail = 0;
}
}
void print(){
for(head ; head != tail ; head++){
printf("%d ",*head);
}
printf("\n");
head = stack;
}
int main() {
char input[100];
int data;
while(1){
printf("command: ");
scanf("%s",input);
if(strcmp(input,"push") == 0) {
printf("input: ");
scanf("%d",&data);
push(data);
}
else if(strcmp(input,"print") == 0) {
print();
}
else if(strcmp(input,"pop") == 0) {
pop();
}
}
}