数据结构那点事--队列(顺序结构)

#include<iostream>
using namespace std;
/**********************


队列的顺序表现形式于链表的顺序的表现形式相同

在队列中比较重要的是对队列的空满的判断,以及队列长度的计算
首先,空满的判断
 1.定义flat,在当为空队列时,即front==rear ,flat=0;
            在当为满队列时,即front==rear ,flat=1;
 2.比较常用的方法:总是在队列满时,数组中还有一个空单元,(rear+1)%QueueSize==front
其次, 队列长度的判断
        (rear-front+QueueSize)%QueueSize 
 **********************/
 //队列的链式存储结构
 #define MAXSIZE 10
 #define OK 1
 #define ERROR 0
typedef int SElemType;
typedef int Status;
typedef struct 
{
 	SElemType data[MAXSIZE];
	int front;
	int rear; 
}SQueue;
//初始化 
Status InitQueue(SQueue *S)
{
	S->front=0;
	S->rear=0;
	return OK;
}
//求队列的长度 
Status QueueLength(SQueue *S,SElemType *e)
{
	*e=(S->rear-S->front+MAXSIZE)%MAXSIZE;
	return OK;
}
//入队列
Status EnQueue(SQueue *S,SElemType e)
{
	if((S->rear+1)%MAXSIZE==S->front)
	{
		cout<<"队满"<<endl;
		return ERROR;
	}
	 S->data[S->rear]=e;
	 S->rear=(S->rear+1)%MAXSIZE;
	 return OK; 
 }  
//出队列
Status DeQueue(SQueue *S,SElemType *e)
{
		if(S->rear==S->front)
	{
		cout<<"队空"<<endl;
		return ERROR;
	}
	*e=S->data[S->front];
	S->front=(S->front+1)%MAXSIZE;
	return OK;
} 
int main()
{
	SQueue S;
	InitQueue(&S);
	cout<<"入队元素的数目:"<<endl;
	int m,n;
	cin>>m;
	for(int i=0;i<m;i++)
	{
		cin>>n;
		EnQueue(&S,n);
	 }
	 int h;
	  int e=1; 
	QueueLength(&S,&e);
	cout<<"队列元素:"; 
	 while(S.front!=S.rear)
	 {
	 	QueueLength(&S,&e);
	 	//cout<<"队列长度:"<<e; 
	 	DeQueue(&S,&h);
	 	cout<<h<<" ";
	 }
	 cout<<endl;
 } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值