#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;
}
}
}
循环链表队列
最新推荐文章于 2022-08-25 14:10:30 发布