循环链表队列

#include <iostream>
using namespace std;

#define maxIndex 5

struct QueueNode{
	int data;
	QueueNode* next;
};

QueueNode *head = NULL;
QueueNode *tail = NULL;

int curQueueCount = 0;
int AddNode()
{
	if(curQueueCount==maxIndex)
	{
		cout<<"队列已满"<<endl;
		return -1;
	}
	cout<<"请输入一个队列值:";
	cin>>tail->data;
	if(curQueueCount==0)
	{	
		head=tail;
		tail=tail->next;
	}
	else
	{
		tail=tail->next;
	}
	curQueueCount++;
	return 0;
}

int DeleteNode()
{
	if(curQueueCount==0)
	{
		cout<<"队列为空,无法删除"<<endl;
		return -1;
	}
	QueueNode *temp;
	temp=head;
	head=head->next;
	delete temp;
	curQueueCount--;
}

int createQueue()
{
	int i=1;
	QueueNode* temp;
	temp=new QueueNode;
	temp->next=NULL;
	head=temp;
	tail=temp;
	while(i<maxIndex)
	{
		temp=new QueueNode;
		temp->next=NULL;
		tail->next=temp;
		tail=temp;
		i++;
	}
	tail->next=head;
	tail=head;
	return 0;
}

int output()
{
	QueueNode *temp;
	temp=head;
	int i=0;
	while(i<curQueueCount)
	{
		cout<<temp->data<<" ";
		i++;
		temp=temp->next;
	}
	cout<<endl;
	return 0;
}

int main()
{
	bool keepOn;
	int choose;
	keepOn = true;
	createQueue();
	while(keepOn)
	{
		cout<<"1.添加元素"<<endl;
		cout<<"2.删除元素"<<endl;
		cout<<"3.输出队列"<<endl;
		cout<<"4.退出"<<endl;
		cin>>choose;
		switch(choose)
		{
			case 1:AddNode();break;
			case 2:DeleteNode();break;
			case 3:output();break;
			case 4:keepOn=false;break;

			default:cout<<"输入错误"<<endl;
				
		}
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值