-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCopy stack.c
136 lines (115 loc) · 1.7 KB
/
Copy 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include<stdio.h>
# define size 100
typedef struct stack1{
int top1;
int a[size];
}Stack_1;
typedef struct stack2{
int top2;
int b[size];
}Stack_2;
void init_1(Stack_1 *p)
{
p->top1=-1;
}
void push_1(Stack_1 *p,int v)
{
if(p->top1 == size-1)
{
printf("stack is full\n");
return;
}
else{
p->top1+=1;
p->a[p->top1]=v;
}
}
int pop_1(Stack_1 *p){
if(p->top1 == -1){
printf("stack is empty\n");
return 0;
}
else{
return p->a[p->top1--];
}
}
char peek_1(Stack_1 *p)
{
if(p->top1 == -1)
return 1;
else{
return p->a[p->top1];
}
}
int empty_1(Stack_1 *p)
{
if(p->top1 == -1)
return 1;
else
return 0;
}
void init_2(Stack_2 *p)
{
p->top2=-1;
}
void push_2(Stack_2 *p,int v)
{
if(p->top2 == size-1)
{
printf("stack is full\n");
return;
}
else{
p->top2+=1;
p->b[p->top2]=v;
}
}
int pop_2(Stack_2 *p){
if(p->top2 == -1){
printf("stack is empty\n");
return 0;
}
else{
return p->b[p->top2--];
}
}
char peek_2(Stack_2 *p)
{
if(p->top2 == -1)
return 1;
else{
return p->b[p->top2];
}
}
int empty_2(Stack_2 *p)
{
if(p->top2 == -1)
return 1;
else
return 0;
}
int main()
{
Stack_1 s;
init_1(&s);
Stack_2 t;
init_2(&t);
push_1(&s,5);
push_1(&s,6);
push_1(&s,7);
push_1(&s,8);
push_1(&s,9);
push_1(&s,10);
push_2(&t,pop_1(&s));
push_2(&t,pop_1(&s));
push_2(&t,pop_1(&s));
push_2(&t,pop_1(&s));
push_2(&t,pop_1(&s));
push_2(&t,pop_1(&s));
printf("%d\t",pop_2(&t));
printf("%d\t",pop_2(&t));
printf("%d\t",pop_2(&t));
printf("%d\t",pop_2(&t));
printf("%d\t",pop_2(&t));
printf("%d\t",pop_2(&t));
}