主要为栈和队列
#include <stack> //提供栈的操作.
#include <queue> //提供队列(允许在一端添加元素,并在另一端删除元素)数据结构的实现.
#include <cstdio> //CSP复赛判题头文件.
栈:
//又名堆栈,是限定在表尾进行插入和输出的列表,元素先进后出.
//入栈:
push();
//出栈:
pop();
//代码:
#include<iostream>
#include<string>
using namespace std;
//定义栈.
const int N=1e5+1;
int stk[N+10];
int top=0;//定义在全局自动默认为0,为方便理解写了=0,实际写时可不写.
void push(int x){ //入栈.
top;
stk[top]=x;
}
void pop(){ //出栈.
top--;
}
bool empty(){ //判断栈是否为空.
return top==0;
}
int size(){ //返回栈元素的数量.
return top;
}
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i){
string s;
int x;
cin>>s;
if(s=="push"){
cin>>x;
push(x);
for(int i=1;i<=top;i++) cout<<stk[i]<<" ";
cout<<"\n";
}
else if(s=="pop"){
if(empty()) cout<<"pop fail"<<"\n";
else pop();
}
cout<<endl;
}
return 0;
}
//函数:
stack<DataType>
//声明栈.
stk.push(daata);
//将data入栈.
stk.top();
//弹出栈顶元素.
stk.empty();
//判断栈是否为空,返回true为空,false为非空.
stk.size();
//返回栈中元素个数.
stk.top();
//获取栈顶元素.
//队列中元素无法遍历(直接用系统函数).
队列:
//入队:
push();
//出队:
pop();
//代码:
#include<iostream>
using namespace std;
const int N=1e5+1;
int q[N];//创建队列.
int head=0;//队头下标.
int tail=0;//队头下标.
void push(int x){ //入队.
q[tail]=x;
tail++;
}
void pop(){ //退队.
head++;
}
int front(){ //获取队头元素.
return q[head];
}
int back(){ //获取队尾元素.
return q[tail-1];
}
int size(){ //获取队列元素个数.
return tail-head;
}
bool empty(){ //判断队列是否为空.
return head==tail;
}
int main(){