栈与队列

10.6栈与队列

栈不存在的条件:base == NULL;
栈为空的条件: base == top;
栈满的条件:top-base == stacksize;

  1. 初始化栈

  2. 获取栈顶元素 top()

  3. 入栈 push()

  4. 出栈 pop()

练习一:十进制n转为r进制数

1
2
3
4
5
6
7
8
9
10
11
12
int n,d,r;
std::stack<int>(s);
std::cin>>n>>r;
while (n!=0){
d = n%r;
n = n/r;
s.push(d);
}
while (!s.empty()){
std::cout<<s.top();
s.pop();
}

练习二:括号匹配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char a[20];
std::stack<char>(s);
cin>>a;
for (int i = 0; a[i] != '\0'; ++i) {
if (s.empty()){
s.push(a[i]);
} else if (((s.top() == '(')&&(a[i] == ')'))||((s.top() == '[')&&(a[i] == ']'))||((s.top() == '{')&&(a[i] == '}'))){
s.pop();
} else if (a[i] == '('||a[i] == '['||a[i] == '{'){
s.push(a[i]);
}
}
if (s.empty()){
cout<<"YES";
} else{
cout<<"NO";
}

10.9 队列

队列

空队列:front == rear
满队列:(rear + 1) % max == front

  1. 循环队列的初始化

  2. 入队

  3. 出队


10.9 上机

栈与队列的基本操作(内含进制转换与括号匹配[STL模版])

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
#include <iostream>
#include <stack>
//#include <queue>
#include <iomanip>

#pragma GCC optimize(2)

using namespace std;

typedef struct node {
int data;
node *next;
}Qnode,Snode;

typedef struct{
Qnode *rear,*front;
}Qlink;

typedef struct{
Snode *base,*top;
}Slink;

void InitStack(Slink &s){
s.top = s.base = NULL;
}

void push(Slink &s,int n){
Snode *p;
p = new (Snode);
p->data = n;
p->next = NULL;
if (s.base == NULL){
s.top = s.base = p;
} else{
s.top->next = p;
s.top = p;
}
}

void pop(Slink &s){
Snode *p;
p = s.base;
if ((s.base == s.top)&&(s.base != NULL)){
free(s.base);
s.base = s.top = NULL;
return;
}
while (p->next != s.top) p = p->next;
s.top = p;
free(p->next);
p->next =NULL;
}

int top(Slink s){
return s.top->data;
}

int empty(Slink s){
if (s.base == NULL)
return 1;
else
return 0;
}

void InitQue(Qlink &q){
Qnode *p = new (node);
p->next = NULL;
q.rear = q.front = p;
}

void InsertQue(Qlink &q,int n){
Qnode *p = new (node);
p->data = n;
p->next = NULL;
q.rear->next = p;
q.rear = p;
}

void DelQue(Qlink &q,int &e){
if (q.front == q.rear){
return;
}
Qnode *p;
p = q.front;
e = q.front->next->data;
q.front = q.front->next;
free(p);
}

void PrintQue(Qlink q){
Qnode *p;
p = q.front->next;
while (p != NULL){
cout<<p->data<<" ";
p = p->next;
}
}

int main() {
int temp;
Qlink q;
InitQue(q);
cin>>temp;
for (int i = 0; i < temp; ++i) {
InsertQue(q,rand()%100);
}
cout<<"输出队列:"<<endl;
PrintQue(q);
DelQue(q,temp);
cout<<endl;
cout<<"出队元素:";
cout<<temp<<endl;
cout<<"出队后队列:"<<endl;
PrintQue(q);
cout<<endl;
//TODO:
Slink s;
InitStack(s);
cin>>temp;
for (int i = 0; i < temp; ++i) {
push(s,rand()%100);
}
cout<<"压栈完成:"<<endl;
for (int i = 0; i < temp; ++i) {
int t = top(s);
cout<<t<<" ";
pop(s);
}
cout<<endl;
int n,r,d;
cout<<"进制转换,输入十进制数n与目标进制r:"<<endl;
InitStack(s);
cin>>n>>r;
while (n!=0){
d = n%r;
n = n/r;
push(s,d);
}
cout<<"转换完成:";
while (!empty(s)){
std::cout<<top(s);
pop(s);
}
cout<<"("<<r<<")"<<endl;
cout<<"括号匹配,输入一行括号:"<<endl;
char a[100];
stack<char>(S);
cin>>a;
for (int i = 0; a[i] != '\0'; ++i) {
if (S.empty()){
S.push(a[i]);
} else if (((S.top() == '(')&&(a[i] == ')'))||((S.top() == '[')&&(a[i] == ']'))||((S.top() == '{')&&(a[i] == '}'))){
S.pop();
} else if (a[i] == '('||a[i] == '['||a[i] == '{'){
S.push(a[i]);
}
}
if (S.empty()){
cout<<"YES";
} else{
cout<<"NO";
}
}


