-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
48 lines (45 loc) · 718 Bytes
/
stack.c
File metadata and controls
48 lines (45 loc) · 718 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
#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 items %d\n", x);
}
else
{
printf("No Spaces\n");
}
}
int pop()
{
if(top >= 0)
{
int value = stack[top];
top = top - 1;
return value;
printf("Remove Item in stack: %d\n", value);
}
return -1;
}
int peek()
{
if(top >= 0)
{
return stack[top];
}
return -1;
}
int main()
{
push(20);
push(30);
push(40);
printf("Pop new item: %d\n", pop());
push(50);
printf("Top of the stack: %d\n", peek());
}