-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack Array.cpp
More file actions
91 lines (87 loc) · 1.22 KB
/
Stack Array.cpp
File metadata and controls
91 lines (87 loc) · 1.22 KB
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
87
88
89
90
/* different operations on stack using array */
#include<stdio.h>
#define MAXSIZE 100
struct stack
{
int data[MAXSIZE];
int top;
};
int main()
{
struct stack s;
int x,n,elt;
void push(struct stack *,int );
int empty(struct stack *);
int pop(struct stack *);
int stacktop(struct stack *);
s.top=-1;
do
{
printf("MENU\n");
printf("1.Push\n2.Pop\n3.Stacktop\n4.Exit\n");
printf("enter choice ");
scanf("%d",&n);
switch(n)
{
case 1:
printf("enter the no to be pushed ");
scanf("%d",&elt);
push(&s,elt);
break;
case 2:
if(empty(&s))
{
printf("UNDERFLOW\n");
}
else
{
x=pop(&s);
printf("popped element = %d\n",x);
break;
}
case 3:
if(empty(&s))
{
printf("UNDERFLOW\n");
}
else
{
x=stacktop(&s);
printf("top element = %d\n",x);
break;
}
}
}
while(n<4);
return(0);
}
void push(struct stack *ps,int x)
{
if(ps->top==MAXSIZE-1)
{
printf("OVERFLOW\n");
}
else
{
ps->data[++ps->top]=x;
}
}
int empty(struct stack *ps)
{
if(ps->top==-1)
{
return(1);
}
else
{
return(0);
}
}
int pop(struct stack *ps)
{
return(ps->data[ps->top--]);
}
int stacktop(struct stack *ps)
{
return(ps->data[ps->top]);
}