/*
* //TODO:进制转换
* int n,d,r;
stack<int>(s);
cin>>n>>r;
while (n!=0){
d = n%r;
n = n/r;
s.push(d);
}
while (!s.empty()){
std::cout<<s.top();
s.pop();
}
*/

/*
* //TODO:括号匹配
* char a[20];
stack<char>(s);
cin>>a;
for (int i = 0; a[i] != '\0'; ++i) {
if (s.empty()){
s.push(a[i]);
} else if (((s.top() == '(')&&(a[i] == ')'))||((s.top() == '[')&&(a[i] == ']'))||((s.top() == '{')&&(a[i] == '}'))){
s.pop();
} else if (a[i] == '('||a[i] == '['||a[i] == '{'){
s.push(a[i]);
}
}
if (s.empty()){
cout<<"YES";
} else{
cout<<"NO";
}
*/

栈实现计算表达式的值

``使用STL模版```

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
#include<iostream>
#include<cstdio>
#include<cctype>
#include<stack>

#pragma GCC optimize(2)

using namespace std;

//stack<char> OPTR;
//stack<double> OPND;

typedef struct node {
char data;
node *next;
}Qnode,Snode;

typedef struct Cnode {
int data;
Cnode *next;
}SnodeC;

typedef struct{
Qnode *rear,*front;
}Qlink;

typedef struct{
SnodeC *base,*top;
}SlinkC;

typedef struct {
Snode *base, *top;
}Slink;

template<class T>
void InitStack(T &s){
s.top = s.base = NULL;
}

//void InitStack(Slink &s){
// s.top = s.base = NULL;
//}
//
//void InitStack(SlinkC &s){
// s.top = s.base = NULL;
//}

void push(Slink &s,int n){
Snode *p;
p = new (Snode);
p->data = n;
p->next = NULL;
if (s.base == NULL){
s.top = s.base = p;
} else{
s.top->next = p;
s.top = p;
}
}

void push(SlinkC &s,char n){
SnodeC *p;
p = new (SnodeC);
p->data = n;
p->next = NULL;
if (s.base == NULL){
s.top = s.base = p;
} else{
s.top->next = p;
s.top = p;
}
}

void pop(Slink &s){
Snode *p;
p = s.base;
if ((s.base == s.top)&&(s.base != NULL)){
free(s.base);
s.base = s.top = NULL;
return;
}
while (p->next != s.top) p = p->next;
s.top = p;
free(p->next);
p->next =NULL;
}

void pop(SlinkC &s){
SnodeC *p;
p = s.base;
if ((s.base == s.top)&&(s.base != NULL)){
free(s.base);
s.base = s.top = NULL;
return;
}
while (p->next != s.top) p = p->next;
s.top = p;
free(p->next);
p->next =NULL;
}

int top(Slink s){
return s.top->data;
}

char top(SlinkC s){
return s.top->data;
}

template<class T>
int empty(T s){
if (s.base == NULL)
return 1;
else
return 0;
}

int getIndex(char theta) //中文"("与")"不可用
{
int index = 0;
switch (theta)
{
case '+':
index = 0;
break;
case '-':
index = 1;
break;
case '*':
index = 2;
break;
case '/':
index = 3;
break;
case '(':
index = 4;
break;
case ')':
index = 5;
break;
case '#':
index = 6;
default:break;
}
return index;
}

char Precede(char theta1, char theta2)
{
const char priority[][7] = //优先度
{
{ '>','>','<','<','<','>','>' },
{ '>','>','<','<','<','>','>' },
{ '>','>','>','>','<','>','>' },
{ '>','>','>','>','<','>','>' },
{ '<','<','<','<','<','=','0' },
{ '>','>','>','>','0','>','>' },
{ '<','<','<','<','<','0','=' },
};

int index1 = getIndex(theta1);
int index2 = getIndex(theta2);
return priority[index1][index2];
}

double calculate(double b, char theta, double a)
{
switch (theta)
{
case '+':
return b + a;
case '-':
return b - a;
case '*':
return b * a;
case '/':
return b / a;
default:
break;
}
}

double getAnswer(SlinkC &OPTR,Slink &OPND)
{
push(OPTR,'#');
int counter = 0; //用来记录数字的位数
char c = getchar();
while (c != '#' || top(OPTR) != '#')
{
if (isdigit(c)) //判断是否是数字
{
if (counter == 1)
{
double t = top(OPND); //保留栈顶
pop(OPND);
push(OPND,t * 10 + (c - '0'));
counter = 1;
}
else
{
push(OPND,c - '0');
counter++; //第二位也是数字,则counter变为1,弹出原有数字并压入新的数字
}
c = getchar();
}
else
{
counter = 0; //数字结束
switch (Precede(top(OPTR), c))
{
case '<':
push(OPTR,c);
c = getchar();
break;
case '=':
pop(OPTR);
c = getchar();
break;
case '>':
char theta = top(OPTR);
pop(OPTR);
double a = top(OPND);
pop(OPND);
double b = top(OPND);
pop(OPND);
push(OPND,calculate(b, theta, a));
}
}
}
return top(OPND);
}

template<class T>
void clearstack(T s) //清空栈
{
while (!empty(s))
pop(s);
}


int main()
{
Slink OPND;
SlinkC OPTR;
InitStack(OPND);
InitStack(OPTR);
int t;
cin >> t;
getchar();
while (t--)
{
clearstack(OPND);
clearstack(OPTR);
double ans = getAnswer(OPTR,OPND);
cout << ans << endl<< endl;
getchar();
}
return 0;
}

未使用STL,正常做法

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
#include<iostream>
#include<cstdio>
#include<cctype>
#include<stack>

#pragma GCC optimize(2)

using namespace std;

stack<char> OPTR;
stack<double> OPND;

int getIndex(char theta) //将运算符转为下标
{
int index = 0;
switch (theta)
{
case '+':
index = 0;
break;
case '-':
index = 1;
break;
case '*':
index = 2;
break;
case '/':
index = 3;
break;
case '(':
index = 4;
break;
case ')':
index = 5;
break;
case '#':
index = 6;
default:break;
}
return index;
}

char Precede(char theta1, char theta2) //计算优先度
{
const char priority[][7] =
{
{ '>','>','<','<','<','>','>' },
{ '>','>','<','<','<','>','>' },
{ '>','>','>','>','<','>','>' },
{ '>','>','>','>','<','>','>' },
{ '<','<','<','<','<','=','0' },
{ '>','>','>','>','0','>','>' },
{ '<','<','<','<','<','0','=' },
};

int index1 = getIndex(theta1);
int index2 = getIndex(theta2);
return priority[index1][index2];
}

double calculate(double b, char theta, double a) //根据theta计算结果
{
switch (theta)
{
case '+':
return b + a;
case '-':
return b - a;
case '*':
return b * a;
case '/':
return b / a;
default:
break;
}
}

double getAnswer()
{
OPTR.push('#');
int counter = 0;
char c = getchar();
while (c != '#' || OPTR.top() != '#')
{
if (isdigit(c)) //如果是数字则进入OPND栈中
{
if (counter == 1) //可能为两位数或者是更多位数,所以要先判断前一个数是否为数字
{
double t = OPND.top();
OPND.pop();
OPND.push(t * 10 + (c - '0'));
counter = 1;
}
else
{
OPND.push(c - '0');
counter++;
}
c = getchar();
}
else //如果是运算符,则判断
{
counter = 0;
switch (Precede(OPTR.top(), c))
{
case '<':
OPTR.push(c);
c = getchar();
break;
case '=':
OPTR.pop();
c = getchar();
break;
case '>':
char theta = OPTR.top();
OPTR.pop();
double a = OPND.top(); //计算a与b运算的值,并将运算得到的结果压入栈中
OPND.pop();
double b = OPND.top();
OPND.pop(); //弹出a,b,并将运算结果存入
OPND.push(calculate(b,theta,a));
}
}
}
return OPND.top();
}

void clearstack() //清空栈
{
while (!OPTR.empty())
OPTR.pop();
while (!OPND.empty())
OPND.pop();
}

int main()
{
int t;
cin >> t;
getchar();
while (t--)
{
clearstack();
double ans = getAnswer();
cout << ans << endl<< endl;
getchar();
}
return 0;
}
逆波兰表达式、波兰表达式