-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstack_copy.c
98 lines (83 loc) · 1.32 KB
/
stack_copy.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
#include<stdio.h>
#include<stdlib.h>
# define Max 5
typedef struct
{
int data[Max];
int top;
}STACK;
int push(STACK *s,int v)
{
if(s->top==Max-1)
{
printf("OF");
return 1;
}
else
{
s->top++;
s->data[s->top]=v;
return 0;
}
}
int pop(STACK *s,int *k)
{
if(s->top==-1)
{
printf("UF");
return 1;
}
else
{
*k=s->data[s->top];
s->top--;
return 0;
}
}
void copy(STACK *s1,STACK *s2)
{
int m;
STACK s;
s.top=-1;
for(int i=0;i<Max;i++)
s.data[i]=0;
while(s1->top!=-1)
{
pop(s1,&m);
push(&s,m);
}
while(s.top!=-1)
{
pop(&s,&m);
push(s2,m);
}
}
int main()
{
STACK s1;
STACK s2;
s1.top=-1;
s2.top=-1;
int m;
for(int i=0;i<Max;i++)
s1.data[i]=0;
for(int i=0;i<Max;i++)
s2.data[i]=0;
push(&s1,10);
push(&s1,5);
push(&s1,6);
push(&s1,7);
push(&s1,8);
for(int i=0;i<Max;i++)
{
printf("%d ",s1.data[i]);
}
printf("\n");
copy(&s1,&s2);
for(int i=0;i<Max;i++)
{
printf("%d ",s2.data[i]);
}
printf("\n");
return 0;
}