【栈 与 队列】

2017 7 25    栈  队列

  

1、栈的输入 输出:

      简单的栈:

代码:

#include<cstdio>
#include<stack>
#include<algorithm>
using namespace std;
int main()
{
	stack<int > sta;
	sta.push(3);
	sta.push(5);
	int x = sta.top();
	printf("%d\n",x);
	sta.pop();
	x = sta.top();
	printf("%d\n",x);
	return 0;
}

       栈之于结构体、数组:

代码:

#include<cstdio>
#include<stack>
#include<algorithm>
using namespace std;
struct node       //node相当于int 
{
	char name[20];
	int age;
}p[1000];        //定义了一个数组,可输入多组数据 
int main()
{
	int i,n;
	scanf("%d\n",&n);
	stack<node > s;   //定义一个栈 s 
	for(i=0;i<n;i++)
	{
		scanf("%s %d",p[i].name,&p[i].age); //先将结构体内的所有成员输入 
		s.push(p[i]);                       //将结构体存入已经定义好的栈中 
	} 
	for(i=0;i<n;i++)
	{
		node one=s.top();     //相当于int one             
		printf("%s %d\n",one.name,one.age);
		s.pop();	 //记得输出一组完了要跳出 
	}
	return 0;
}





2、队列:(与栈同理)

       简单的队列:

代码:

#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
int main()
{
	queue<int > que;
	que.push(3);
	que.push(5);
	int x = que.front();
	printf("%d\n",x);
	que.pop();
	x = que.front();
	printf("%d\n",x);
	return 0;
}

       队列之于结构体、数组:

代码:

#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
struct node
{
	char name[20];
	int age;
}p[1000];
int main()
{
	int i,n;
	scanf("%d\n",&n);
	queue<node > s;
	for(i=0;i<n;i++)
	{
		scanf("%s %d",p[i].name,&p[i].age);
		s.push(p[i]);
	} 
	for(i=0;i<n;i++)
	{
		node one=s.front();
		printf("%s %d\n",one.name,one.age);
		s.pop();	
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值