队列的简洁版(只要理解三个函数,你就会掌握队列)

在这里插入图片描述
我们可以将队列想成一个·线型的管道
在这里插入图片描述
所以我们要制造一个管道及需要队列初始化函数,管道需要进入东西,这需要入队函数,管道需要排除(释放)东西,这需要出队函数
请看代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<cstdlib>
using namespace std;
#define SIZE 20
typedef struct Data
{
	char info;
}DATA;
typedef struct queue
{
	DATA queues[SIZE];
	int rear;//队尾 
	int front;//队头 
}Queue;
Queue *InitQueue( )//初始化队列 
{
	Queue *p;
	if((p=(Queue *)malloc(sizeof(Queue)))==NULL)
	{
		cout<<"分配内存失败"<<endl; 
		exit(1);
	}
	p->rear=0;
	p->front=0;
	return p;
}
void InQueue(Queue *p,char information )//入队(我们要考虑的是否队列已满) 
{
	if(p->rear==SIZE)
	{
		cout<<"队列已满"<<endl; 
	}
	else
	{
		p->queues[p->rear++].info=information;//希望各位客官能够明白(理解)p->rear++的作用
		//改代码先执行p->queues[p->rear]=information 然后执行p->rear加1 
	}
}
void OutQueue(Queue *p)//出队(我们要考虑的是否队列为空) 
{
	 if(p->rear==p->front)
	 {
	 	cout<<"队列为空"<<endl; 
	 }
	 else
	 {
	 	cout<<p->queues[p->front++].info<<endl;
	 }
} 
int main()
{
	Queue *p;
	char information;
	p=InitQueue();
	cout<<"请输入十个字符"<<endl;
	for(int i=0;i<10;i++)
	{
		cin>>information;
		InQueue(p,information);
	} 
	cout<<"出队结果"<<endl; 
	for(int i=0;i<10;i++)
	{
		OutQueue(p);
	}
	cout<<"这就是先进先出(后进后出)"<<endl;
	return 0;
} 

在这里插入图片描述
想到队列我们要想到队列先入先出(后入后出)的特点。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值