include <iostream>
using namespace std;
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef struct QNode {
ElemType data;
struct QNode* Next;
}QNode,*QueuePtr;
typedef struct {
QueuePtr front;
QueuePtr rear;
}LinkQueue;
int InitQueue(LinkQueue& Q) {
Q.front = Q.rear = new QNode;
Q.front->Next = NULL;
return OK;
}
int EnQueue(LinkQueue& Q, ElemType e) {
QueuePtr p;
p = new QNode;
p->data = e;
p->Next = NULL;
Q.rear->Next = p;
Q.rear = p;
return OK;
}
int DeQueue(LinkQueue& Q, ElemType& e) {
if (Q.front == Q.rear) return ERROR;
QueuePtr p;
p = Q.front->Next;
e = p->data;
Q.front->Next = p->Next;
delete p;
return OK;
}
int GetHead(LinkQueue Q) {
if (Q.front != Q.rear)
return Q.front->Next->data;
else{
cout << "错误" << endl;
return ERROR;
}
}
int main()
{
cout << "0、退出程序" << endl;
cout << "1、队列入队" << endl;
cout << "2、队列出队" << endl;
cout << "3、取头元素" << endl;
cout << "---------------------------" << endl;
LinkQueue lq;
InitQueue(lq);
int num,n,status,elem,temp;
while (true) {
cout << "你的选择:" << endl;
cin >> num;
cout << "---------------------------" << endl;
switch (num)
{
case 1:
cout << "你想插入的数:";
cin >> n;
status = EnQueue(lq, n);
if (status == 1) {
cout << " 插入成功。" << endl;
cout << "---------------------------" << endl;
continue;
}
else {
cout << "操作失败!" << endl;
cout << "---------------------------" << endl;
continue;
}
case 2:
status = DeQueue(lq, temp);
if (status == 1) {
cout << "出队成功。"<< endl;
cout << "---------------------------" << endl;
continue;
}
else {
cout << "操作失败!" << endl;
cout << "---------------------------" << endl;
continue;
}
case 3:
elem = GetHead(lq);
if (elem) {
cout << "当前元素为" << elem << endl;
cout << "---------------------------" << endl;
continue;
}
else {
cout << "操作失败!" << endl;
cout << "---------------------------" << endl;
continue;
}
case 0:
break;
default:
cout << "输入错误!" << endl;
cout << "---------------------------" << endl;
continue;
}
break;
}
}
标签:QNode,队列,LinkQueue,Next,int,front,rear
From: https://www.cnblogs.com/qianyuzz/p/17059921.html