基础的栈:
#include<iostream>
#include<stack>
#include<stdio.h>
using namespace std;
int main(void)
{
stack<double>s;//定义一个栈
for(int i=0;i<10;i++)
s.push(i);
while(!s.empty())
{
printf("%lf\n",s.top());
s.pop();
}
cout<<"栈内的元素的个数为:"<<s.size()<<endl;
return 0;
}
用栈进行10进制转8进制:
#include<stdio.h>
#include<stack>
using namespace std;
stack<int>s;
int main()
{
int N;
scanf("%d",&N);
while(N)
{
s.push(N%8);
//Push(S,N%8);
N/=8;
}
while(!s.empty())
{
//Pop(S,e);
int e=s.top();
printf("%d",e);
s.pop();
}
}
用队列写的,卡片游戏:抛弃开头,把第二张放到后面,打印抛弃的数
#include<cstdio>
#include<queue>
using namespace std;
queue<int>q;
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
q.push(i+1);
}
while(!q.empty())
{
printf("%d ",q.front());
q.pop();
q.push(q.front());
q.pop();
}
return 0;
}