forked from zjpzhao/JLU_data_structure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-3bracket.cpp
More file actions
240 lines (226 loc) · 4.08 KB
/
2-3bracket.cpp
File metadata and controls
240 lines (226 loc) · 4.08 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
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
/*
第二周 第三题 括号匹配
利用循环链表存储表达式,利用栈判断括号是否匹配
不得不吐槽正常括号匹配根本不用存储表达式,这居然还用循环链表存...老师设计题目真是煞费苦心
1、实现循环链表类。
2、创建循环链表s,通过用户逐个字符输入,完成s的初始化。
3、编写程序实现函数Check,并输出检查结果。
4、为增强程序的可读性,对程序中较难理解的语句要有准确、清晰的注释。
5、由教师随机给出测试数据,程序能够反馈括号是否匹配,如果不匹配,指出第一个不匹配所在位置。
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
template<typename T>
class Node
{
public:
T data;
Node(T temp):data(temp){ next=NULL; }
Node<T> *next;
};
template<typename T>
class List
{
Node<T> *head;
Node<T> *curr;
Node<T> *last;
int size;
public:
List()
{
head=curr=last=NULL;
size=0;
}
int push_first(T);
int push_back(T dat);
int show();
int check(int);
};
template<typename T>
int List<T>::push_first(T dat)
{
if(size!=0)
return 1;
Node<T>*p=new Node<T>(dat);
head=last=p;
head->next=head;
size=1;
return 0;
}
template<typename T>
int List<T>::push_back(T dat)
{
if(size==0)
{
push_first(dat);
return 0;
}
size++;
Node<T>*p=new Node<T>(dat);
last->next=p;
last=p;
last->next=head;
return 0;
}
template<typename T>
int List<T>::show()
{
if(size==0)
return 1;
Node<T>*p=head;
while(1)
{
cout<<p->data<<" ";
p=p->next;
if(p==head)
break;
}
cout<<endl;
return 0;
}
#define MAX_SIZE 200 //定义最大栈长度
template<typename T>
class Stack
{
T data[MAX_SIZE];
int last;
public:
Stack(){last=-1;}
int push(T);
int pop();
vector<T> top();
int size();
bool empty();
bool full();
int show();
T bottom();
};
template<typename T>
int Stack<T>::push(T dat)
{
if(full())
{
cout<<"当前栈已满,pop或者增加栈容量重试"<<endl;
return 1;
}
last++;
data[last]=dat;
return 0;
}
template<typename T>
int Stack<T>::pop()
{
if(empty())
{
cout<<"空栈无法执行退栈操作"<<endl;
return 1;
}
last--;
return 0;
}
template<typename T>
vector<T> Stack<T>::top()
{
vector<T>v;
if(empty())
return v;
v.push_back(data[last]);
return v;
}
template<typename T>
int Stack<T>::size()
{
return last+1;
}
template<typename T>
bool Stack<T>::empty()
{
if(last==-1)
return true;
return false;
}
template<typename T>
bool Stack<T>::full()
{
if(last==MAX_SIZE)
return true;
return false;
}
template<typename T>
int Stack<T>::show()
{
if(empty())
{
cout<<"当前为空栈"<<endl;
return 0;
}
for(int i=0;i<=last;i++)
cout<<data[i]<<" ";
cout<<endl;
return 0;
}
inline bool is_bracket(char c)
{
if(c=='('||c==')'||c=='['||c==']'||c=='{'||c=='}')
return true;
return false;
}
template<typename T>
T Stack<T>::bottom()
{
return data[0];
}
template<typename T>
int List<T>::check(int n)
{
class Stack<char>s;
class Stack<int>pos;
Node<char>*p=new Node<char>('a');
p->next=head;
int count=-1;
for(int i=1;i<=n;i++) //读取list里面的每一个字符
{
p=p->next;
count++;
if(is_bracket(p->data))
{
if(s.empty())
{
if(p->data==')'||p->data==']'||p->data=='}')
return count;
s.push(p->data),pos.push(count);
continue;
}
vector<char>v=s.top();
char l=v[0],r=p->data;
if( (l=='('&&r==')') || (l=='['&&r==']') || (l=='{'&&r=='}'))
{
s.pop(),pos.pop();
continue;
}
if(p->data==')'||p->data==']'||p->data=='}')
return pos.top()[0];
s.push(p->data),pos.push(count);
}
}
if(s.empty())
return -1;
else
return pos.bottom();
}
int main()
{
class List<char> l;
string str;
cin>>str;
for(int i=0;i<str.size();i++)
l.push_back(str[i]);
int t=l.check(str.size());
if(t==-1)
cout<<"括号正确匹配"<<endl;
else
cout<<"括号在第 "<<t<<" 个位置失配"<<endl;
return 0;
}