基于循环链表的队列的基本操作

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

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

int DeQueue(SqQueue &Q,int &e)//&
{
	if(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;
		if(n==0&&m==0)	break;
		SqQueue Q;
		InitQueue(Q);
		while(n!=0)
		{
			cin>>a;
			EnQueue(Q,a);
			n--;
		 } 
		while(m!=0)
		{
			DeQueue(Q,b);
			cout<<b;
			cout<<" ";
			m--;
		}
		cout<<IsEmpty(Q);
		cout<<endl;
	}
 }

描述

用带头结点的循环链表表示队列,并且只设一个指针指向队尾元素结点(不设头指针)。实现该队列的入队出队以及判断队列是否为空操作。

输入

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

输出

对应每组数据输出一行。每行包括m+1个整数,前m个数代表出队序列B的各个整数,最后一个整数表示队列是否为空,队列为空输出0,不为空输出1。整数之间用空格分隔。

输入样例 1 

5 3
1 3 5 3 6
4 4
-1 2 3 4
0 0

输出样例 1

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

猜你喜欢

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