栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

stack容器和queue容器

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

stack容器和queue容器

stack是一个先进后出的数据结构
栈只有顶端元素才可以被外界使用
因此栈不允许有遍历行为 

#include
#include
using namespace std;
void test()
{
	stacks;
	s.push(1);
	s.push(2);
	s.push(3);
	s.push(4);
	s.push(5);
	cout << "栈的大小为:" << s.size() << endl;
	while(!s.empty())
	{
		cout << "栈顶元素为:" << s.top() << "  ";
		s.pop();
		cout << "栈的大小为:" << s.size() << endl;
	}
	s.push(888);
	cout << "栈顶元素为:" << s.top() << "  ";
	cout << "栈的大小为:" << s.size() << endl;
}
int main()
{
	test();
	return 0; 
}

queue是一种先进先出的数据接口
队列容器允许从一端新增元素,从另一端移除元素
队列只有队头和队尾可以被外界使用,因此队列不允许有遍历行为  

#include
#include 
using namespace std;
void test()
{
	queueq;
	for(int i = 0;i < 5;i++)
	{
		q.push(i);
	}
	while(!q.empty())
	{
		cout << "第一个元素:" << q.front() << endl;
		cout << "最后一个元素:" << q.back() << endl;
		cout << "栈的大小为:" << q.size() << endl;
		//移除队头元素
		q.pop();
		cout << "移除队头元素后队头元素为:" << q.front() << endl; 
		cout << "移除队头元素后栈的大小为:" << q.size() << endl;
		cout << endl;
	}
	//将移除元素后的栈q拷贝给q1
	queueq1(q); 
	cout << "移除后:" << endl; 
	if(q1.empty())
	{
		cout << "栈q1为空!" << endl;
	}
	else
	{
		cout << "栈不为空!" << endl;
		cout << "第一个元素:" << q.front() << endl;
		cout << "最后一个元素:" << q.back() << endl;
		cout << "栈的大小为:" << q.size() << endl; 
	} 
}
int main()
{
	test();
	return 0; 
} 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/702799.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号