-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasics_of_stack.c
72 lines (69 loc) · 1015 Bytes
/
basics_of_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//basics of stack
//push
//pop
//peek
#include<stdio.h>
#include<stdlib.h>
void push(int *,int);
void pop(int *);
void peek(int *);
int max=5;
int top=-1;
int main()
{
int choice,data,stack[max];
while(1)
{
printf("choose any option\n");
printf("1.push 2.pop 3.peek 4.exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: if(top==max-1)
{
printf("stack is full\n");
}
else
{
printf("enter the data to be add\n");
scanf("%d",&data);
push(stack,data);
printf("%d added to the stack\n",data);
}
break;
case 2: if(top==-1)
{
printf("stack is empty\n");
}
else
{
pop(stack);
}
break;
case 3: if(top==-1)
{
printf("stack is empty\n");
}
else
{
peek(stack);
}
break;
case 4: exit(0);
}
}
}
void push(int *stack,int data)
{
stack[++top]=data;
}
void pop(int *stack)
{
printf("%d is poped out\n",stack[top]);
stack[top]=0;
--top;
}
void peek(int *stack)
{
printf("%d is at the top\n",stack[top]);
}