栈和队列简单介绍

1 栈

1.1介绍

栈是一种类似桶的数据结构,因为最先进入栈的元素像被压在桶底的物品一样,只有拿掉在其之后放入的物品,才能将其取出(FILO

严格来讲就是进栈、退栈
在这里插入图片描述

1.2 函数库

栈的标准库叫stack

1.2.1 声明方式

	stack</*元素类型*/int> /*栈名*/sta;
	stack<int> tmp;

1.2.2 操作函数

1.empty()
返回本栈是否为空

	bool n=/*栈名*/sta.empty();
	bool tmpn=tmp.empty();

2.size()
返回栈中的元素集合长度

	int n=/*栈名*/sta.size();
	int tmpn=tmp.size();

3.top()
返回栈顶元素

	int n=/*栈名*/sta.top();
	int tmpn=tmp.top();

4.push()
将某元素插入栈顶

	/*栈名*/sta.push(n);
	tmp.push(tmpn);

5.emplace()
栈顶增加一个元素

	/*栈名*/sta.emplace(n);
	tmp.emplace(tmpn);

6.pop()
栈顶元素出栈

	/*栈名*/sta.pop();
	tmp.pop();

7.swap()
交换两个栈的元素

	/*栈名*/sta.swap(/*栈名*/tmp);
	tmp.swap(sta);

1.3 例题

后缀表达式求值
描述
不包含括号,运算符放在两个运算对象的后面,所有的计算按运算符出现的顺序,严格从左向右进行,不再考虑运算符的优先规则,如:(2 + 1) × 3 , 即2 1 + 3 ×。这样的表达式称为后缀表达式,也叫逆波兰表达式。它是为了方便在计算机中进行表达式求值而出现的。

给出一个仅由整数 、+、-、*、/等组成的后缀表达式,符号之间用空格分开,计算它的值。/ 表示整除。
输入样例
2 1 + 3 *
输出样例
9
数据范围
字符串长100000,
代码

#include <iostream>
#include <cstring>
#include <stack>
using namespace std;
int main() {
    
    
	stack<int> s;
	char c[100005];
	gets(c);
	int n=strlen(c),t;
	for(int i=0;i<n;i++) {
    
    
		if(c[i]>='0' && c[i]<='9') {
    
    //循环读数
			t=c[i]-'0';
			while(c[++i]>='0' && c[i]<='9') {
    
    
				t*=10;
				t+=c[i]-'0';
			}
			s.push(t);
		}
		//做运算
		else if(c[i]=='+') {
    
    
			int a=s.top();
			s.pop();
			int b=s.top();
			s.pop();
			s.push(a+b);
		}
		else if(c[i]=='-') {
    
    
			int a=s.top();
			s.pop();
			int b=s.top();
			s.pop();
			s.push(b-a);//b-a
		}
		else if(c[i]=='*') {
    
    
			int a=s.top();
			s.pop();
			int b=s.top();
			s.pop();
			s.push(a*b);
		}
		else if(c[i]=='/') {
    
    
			int a=s.top();
			s.pop();
			int b=s.top();
			s.pop();
			s.push(b/a);//b/a
		}
	}
	cout<<s.top();
	return 0;
}

2 队列

2.1 介绍

队列是一种类似管道的数据结构,因为最先进入队列的元素像在管道中物品一样,第几个进去,就是第几个出来,先进去的先出来(FIFO

也就是入队和出队[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hBoeQuWL-1615788324923)(file:///C:/Users/HF01/Desktop/aHR0cHM6Ly9zczAuYmRzdGF0aWMuY29tLzcwY0Z2SFNoX1ExWW54R2twb1dLMUhGNmhoeS9pdC91PTMzMjg1MzI0ODgsNDE0NTk5NjEyNCZmbT0yNiZncD0wLmpwZw%20%28553%C3%97184%29.png#pic_center)]

在这里插入图片描述

2.2 函数库

队列的标准库是queue

2.2.1 声明方式

	queue</*元素类型*/int> /*队名*/que;
	queue<int> tmp;

1.2.2 操作函数

1.empty()
返回本队是否为空

	bool n=/*队名*/que.empty();
	bool tmpn=tmp.empty();

2.size()
返回队中的元素集合长度

	int n=/*队名*/que.size();
	int tmpn=tmp.size();

3.front()
返回队首元素

	int n=/*队名*/que.front();
	int tmpn=tmp.front();

4.back()
返回队尾元素

	int n=/*队名*/que.front();
	int tmpn=tmp.front();

5.push()
将某元素插入队尾

	/*队名*/que.push(n);
	tmp.push(tmpn);
  1. emplace()
    队尾增加一个元素
	/*队名*/que.emplace(n);
	tmp.emplace(tmpn);
  1. pop()
    队首元素出队
	/*队名*/.pop();
	tmp.pop();
  1. swap()
    交换两个队列的元素
	/*队名*/que.swap(/*队名*/tmp);
	tmp.swap(que);

1.3例题

约瑟夫问题
内存限制:128 MiB
时间限制:1000 ms
描述:
已知 n 个人(编号分别为 1. 2. 3. ……. n.)围坐在一张圆桌周围。

从编号为 的人开始报数,数到 的那个人出列;

他的下一个人又从 开始报数,数到 m 的那个人又出列 ……,依此规律重复下去,直到圆桌周围的人全部出列。
输入样例:
9 5
输出样例:
5 1 7 4 3 6 9 2 8
数据范围:
2≤m≤n≤1000
思路:
我们先用队列来模拟这个环
点到第m个人就出队
代码:

#include <iostream>
#include <queue>
using namespace std;
int main() {
    
    
	int n,m,t;
	cin>>n>>m;
	queue<int> q;
	for(int i=1;i<=n;i++) q.push(i);
	for(int i=1;i<=n;i++) {
    
    //和 while(!q.empty()) 等价
		for(int j=1;j<m;j++) {
    
    //把队首的人置入队尾
			t=q.front();
			q.pop();
			q.push(t);
		}
		//第 m 个人出队
		cout<<q.front()<<' ';
		q.pop();
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_49692699/article/details/107822937