-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcreate_stack.c
More file actions
121 lines (103 loc) · 1.71 KB
/
create_stack.c
File metadata and controls
121 lines (103 loc) · 1.71 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include<stdio.h>
#include<stdlib.h>
# define Max 5
typedef struct
{
int data[Max];
int top1;
int top2;
}STACK;
int push(STACK *s,int v,int p)
{
int count=0;
if(p==0)
{
if (s->top1 == s->top2 - 1)
{
printf("OverFlow");
return 1;
}
else
{
s->top1++;
s->data[s->top1]=v;
count++;
return 0;
}
}
if(p==1)
{
if (s->top2 == s->top1 + 1)
{
printf("OverFlow");
return 1;
}
else
{
s->top2--;
s->data[s->top2]=v;
count++;
return 0;
}
}
}
int pop(STACK *s,int *k,int p)
{
if(p==0)
{
if(s->top1==-1)
{
printf("UnderFlow");
return 1;
}
else
{
s->data[s->top1]=0;
s->top1--;
return 0;
}
}
if(p==1)
{
if(s->top2==Max)
{
printf("UnderFlow");
return 1;
}
else
{
s->data[s->top2]=0;
s->top2++;
return 0;
}
}
}
int main()
{
STACK s1;
s1.top1=-1;
s1.top2=Max;
int m;
for(int i=0;i<Max;i++)
s1.data[i]=0;
push(&s1,10,0);
push(&s1,5,1);
push(&s1,2,0);
push(&s1,3,1);
push(&s1,4,0);
for(int i=0;i<Max;i++)
{
printf("%d ",s1.data[i]);
}
pop(&s1,&m,1);
pop(&s1,&m,1);
pop(&s1,&m,0);
pop(&s1,&m,0);
pop(&s1,&m,0);
printf("\n");
for(int i = 0; i < Max; i++)
{
printf("%d ", s1.data[i]);
}
return 0;
}