#include<iostream>
#define SIZE 10
using namespace std;
typedef int Type;
typedef struct qnode{
Type data;
qnode *next;
}qnode,*queue_pp;
typedef struct {
queue_pp front;
queue_pp rear;
int qlen;
}linkqueue;
int initqueue(linkqueue &q){ //構造空隊列
q.front = q.rear = new qnode;
if(!q.front) return 0;
q.front->next = NULL;
q.qlen = 0;
return 1;
}
int destroyqueue(linkqueue &q) { //撤銷隊列
while(q.front) {
q.rear = q.front->next;
delete q.front;
q.front = q.rear;
}
return 1;
}
int insertqueue(linkqueue &q,Type e) { //插入元素e為新的隊尾元素
qnode *p;
p = new qnode;
if(!p) return 0;
p->data = e; p->next = NULL;
q.rear->next = p;
q.rear = p;
++q.qlen;
return 1;
}
int deletequeue(linkqueue &q,Type e) { //刪除隊頭元素
if(q.front == q.rear) return 0;
qnode *p;
p = q.front->next;
e = p->data;
q.front->next = p->next;
if(q.rear == p) q.rear = q.front;
delete q.front;
--q.qlen;
return 1;
}
int queueexist(linkqueue &q) {return q.front == 0;} //隊列是否存在
int queueclear(linkqueue &q) { //置為空白隊
qnode *e;
e = q.front->next;
delete q.front;
q.front = e;
}
int queueempty(linkqueue &q){return q.front == q.rear;} //隊列空為真
int queuelength(linkqueue q){cout<<q.qlen<<endl;}//隊列元素個數
int queueshow(linkqueue &q) //輸出隊列元素
{
if(queueempty(q) || queueexist(q))
cout<<"隊中沒有元素"<<endl;
else {
qnode *p;
p = q.front->next;
while(p) {
cout<<p->data<<" ";
p = p->next;}
}
}
int main()
{
cout<<"□□□□隊列基本操作□□□□"<<endl;
cout<<"□□□ 1.建立空隊列 □□□□"<<endl;
cout<<"□□□ 2.插入隊列 □□□□□"<<endl;
cout<<"□□□ 3.刪除隊頭元素 □□□"<<endl;
cout<<"□□□ 4.返回隊列長度 □□□"<<endl;
cout<<"□□□ 5.判斷隊列是否為空白 □"<<endl;
cout<<"□□□ 6.清空隊列 □□□□□"<<endl;
cout<<"□□□ 7.撤銷隊列 □□□□□"<<endl;
cout<<"□□□ 8.輸出隊列 □□□□□"<<endl;
cout<<"□□□ 0.退出 □□□□□□□"<<endl;
linkqueue qq;
int ch;
Type e;
while(1) {
cout<<endl;
cout<<"請輸入你的選擇"<<endl;
cin>>ch;
switch(ch)
{
case 1:
initqueue(qq);
cout<<"隊列已建立"<<endl;
break;
case 2:
if(queueexist(qq))break;
cout<<"你入隊元素是:";
cin>>e;
insertqueue(qq,e);
cout<<"隊中元素:";
queueshow(qq);
break;
case 3:
if(queueexist(qq))
break;
deletequeue(qq,e);
cout<<"您刪除的元素是"<<e;
queueshow(qq);
break;
case 4:
queuelength(qq);
break;
case 5:
if(queueempty(qq))
cout<<"隊為空白"<<endl;
else {
cout<<"隊未空"<<endl;}
break;
case 6:
queueclear(qq);
break;
case 7:
destroyqueue(qq);
break;
case 8:
queueshow(qq);
break;
case 0:
exit(0);
break;
default:
cout<<"輸入錯誤,請重新輸入選擇"<<endl;
break;
}
}
system("pause");
return 1;
}