【计算机专业复试机试】 七:线性数据结构--队列quene与栈stack



七、线性数据结构–队列quene与栈stack

7.1 队列queue的使用

队列:先进先出
//C++的STL容器提供
#include<queue>
//创建队列
queue<int> myQueue
//队尾入队
myQueue.push(x)
//队首出队
myQueue.pop()
//front队首元素 back队尾元素
int a  = myQueue.front();
int b = myQueue.back();
//empty判断队列是否为空
myQueue.empty()
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<queue>

using namespace std;

int main() {
	queue<int> myQueue;
	if (myQueue.empty()) {
		printf("myQueue is empty!\n");
	}
	for (int i = 0; i < 10; i++) {
		myQueue.push(i);
		printf("front = %d , back = %d \n", myQueue.front(), myQueue.back());
	}
	printf("--------------------------\n");
	for (int i = 0; i < 9; i++) {
		myQueue.pop();
		printf("front = %d , back = %d \n", myQueue.front(), myQueue.back());
	}
	return 0;
}

7.2 约瑟夫问题

在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<queue>

using namespace std;

int main() {
	queue<int> MyQueue;
	int n, p, m;
	while (scanf("%d%d%d", &n, &p, &m) != EOF) {
		if (n == 0 && p == 0 && m == 0) {
			break;
		}
		for (int i = 1; i <= n; i++) {
			MyQueue.push(i);
		}
		for (int i = 1; i < p; i++) {
			int tmp = MyQueue.front();
			MyQueue.pop();
			MyQueue.push(tmp);
		}
		int say = 1;
		int now;
		while (!MyQueue.empty()) {
			now = MyQueue.front();
			if (say != m) {
				MyQueue.pop();
				MyQueue.push(now);
				say++;
			}
			else {
				if (MyQueue.size() == 1) {
					printf("%d\n", now);
				}
				else {
					printf("%d,", now);
				}
				MyQueue.pop();	
				say = 1;
			}
		}
	}
	return 0;
}

7.3 栈stack的使用

栈是一种操作受限的数据结构
特地那是后进先出 
栈底与栈顶:栈顶入栈、栈顶出栈
应用:深度优先遍历、表达式解析、递归

栈的使用

#include<stack>
//建立栈
stack<int> myStack;
//push()入栈、pop()弹栈、top()获取栈顶元素
myStack.push(i);
myStack.pop();
myStack.top();
//empty()判断是否为空
myStack.empty()
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stack>

using namespace std;

int main() {
	stack<int> myStack;
	if (myStack.empty()) {
		printf("myStack is empty!\n");
	}
	for (int i = 0; i < 10; i++) {
		myStack.push(i);
		printf("myStack top = %d\n", myStack.top());
	}
	printf("-------------\n");
	for (int i = 0; i < 9; i++) {
		myStack.pop();
		printf("myStack top = %d\n", myStack.top());
	}
	return 0;
}

7.4 编排字符串

在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string>
#include<algorithm>
#include<stack>

using namespace std;

int main() {
	int n;
	scanf("%d", &n);
	stack<string> myStack;
	for (int i = 0; i < n; i++) {
		char strArr[100] = { 0 };
		scanf("%s", strArr);
		string str = strArr;
		myStack.push(str);
		stack<string> tmpStack = myStack;
		for (int j = 1; j <= min(4, int(myStack.size())); j++) {
			printf("%d=%s ", j, tmpStack.top().c_str());
			tmpStack.pop();
		}
		printf("\n");
	}
	return 0;
}

7.5 括号匹配

在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string>
#include<stack>

using namespace std;

int main() {
	stack<char> myStack;
	char strArr[1000] = { 0 };
	scanf("%s", strArr);
	for (int i = 0; strArr[i] != '\0'; i++) {
		if (strArr[i] == '<' || strArr[i] == '[' || strArr[i] == '{') {
			myStack.push(strArr[i]);
		}
		else if (strArr[i] == '>') {
			if (myStack.empty() || myStack.top() != '<') {
				printf("NO\n");
				return 0;
			}
			else {
				myStack.pop();
			}
		}
		else if (strArr[i] == ']') {
			if (myStack.empty() || myStack.top() != '[') {
				printf("NO\n");
				return 0;
			}
			else {
				myStack.pop();
			}
		}
		else if (strArr[i] == '}') {
			if (myStack.empty() || myStack.top() != '{') {
				printf("NO\n");
				return 0;
			}
			else {
				myStack.pop();
			}
		}
	}
	if (myStack.empty()) {
		printf("YES\n");
	}
	else {
		printf("NO\n");
	}
}

7.6 计算表达式

在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string>
#include<stack>
#include<map>

using namespace std;

int main() {
	//运算符优先级map
	map<char, int> priority = {
		{'\0',0},
		{'+',1},{'-',1},
		{'*',2},{'/',2}
	};
	stack<double> numStack;	//操作数栈
	stack<char> opStack;	//运算符栈
	char str[1000] = { 0 };
	while (scanf("%s", str) != EOF) {
		string numStr = "";
		for (int i = 0; ; i++) {
			if (str[i] >= '0' && str[i] <= '9') {
				numStr.push_back(str[i]);
			}
			else {
				double num = stod(numStr);
				numStr = "";
				numStack.push(num);
				//如果操作符栈为空则入栈, 后操作符优先级>前操作符优先级 入栈
				if (opStack.empty()|| priority[str[i]] > priority[opStack.top()]) {
					opStack.push(str[i]);
				}
				else {
					//后操作符优先级<=前操作符优先级 弹栈  进行运算 ,并一直检测优先级
					while (!opStack.empty() && priority[str[i]] <= priority[opStack.top()]) {
						double rhs = numStack.top();
						numStack.pop();
						double lhs = numStack.top();
						numStack.pop();
						if (opStack.top() == '+') {
							numStack.push(lhs + rhs);
						}else if (opStack.top() == '-') {
							numStack.push(lhs - rhs);
						}
						else if (opStack.top() == '*') {
							numStack.push(lhs * rhs);
						}
						else if (opStack.top() == '/') {
							numStack.push(lhs / rhs);
						}
						opStack.pop();
					}
					//将后面的新操作符入栈
					if (str[i] == '\0') {
						printf("%d\n", (int)numStack.top());
						break;
					}
					else {
						opStack.push(str[i]);
					}
				}
			}
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一叶怎知秋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值