7.1栈的应用
![](https://i-blog.csdnimg.cn/direct/c81990c897fc4ad49f202733a5d6a146.png)
![](https://i-blog.csdnimg.cn/direct/5b782f6970a24181b0244dc3380c404b.png)
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main() {
int n, x;
string action;
cin >> n;
stack<int> s;
for (int i = 0; i < n; i++) {
cin >> action;
if (action == "push") {
cin >> x;
s.push(x);
} else {
if (s.empty()) {
cout << -1 << endl;
} else {
cout << s.top() << endl;
s.pop();
}
}
}
return 0;
}
![](https://i-blog.csdnimg.cn/direct/cfa40bed1056466c8c892d1f836e9917.png)
#include <iostream>
#include <string>
using namespace std;
string toPostfixExpr(string infixExpr) {
string result = "";
result += infixExpr[0];
for (int i = 2; i < infixExpr.length(); i += 4) {
result += " ";
result += infixExpr[i + 2];
result += " ";
result += infixExpr[i];
}
return result;
}
int main() {
string expr;
getline(cin, expr);
cout << toPostfixExpr(expr);
return 0;
}
![](https://i-blog.csdnimg.cn/direct/fbec432959634622aa53f3f6f7d92978.png)
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <stack>
using namespace std;
struct Node {
int num;
char op;
bool isOp;
};
map<char, int> priority;
vector<Node> toNodeVector(string expr) {
vector<Node> nodes;
for (int i = 0; i < expr.length(); i++) {
if (expr[i] == ' ') {
continue;
}
if (expr[i] >= '0' && expr[i] <= '9') {
Node numNode;
numNode.isOp = false;
numNode.num = expr[i] - '0';
nodes.push_back(numNode);
} else {
Node opNode;
opNode.isOp = true;
opNode.op = expr[i];
nodes.push_back(opNode);
}
}
return nodes;
}
vector<Node> toPostfixVector(vector<Node> infix) {
vector<Node> postfix;
stack<Node> opStack;
for (int i = 0; i < infix.size(); i++) {
if (infix[i].isOp) {
while (!opStack.empty() && priority[infix[i].op] <= priority[opStack.top().op]) {
postfix.push_back(opStack.top());
opStack.pop();
}
opStack.push(infix[i]);
} else {
postfix.push_back(infix[i]);
}
}
while (!opStack.empty()) {
postfix.push_back(opStack.top());
opStack.pop();
}
return postfix;
}
int main() {
priority['+'] = priority['-'] = 0;
priority['*'] = priority['/'] = 1;
string expr;
getline(cin, expr);
vector<Node> infix = toNodeVector(expr);
vector<Node> postfix = toPostfixVector(infix);
for (int i = 0; i < postfix.size(); i++) {
if (postfix[i].isOp) {
cout << postfix[i].op;
} else {
cout << postfix[i].num;
}
if (i < (int)postfix.size() - 1) {
cout << " ";
}
}
return 0;
}
![](https://i-blog.csdnimg.cn/direct/6d4fc1f3c5d340f1859b44b105d627ae.png)
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <map>
#include <stack>
using namespace std;
struct Node {
int num;
char op;
bool isOp;
};
map<char, int> priority;
vector<Node> toNodeVector(string expr) {
vector<Node> nodes;
for (int i = 0; i < expr.length(); i++) {
if (expr[i] == ' ') {
continue;
}
if (expr[i] >= '0' && expr[i] <= '9') {
Node numNode;
numNode.isOp = false;
numNode.num = expr[i] - '0';
nodes.push_back(numNode);
} else {
Node opNode;
opNode.isOp = true;
opNode.op = expr[i];
nodes.push_back(opNode);
}
}
return nodes;
}
double eval(vector<Node> postfix) {
stack<double> numStack;
for (int i = 0; i < postfix.size(); i++) {
if (!postfix[i].isOp) {
numStack.push(postfix[i].num);
} else {
double num2 = numStack.top();
numStack.pop();
double num1 = numStack.top();
numStack.pop();
if (postfix[i].op == '+') {
numStack.push(num1 + num2);
} else if (postfix[i].op == '-') {
numStack.push(num1 - num2);
} if (postfix[i].op == '*') {
numStack.push(num1 * num2);
} if (postfix[i].op == '/') {
numStack.push(num1 / num2);
}
}
}
return numStack.top();
}
int main() {
priority['+'] = priority['-'] = 0;
priority['*'] = priority['/'] = 1;
string expr;
getline(cin, expr);
vector<Node> postfix = toNodeVector(expr);
printf("%.2f", eval(postfix));
return 0;
}
![](https://i-blog.csdnimg.cn/direct/1fcf67b1cd47440e921166c7af19b981.png)
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <stack>
using namespace std;
struct Node {
int num;
char op;
bool isOp;
};
map<char, int> priority;
vector<Node> toNodeVector(string expr) {
vector<Node> nodes;
for (int i = 0; i < expr.length(); i++) {
if (expr[i] == ' ') {
continue;
}
if (expr[i] >= '0' && expr[i] <= '9') {
Node numNode;
numNode.isOp = false;
numNode.num = expr[i] - '0';
nodes.push_back(numNode);
} else {
Node opNode;
opNode.isOp = true;
opNode.op = expr[i];
nodes.push_back(opNode);
}
}
return nodes;
}
vector<Node> toPostfixVector(vector<Node> infix) {
vector<Node> postfix;
stack<Node> opStack;
for (int i = 0; i < infix.size(); i++) {
if (infix[i].isOp) {
while (!opStack.empty() && priority[infix[i].op] <= priority[opStack.top().op]) {
postfix.push_back(opStack.top());
opStack.pop();
}
opStack.push(infix[i]);
} else {
postfix.push_back(infix[i]);
}
}
while (!opStack.empty()) {
postfix.push_back(opStack.top());
opStack.pop();
}
return postfix;
}
double eval(vector<Node> postfix) {
stack<double> numStack;
for (int i = 0; i < postfix.size(); i++) {
if (!postfix[i].isOp) {
numStack.push(postfix[i].num);
} else {
double num2 = numStack.top();
numStack.pop();
double num1 = numStack.top();
numStack.pop();
if (postfix[i].op == '+') {
numStack.push(num1 + num2);
} else if (postfix[i].op == '-') {
numStack.push(num1 - num2);
} if (postfix[i].op == '*') {
numStack.push(num1 * num2);
} if (postfix[i].op == '/') {
numStack.push(num1 / num2);
}
}
}
return numStack.top();
}
int main() {
priority['+'] = priority['-'] = 0;
priority['*'] = priority['/'] = 1;
string expr;
getline(cin, expr);
vector<Node> infix = toNodeVector(expr);
vector<Node> postfix = toPostfixVector(infix);
printf("%.2f", eval(postfix));
return 0;
}
7.2队列的应用
![](https://i-blog.csdnimg.cn/direct/55d2a21c6ced40a0891ffa9838181609.png)
![](https://i-blog.csdnimg.cn/direct/9a71c7c03f7c401d9255084e1ea0c6f3.png)
#include <iostream>
#include <string>
#include <queue>
using namespace std;
int main() {
int n, k;
cin >> n;
string action;
queue<int> q;
for (int i = 0; i < n; i++) {
cin >> action;
if (action == "push") {
cin >> k;
q.push(k);
} else {
if (q.empty()) {
cout << -1 << endl;
} else {
cout << q.front() << endl;
q.pop();
}
}
}
return 0;
}
![](https://i-blog.csdnimg.cn/direct/8fb35ce163014faa8be166aecbd387c5.png)
![](https://i-blog.csdnimg.cn/direct/41102bb794cd4e8aa68d26db7ab55a34.png)
#include <cstdio>
#include <queue>
using namespace std;
int main() {
int n, x;
scanf("%d", &n);
queue<int> q;
for (int i = 0; i < n; i++) {
scanf("%d", &x);
q.push(x);
}
while (q.size() > 1) {
int front1 = q.front();
q.pop();
int front2 = q.front();
q.pop();
q.push(front1 + front2);
}
printf("%d", q.front());
return 0;
}
![](https://i-blog.csdnimg.cn/direct/17a13aa006a641e690df30aa6c833d40.png)
![](https://i-blog.csdnimg.cn/direct/21d6a533bee8450fb43d54fa3b196f6a.png)
#include <cstdio>
#include <queue>
using namespace std;
int main() {
int n, k;
scanf("%d%d", &n, &k);
queue<int> q;
for (int i = 1; i <= n; i++) {
q.push(i);
}
while (!q.empty()) {
for (int i = 0; i < k - 1; i++) {
int front = q.front();
q.pop();
q.push(front);
}
printf("%d", q.front());
q.pop();
if (!q.empty()) {
printf(" ");
}
}
return 0;
}
7.3链表处理
![](https://i-blog.csdnimg.cn/direct/278a1ff2022c479b9aeca3de93ca3801.png)
![](https://i-blog.csdnimg.cn/direct/3d28e95eea5b4847bff53736e7cc215f.png)
#include <cstdio>
const int MAXN = 100;
struct Node {
int data, next;
} nodes[MAXN];
int main() {
int n, first, id;
scanf("%d%d", &n, &first);
for (int i = 0; i < n; i++) {
scanf("%d", &id);
scanf("%d%d", &nodes[id].data, &nodes[id].next);
}
int current = first;
while (current != -1) {
printf("%d %d %d\n", current, nodes[current].data, nodes[current].next);
current = nodes[current].next;
}
return 0;
}
![](https://i-blog.csdnimg.cn/direct/e9d1f7bccbc644d780a4290107a179c9.png)
![](https://i-blog.csdnimg.cn/direct/d816f39502c3418f84db0b783bc66a88.png)
#include <cstdio>
const int MAXN = 100;
struct Node {
int data, next;
} nodes[MAXN];
int main() {
int n, first, id;
scanf("%d%d", &n, &first);
for (int i = 0; i < n; i++) {
scanf("%d", &id);
scanf("%d%d", &nodes[id].data, &nodes[id].next);
}
int current = first;
int num=0;
while (current != -1) {
//printf("%d %d %d\n", current, nodes[current].data, nodes[current].next);
current = nodes[current].next;
num++;
}
printf("%d",num);
return 0;
}
![](https://i-blog.csdnimg.cn/direct/e1be1ccdc173415d86b2e40c61a636eb.png)
![](https://i-blog.csdnimg.cn/direct/57ee8a616036416ea13ff08bf242e21a.png)
#include <cstdio>
const int MAXN = 1024;
struct Node {
int data, next;
} nodes[MAXN];
int main() {
int n, first, id;
scanf("%d%d", &n, &first);
for (int i = 0; i < n; i++) {
scanf("%d", &id);
scanf("%d%d", &nodes[id].data, &nodes[id].next);
}
int num=0;
int current = first;
scanf("%d",&num);
for (int i = 0; i < num; i++) {
scanf("%d", &id);
scanf("%d", &nodes[id].data);
nodes[id].next=current;
current=id;
}
while (current != -1) {
printf("%d %d %d\n", current, nodes[current].data, nodes[current].next);
current = nodes[current].next;
}
return 0;
}
![](https://i-blog.csdnimg.cn/direct/287dad67033c40849df40976bea9f3db.png)
![](https://i-blog.csdnimg.cn/direct/dfac5b4d60ac465a9a7ccc4c12480e29.png)
#include <cstdio>
const int MAXN = 100;
struct Node {
int data, next;
} nodes[MAXN];
int main() {
int n, first, k, id;
scanf("%d%d%d", &n, &first, &k);
for (int i = 0; i < n; i++) {
scanf("%d", &id);
scanf("%d%d", &nodes[id].data, &nodes[id].next);
}
int current = first, last = 0;
while (current != -1) {
if (nodes[current].data == k) {
if (current == first) {
first = nodes[current].next;
}
nodes[last].next = nodes[current].next;
current = nodes[last].next;
} else {
last = current;
current = nodes[current].next;
}
}
current = first;
while (current != -1) {
printf("%d %d %d\n", current, nodes[current].data, nodes[current].next);
current = nodes[current].next;
}
return 0;
}
![](https://i-blog.csdnimg.cn/direct/83d1b4a538ca45aa96971d39ec5db4e5.png)
![](https://i-blog.csdnimg.cn/direct/e89514f1792a415db3803b260c535bc9.png)
#include <cstdio>
const int MAXN = 100;
struct Node {
int data, next;
} nodes[MAXN];
int main() {
int n, first, id;
scanf("%d%d", &n, &first);
for (int i = 0; i < n; i++) {
scanf("%d", &id);
scanf("%d%d", &nodes[id].data, &nodes[id].next);
}
int current = first, last = -1;
while (current != -1) {
int next = nodes[current].next;
nodes[current].next = last;
last = current;
current = next;
}
current = last;
while (current != -1) {
printf("%d %d %d\n", current, nodes[current].data, nodes[current].next);
current = nodes[current].next;
}
return 0;
}
![](https://i-blog.csdnimg.cn/direct/4b34b3cd934040508bbe4546ec38a313.png)
![](https://i-blog.csdnimg.cn/direct/8c3d5d1fe5b24345985da58c9d19e135.png)
#include <cstdio>
const int MAXN = 100;
const int MAXV = 1000 + 1;
struct Node {
int data, next;
} nodes[MAXN];
bool needDelete[MAXV] = {false};
int main() {
int n, first, id;
scanf("%d%d", &n, &first);
for (int i = 0; i < n; i++) {
scanf("%d", &id);
scanf("%d%d", &nodes[id].data, &nodes[id].next);
}
int current = first, last = -1;
while (current != -1) {
if (needDelete[nodes[current].data]) {
nodes[last].next = nodes[current].next;
current = nodes[current].next;
} else {
needDelete[nodes[current].data] = true;
last = current;
current = nodes[current].next;
}
}
current = first;
while (current != -1) {
printf("%d %d %d\n", current, nodes[current].data, nodes[current].next);
current = nodes[current].next;
}
return 0;
}
![](https://i-blog.csdnimg.cn/direct/53a97cc47adb4469ba3c1caa24280bbd.png)
![](https://i-blog.csdnimg.cn/direct/045e0efa8cfc43bb87f71e07a20b90c4.png)
#include <cstdio>
const int MAXN = 100;
struct Node {
int data, next;
} nodes[MAXN];
int main() {
int n, first, id;
scanf("%d%d", &n, &first);
for (int i = 0; i < n; i++) {
scanf("%d", &id);
scanf("%d%d", &nodes[id].data, &nodes[id].next);
}
int fast = first, slow = first;
while (nodes[fast].next != -1 && nodes[nodes[fast].next].next != -1) {
slow = nodes[slow].next;
fast = nodes[fast].next;
fast = nodes[fast].next;
}
if (nodes[fast].next == -1) {
printf("%.1f", (double)nodes[slow].data);
} else {
printf("%.1f", (nodes[slow].data + nodes[nodes[slow].next].data) / 2.0);
}
return 0;
}
![](https://i-blog.csdnimg.cn/direct/5a05af56ecbc49719c811d8dd9bc01de.png)
![](https://i-blog.csdnimg.cn/direct/9949df1708d44bc38a823b5c8a84af5a.png)
#include <cstdio>
const int MAXN = 100;
struct Node {
int data, next;
} nodes[MAXN];
int main() {
int n, first, k, id;
scanf("%d%d%d", &n, &first, &k);
for (int i = 0; i < n; i++) {
scanf("%d", &id);
scanf("%d%d", &nodes[id].data, &nodes[id].next);
}
int fast = first;
while (k--) {
fast = nodes[fast].next;
}
int slow = first;
while (fast != -1) {
slow = nodes[slow].next;
fast = nodes[fast].next;
}
printf("%d %d %d", slow, nodes[slow].data, nodes[slow].next);
return 0;
}
![](https://i-blog.csdnimg.cn/direct/a215cd4cb61645178d096cf1cb34ce4e.png)
![](https://i-blog.csdnimg.cn/direct/909e1500bb734952a0ce2ca480ad881e.png)
#include <cstdio>
const int MAXN = 100;
struct Node {
int data, next;
} nodes[MAXN];
int reverseList(int first) {
int current = first, last = -1;
while (current != -1) {
int next = nodes[current].next;
nodes[current].next = last;
last = current;
current = next;
}
return last;
}
bool judgePalindrome(int head1, int head2) {
while (head2 != -1) {
if (nodes[head1].data != nodes[head2].data) {
return false;
}
head1 = nodes[head1].next;
head2 = nodes[head2].next;
}
return true;
}
int main() {
int n, first, id;
scanf("%d%d", &n, &first);
for (int i = 0; i < n; i++) {
scanf("%d", &id);
scanf("%d%d", &nodes[id].data, &nodes[id].next);
}
int fast = first, slow = first;
while (nodes[fast].next != -1 && nodes[nodes[fast].next].next != -1) {
slow = nodes[slow].next;
fast = nodes[fast].next;
fast = nodes[fast].next;
}
int headOfSecondPart = reverseList(nodes[slow].next);
bool isPalindrome = judgePalindrome(first, headOfSecondPart);
nodes[slow].next = reverseList(headOfSecondPart);
printf(isPalindrome ? "Yes" : "No");
return 0;
}