一.头文件及定义
#include <stack> //栈的头文件
#include<queue >//队列头文件
stack<int> s;//定义了int类型的栈
queue<int>q;//定义了int类型的队列
栈的相关函数
s.empty() 如果栈为空返回true,否则返回false
s.size() 返回栈中元素的个数
s.pop() 删除栈顶元素但不返回其值
s.top() 返回栈顶的元素,但不删除该元素
s.push() 在栈顶压入新元素
栈操作
#include<bits/stdc++.h>
using namespace std;
stack<int> s;//定义了一个int类型的栈,栈的名称为s
int main()
{
printf("栈是否为空:%d\n",s.empty());
s.push(1);
printf("栈是否为空:%d\n",s.empty());
printf("栈中元素个数是:%d\n",s.size());
printf("栈顶元素是:%d\n",s.top());
s.push(3);
printf("栈中元素个数是:%d\n",s.size());
printf("栈顶元素是:%d\n",s.top());
s.pop();
s.push(4);
s.push(5);
printf("栈中元素个数是:%d\n",s.size());
printf("栈顶元素是:%d\n",s.top());
return 0;
}
队列相关函数
q.empty() 如果队列为空返回true,否则返回false
q.size() 返回队列中元素的个数
q.pop() 删除队列首元素但不返回其值
q.front() 返回队首元素的值,但不删除该元素
q.push() 在队尾压入新元素
q.back() 返回队列尾元素的值,但不删除该元素
队列操作
#include<bits/stdc++.h>
using namespace std;
queue<int> q;//定义了一个int类型的队列,栈的名称为q
int main()
{
printf("队列是否为空:%d\n",q.empty());
q.push(1);
printf("队列是否为空:%d\n",q.empty());
printf("队列中元素个数是:%d\n",q.size());
printf("队首元素是:%d\n",q.front());
q.push(3);
printf("队列中元素个数是:%d\n",q.size());
printf("队首元素是:%d\n",q.front());
q.pop();
q.push(4);
q.push(5);
printf("队列中元素个数是:%d\n",q.size());
printf("队首元素是:%d\n",q.front());
return 0;
}