-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack7.c
86 lines (83 loc) · 1.19 KB
/
Stack7.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Stack7.c
*
* Created on: 14 Jul 2024
* Author: ABHISHEK
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 100
struct stack
{
int list[size];
int top;
};
void push(struct stack *sp,int x)
{
if(sp->top==size)
{
printf("\n Stack overflow");
return;
}
sp->list[++sp->top]=x;
return;
}
int pop(struct stack *sp)
{
if(sp->top==-1)
{
printf("\n Stack is empty i.e underflow");
return -1;
}
return(sp->list[sp->top--]);
}
int peek(struct stack p)
{
int n;
if(p.top==-1)
{
printf("\nStack is underflow");
return -1;
}
n=p.list[p.top];
return n;
}
int main()
{
struct stack s;
int opt,x,n;
s.top=-1;
while(-1)
{
printf("\n1 Push 2 pop 3 peek 4 exit\n");
printf("Enter your choice :");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("\nEnter number:");
scanf("%d",&x);
push(&s,x);
break;
case 2:
n=pop(&s);
if(n!=-1)
{
printf("poped value = %d",n);
}
break;
case 3:
n=peek(s);
if(n!=-1)
{
printf("\nTop element :%d",n);
}
break;
case 4:
exit(0);
}
}
getch();
return 0;
}