-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenlist.cpp
More file actions
140 lines (130 loc) · 3.55 KB
/
genlist.cpp
File metadata and controls
140 lines (130 loc) · 3.55 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <iostream>
#include <string>
using std::cin, std::cout;
template <typename T>
class GenList
{
private:
struct node
{
bool utype; //0 for listhead, 1 for atom
union {T value; node* hlink;} info;
node* tlink;
node(bool u = 0) : utype(u), tlink(nullptr) {if (utype == 0) info.hlink = nullptr;}
};
node* first;
node* conv(const std::string& s, size_t&); //string -> GenList<char>
void print(node *p);
void destroy(node *p);
public:
GenList() : first(nullptr) {}
~GenList() {destroy();}
void conv(const std::string& s) {size_t pos = 0; first = conv(s, pos);}
void print() {print(first);}
bool equal(node* s, node* t);
bool equal(const GenList& other) {return equal(this -> first, other.first);}
};
template<>
GenList<char>::node* GenList<char>::conv(const std::string& s, size_t& pos)
{
node* head = nullptr;
node* last = nullptr;
while(pos < s.size())
{
if(s[pos] == '(')
{
pos++;
node* newnode = new node(0);
newnode -> info.hlink = conv(s, pos);
if(head == nullptr)
last = head = newnode;
else
{
last -> tlink = newnode;
last = newnode;
}
}
else if(s[pos] == ' ' || s[pos] == '\t' || s[pos] == ',')
pos++;
else if(s[pos] == ')')
{
pos++;
return head;
}
else
{
node* newnode = new node(1);
newnode -> info.value = s[pos];
if(head == nullptr)
head = last = newnode;
else
{
last -> tlink = newnode;
last = newnode;
}
pos++;
}
}
return head;
}
template <typename T>
void GenList<T>::print(typename GenList<T>::node* p)
{
if(p == nullptr)
return;
if(p -> utype == 0)
{
cout << "(";
print(p -> info.hlink);
cout << ")";
if(p ->tlink != nullptr)
cout << ",";
print(p -> tlink);
}
else
{
cout << p -> info.value;
if(p ->tlink != nullptr)
cout << ",";
print(p -> tlink);
}
}
template<typename T>
bool GenList<T>::equal(node* s, node* t)
{
if(s == nullptr && t == nullptr)
return 1;
if(s == nullptr || t == nullptr)
return 0;
if(s -> utype != t -> utype)
return 0;
if(s -> utype == 1)
return (s -> info.value == t -> info.value) && equal(s -> tlink, t -> tlink);
else
return equal(s -> info.hlink, t -> info.hlink) && equal(s -> tlink, t -> tlink);
return 0;
}
template <typename T>
void GenList<T>::destroy(node* p)
{
node* temp;
while(p != nullptr)
{
if(p -> utype == 0)
destroy(p -> info.hlink);
temp = p;
p = p -> tlink;
delete temp;
}
}
int main()
{
GenList<char> a, b, c;
a.conv("(a,(b, c), d)"); b.conv("(a,(b, c), d)"); c.conv("(a,(b,c))");
printf("GenList a: "); a.print(); cout << std::endl;
printf("GenList b: "); b.print(); cout << std::endl;
printf("GenList c: "); c.print(); cout << std::endl;
printf("a == b : "); cout << a.equal(b) << std::endl;
printf("a == c : "); cout << a.equal(c) << std::endl;
return 0;
}