-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstake_using_array.cpp
More file actions
85 lines (80 loc) · 1.16 KB
/
stake_using_array.cpp
File metadata and controls
85 lines (80 loc) · 1.16 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
#include <stdio.h>
#include <stdlib.h>
struct stack
{
int size;
int top;
int *s;
};
void create(struct stack *st)
{
printf("enter your size");
scanf("%d", &st->size);
st->top = -1;
st->s = (int *)malloc(st->size * sizeof(int));
}
void push(struct stack *st, int x)
{
if (st->top == st->size - 1)
{
printf("stack is overflow\n");
}
else
{
st->top++;
st->s[st->top] = x;
}
}
int pop(struct stack *st)
{
int x = -1;
if (st->top == -1)
{
printf("stack in under flow");
}
else
{
x = st->s[st->top];
st->top--;
}
return x;
}
void display(struct stack st)
{
for (int i = st.top; i >= 0; i--)
{
printf("%d", st.s[i]);
}
printf("\n");
}
int main()
{
struct stack st;
create(&st);
int x;
int a;
while (1)
{
printf("enter 1 for push and 2 for pop and 3 for display");
scanf("%d", &x);
switch (x)
{
case 1:
printf("enter push item : ");
scanf("%d", &a);
push(&st, a);
break;
case 2:
a = pop(&st);
if (a != -1)
{
printf("item poped %d\n",a);
}
break;
case 3:
display(st);
break;
}
}
return 0;
}