-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
43 lines (41 loc) · 669 Bytes
/
stack.c
File metadata and controls
43 lines (41 loc) · 669 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
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
struct stack{
int data[MAXSIZE];
int top;
};
struct stack s;
void push(int new_data){//insert new data to top of stack
if(s.top == MAXSIZE-1){
printf("Stack is full cannot add anymore items");
return;
}else{
s.top++;
s.data[s.top] = new_data;
return;
}
}
int pop(){//remove item from top
int num;
num = s.data[s.top];
s.top--;
return num;
}
void printStack(){
for(int i = s.top; i >= 0; i--){
printf("%d\n", s.data[i]);
}
return;
}
int main(){
s.top = -1;
push(1);//1
push(2);//2 1
push(3);//3 2 1
pop();//2 1
push(12);// 12 2 1
push(8);//8 12 2 1
printStack();
return 0;
}