/
stack.c
56 lines (50 loc) · 908 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
56
#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("Successfully added item: %d\n", x);
}
else
{
printf("Exception! No spaces\n");
}
}
int pop()
{
if (top >= 0)
{
int topVal = stack[top];
top = top - 1;
return topVal;
}
printf("Exception from pop ! Empty stack \n");
return -1;
}
int peek()
{
if (top >= 0)
{
return stack[top];
}
printf("Exception from Peek! Empty Stack\n");
return -1;
}
int main()
{
printf("Implementing my stack datastructure in c.\n");
peek();
push(10);
push(20);
push(30);
printf("Pop here value : %d\n", pop());
push(40);
printf("Top of stack: %d\n", peek());
printf("Pop value : %d\n", pop());
return 0;
}