用非递归方法实现共享表运算

这里把无共享表的广义表的运算代码(基于附加头节点的链表表示)稍作修改,得到了基于附加头节点链表表示的共享表运算代码,这里就不详细注释了,可以参考之前发布的无共享表运算代码的注释来帮助理解,在共享表运算代码里映射表标准库类型map起到了重要作用

广义表(包括共享表情形)字符串形式和链表表示的相互转换:

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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254

#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;

struct
{
int utype;
union
{
int ref;
struct *hlink;
char value;
}info;
struct *tlink;
Gen(int u);
Gen(int u, char v);
Gen(int u, int r);
};
Gen::Gen(int u) :utype(u), tlink(nullptr)
{
info.hlink = nullptr;
}

Gen::Gen(int u, int r) :utype(u), tlink(nullptr)
{
info.ref = r;
}

Gen::Gen(int u, char v) :utype(u), tlink(nullptr)
{
info.value = v;
}

class info
{
public:
vector<int> left;
vector<int> right;
Gen *share = nullptr;
vector<int>::size_type position = 0;
};

void creatsharelist(string &glist, map<string, info> &sharelist);
map<string, info>::iterator Search(int left, map<string, info> &sharelist);

int main()
{
string glist;
cout << "请输入广义表的字符串形式" << endl;
cin >> glist;
map<string, info> sharelist;

creatsharelist(glist, sharelist);

Gen *ptr = nullptr; vector<Gen *> stack; bool TF;
int ref = 0;
for (string::size_type i = 0; i != glist.size(); ++i)
{
if (glist[i] == '(')
{
if (i == 0)
{
ptr = new Gen(0, 0);
stack.push_back(ptr);
TF = true;
}
else
{
Gen *temp = new Gen(1);
if (ptr->utype == 1)
{
if (TF == true)
ptr->info.hlink->tlink = temp;
else
ptr->tlink = temp;
}
else
{
ptr->tlink = temp;
}

auto p = Search(i + 1, sharelist);
if (p != sharelist.end() && p->second.share != nullptr)
{
temp->info.hlink = p->second.share;
++p->second.share->info.ref;
TF = false;
ptr = temp;
i = p->second.right[p->second.position-1]-1;
continue;
}
else
{
stack.push_back(temp);
TF = true;
ptr = temp;
temp = new Gen(0, 1);
ptr->info.hlink = temp;
if (p != sharelist.end())
p->second.share = ptr->info.hlink;
}
}
}
else
{
if (glist[i] == ')')
{
ptr = stack[stack.size() - 1];
stack.pop_back();
TF = false;
}
else
{
if (glist[i] != ',')
{
Gen *temp = new Gen(2, glist[i]);
if (ptr->utype == 1)
{
if (TF == true)
ptr->info.hlink->tlink = temp;
else
ptr->tlink = temp;
}
else
{
ptr->tlink = temp;
}

ptr = temp;
}
}
}
}
cout << "已将广义表的字符串形式转换为带附加头节点的链表形式" << endl;
cout << "链表表示对应的广义表形式为:" << endl;
TF = true;
while (true)
{
if (ptr != nullptr && (ptr->utype == 0 || ptr->utype == 1)) //注意
{
if (TF == true)
{
if (ptr->utype == 0) //注意
{
if (ptr->info.ref == 0)
{
stack.push_back(ptr);
cout << "(";
}
ptr = ptr->tlink;
}
else
{
stack.push_back(ptr);
cout << "(";
ptr = ptr->info.hlink;
}
}
else
{
if (ptr->utype == 0)
break;
else
{
ptr = ptr->tlink;
TF = true;
}
}
}
else
{
if (ptr == nullptr)
{
cout << ")";
ptr = stack[stack.size() - 1];
if (ptr->utype != 0 && ptr->tlink != nullptr) //注意
cout << ",";
stack.pop_back();
TF = false;
}
else
{
cout << ptr->info.value;
ptr = ptr->tlink;
if (ptr != nullptr)
cout << ",";
}
}
}
cout << endl;
return 0;
}

void creatsharelist(string &glist, map<string, info> &sharelist)
{
vector<int> stack;
int total = 0;
for (const auto &s : glist)
{
if (s == '(')
{
++total;
stack.push_back(total);
}
else if (s == ')')
{
++total;
string temp = glist.substr(stack[stack.size() - 1] - 1, total - stack[stack.size() - 1] + 1);
auto p = sharelist.find(temp);
if (p == sharelist.end())
{
auto q = sharelist.insert(make_pair(temp, *(new info)));
q.first->second.left.push_back(stack[stack.size() - 1]);
q.first->second.right.push_back(total);
}
else
{
p->second.left.push_back(stack[stack.size() - 1]);
p->second.right.push_back(total);
}
stack.pop_back();
}
else
{
++total;
}
}

auto m = sharelist.begin();
while (m != sharelist.end())
{
if (m->second.left.size() == 1)
m = sharelist.erase(m);
else
++m;
}
}

map<string, info>::iterator Search(int left, map<string, info> &sharelist)
{
auto p = sharelist.begin();
for (; p != sharelist.end(); ++p)
{
if (p->second.left.size() != p->second.position && p->second.left[p->second.position] == left)
{
++p->second.position;
return p;
}
}
return p;
}

广义表(包括共享表情形)字符串形式和多叉树(包括共享树)

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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268

#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;

class Knode
{
public:
char data;
int ref;
Knode **p;
Knode(int k, int r, char ch);
};

Knode::Knode(int k, int r, char ch = '