循环队列

假设将循环队列定义为:以域变量rear和length分别指示循环队列中队尾元素的位置和内含元素的个数。编写相应的入队列和出队列的程序,并判断循环队列是否队满(在出队列的算法中要返回队头元素)。

假设队列数组为Queue[MAXSIZE],第一行输入队列大小N,第二行开始输入若干入队元素,队满时,停止入队。第三行输入出队元素。

输出入队出队操作后的循环队列,并返回出队元素的队头元素。

5

3 4 6 2 7

4

6 2 7

6

#include<iostream>
#include<stdio.h>
#include<stdlib.h>

using namespace std;
struct linklist
{
	struct linklist *next;
	int data;
};
bool isdigi(char j)
{
	return (j >= '0' && j <= '9');
}
struct linkqueue
{
	struct linklist *rear;
	struct linklist *head;
	int length;
}linq;
void init()
{
	linq.length = 0;
	linq.rear = NULL;
}
void popl()
{
	if(linq.head->next == linq.head)
	{
		linq.length = 0;
		linq.rear = NULL;
		linq.head = NULL;
	}
	else
	{
		linq.length--;
		linq.rear->next = linq.head->next;
		linq.head = linq.head->next;
	}
}
void pushl(int x)
{
	struct linklist *newblock;
	newblock = (struct linklist *)malloc(sizeof(struct linklist));
	newblock->data = x;
	if(linq.rear == NULL)
	{
		linq.rear = newblock;
		linq.head = newblock;
		linq.rear->next = linq.rear;
	}
	else
	{
		newblock->next = linq.head;
		linq.rear->next = newblock;
		linq.rear = newblock;
	}
	linq.length+=1;
}
int main()
{
	struct linklist *lq;
	int num,data;
	char m;
	cin>>num;
	init();
	m = getchar();
	m = getchar();
	for(;m != '\n';)
	{
		if(isdigi(m) && linq.length < num)
		{
			pushl((int)(m-48));
		}
		m = getchar();
	}
	if(num <= linq.length)
	{
		getchar();
		getchar();
		getchar();
		getchar();
	}
	else
	{
		getchar();
		getchar();
		getchar();
	}
	cin >> data;
	for(;;)
	{
		if(linq.head->data != data)
		{
			popl();
		}
		else
		{
			popl();
			break;
		}
	}
	lq = linq.head;
	if(lq == NULL)
	{
		cout<<endl;
	}
	else
	{
		for(;lq->next != linq.head;)
		{
			cout<< lq->data << ' ';
			lq = lq->next;
		}
		cout<< lq->data <<endl;
	}
	if(lq == NULL)
	{
		cout<<endl;
	}
	else
	{
		cout<<linq.head->data<<endl;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值