-
Notifications
You must be signed in to change notification settings - Fork 0
/
03-树3TreeTraversalsAgain.c
162 lines (145 loc) · 1.95 KB
/
03-树3TreeTraversalsAgain.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
13/25
测试点 1 2 4 错误
*/
#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node *List;
struct Node
{
int data;
List next;
};
List Crea();
void Push(int data,List L);
int Pop(List L);
int IsEmp(List L);
int main()
{
int n;
scanf("%d", &n);
char type[31][4];
int number,flag,count,vol;
int temp[31];
int data[31];
List L;
L = Crea();
flag = 0;
count = 0;
vol = 0;
for (int i=0;i<31;i++)
{
temp[i] = 0;
data[i] = 0;
}
for (int i = 0; i < 2 * n; i++)
{
scanf(" %s", type[i]);
if (strcoll(type[i], "Push") == 0)
{
scanf(" %d",&number);
Push(number, L);
temp [i] = 0;
}
else
{
temp[i] = Pop(L);
}
}
for (int i=0;i<n*2+1;i++)
{
if (flag=0 && temp[i]!=0)
{
flag = 1;
}
else
{
data[count] = temp[i];
count++;
}
}
L = Crea();
flag = 0;
for (int i = 0; i < count + 2; i++)
{
if (data[i]!=0 && data[i+1]==0)
{
if (flag == 0)
{
flag = 1;
}
if (vol < 2)
{
Push(data[i], L);
vol++;
}
else if (vol == 2)
{
printf("%d ", Pop(L));
printf("%d ", Pop(L));
Push(data[i], L);
vol = 1;
}
}
else if (data[i]!=0)
{
printf("%d ",data[i]);
if (flag == 1)
{
printf("%d ",Pop(L));
vol = 0;
flag = 2;
}
} if (i == count)
{
while (!IsEmp(L))
{
printf("%d", Pop(L));
if (L->next!=NULL)
{
printf(" ");
}
}
}
}
return 0;
}
List Crea( )
{
List L;
L = (List)malloc(sizeof(struct Node));
L->next = NULL;
return L;
}
int IsEmp(List L)
{
if (L->next==NULL)
{
return 1;
}
return 0;
}
int Pop(List L)
{
if (IsEmp(L))
{
return 0;
}
int number;
List temp;
temp= L->next;
number = temp->data;
L->next = temp->next;
free(temp);
return number;
}
void Push(int data, List L)
{
List temp;
temp = (List)malloc(sizeof (struct Node));
temp->data = data;
temp->next = L->next;
L->next = temp;
}