-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.c
55 lines (41 loc) · 802 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
53
54
55
#include <stdio.h>
#define CAPACITY 3
int stack[CAPACITY];
int top = -1;
void push(int x){
if(top<CAPACITY - 1)
{
top = top + 1;
stack[top]= x;
printf("Succesfully added item: %d\n", x);
}else{
printf("Exception! No space\n");
}
}
int pop()
{ if (top>=0){
int val = stack[top];
top = top -1;
return val;
}
printf("Exception from pop! Empty stack\n");
return -1;
}
int peek()
{ if(top >= 0){
return stack [top];
}
printf("Expection from peek! stack\n");
return -1;
}
int main(){
printf("Implementing my stack in c.\n");
peek();
push(10);
push(20);
push(30);
printf("pop item : %d\n",pop());
push(40);
printf("Top of stack : %d\n",peek());
return 0;
}