附加判定标志的循环队列的基本操作

#include <iostream>
using namespace std;
#define MAXSIZE 100
typedef struct
{
	int *base;
	int front;
	int rear;
	int tag;
}SqQueue;

int InitQueue(SqQueue &Q)
{
	Q.base=new int[MAXSIZE];
	if(!Q.base)return 0;
	Q.front=Q.rear=0;
	Q.tag=0;
	return 1;
 }
 
int EnQueue(SqQueue &Q,int e) 
{
	if((Q.tag==1)&&(Q.rear==Q.front))	
		return 0;
	Q.base[Q.rear]=e;
	Q.rear=(Q.rear+1)%MAXSIZE;
	if(Q.tag==0) Q.tag=1;
	return 1; 
}

int DeQueue(SqQueue &Q,int &e)//&
{
	if(Q.tag==0&&Q.front==Q.rear)	return 0;
	e=Q.base[Q.front];
	Q.front=(Q.front+1)%MAXSIZE;
	return 1; 
}

int IsEmpty(SqQueue &Q)
{
	if(Q.front==Q.rear)	return 0;
	else return 1;
}

int main()
{
	int n,m;int a=0,b=0;
	while(1)
	{
		cin>>n;m=n;
		if(n==0)	break;
		SqQueue Q;
		InitQueue(Q);
		while(n!=0)
		{
			cin>>a;
			EnQueue(Q,a);
			n--;
		 } 
		while(m!=0)
		{
			DeQueue(Q,b);
			cout<<b;
			if(m!=1)cout<<" ";
			m--;
		}
		//cout<<IsEmpty(Q);
		cout<<endl;
	}
 }

描述

假设以数组Q[m]存放循环队列中的元素, 同时设置一个标志tag,以tag== 0和tag == 1来区别在队头指针(front)和队尾指针(rear)相等时,队列状态为“空”还是“满”。试编写与此结构相应的插入(enqueue)和删除(dlqueue)算法。

输入

多组数据,每组数据有两行。第一行为一个整数n,n表示入队序列A的长度(n个数依次连续入队,中间没有出队的情况),第二行为序列A(空格分隔的n个整数)。当n=0时,输入结束。

输出

对应每组数据输出一行。依次输出队列中所有的整数,每两个整数之间用空格分隔。

输入样例 1 

4
1 2 3 4
5
1 2 4 5 3
0

输出样例 1

1 2 3 4
1 2 4 5 3
发布了100 篇原创文章 · 获赞 4 · 访问量 3685

猜你喜欢

转载自blog.csdn.net/weixin_43673589/article/details/104391958