#include<iostream>
#include <stdlib.h>
using namespace std;
#define OK 1
typedef struct QNode{
int data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
//链队列初始化
int InitQueue(LinkQueue &Q) {
Q.rear=Q.front=new QNode;
Q.front->next=NULL;
return 1;
}
//连队列的销毁
int DestroyQueue(LinkQueue &Q){
QueuePtr p;
while(Q.front)
{
p=Q.front->next;
delete(Q.front);
Q.front=p;
//或者
/*Q.rear=Q.front->next;
delete(Q.front);
Q.front=Q.rear;
*/
}
return OK;
}
//入队在队尾
int EnQueue(LinkQueue &Q,int e)
{
QueuePtr p;
p=new QNode;
//准备好p节点
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}
//出队,
int DeQueue(LinkQueue &Q,int &e)
{
QueuePtr p=new QNode;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p) Q.rear=Q.front;
delete p;
return OK;
}
//函数功能:按照先进先出原则对队列进行打印
int main()
{
LinkQueue Q;
InitQueue(Q);
int n;
int e;
cout<<"Do you want to append a new node(Y/N)?";
char c;
cin>>c;
while (c == 'Y' || c == 'y')
{ cout<<"请输入你的入队数据";
cin>>e;
EnQueue(Q,e); /* 入队 */
cout<<"Do you want to append a new node(Y/N)?";
cin>>c;
}
//显示队列中元素
QueuePtr p=Q.front->next;
while(p!=NULL)
{
cout<<p->data<<endl;
p=p->next;
}
cout<<"Do you want to delete node(Y/N)?";
cin>>c;
while (c == 'Y' || c == 'y')
{
cout<<"开始出队"<<endl;
DeQueue(Q,e); /* 出队 */
cout<<"出队元素为"<<e; /* 按先进先出对队列进行打印 */
printf("Do you want to delete node(Y/N)?");
cin>>c;
}
cout<<"ok"<<endl;
return 0;
}