谨以此文记录链队列的实现:
#include <iostream>
using namespace std;
typedef int QElemType;
typedef struct QNode
{
QElemType data;
QNode *next;
} QNode, *QueuePtr;
typedef struct
{
QueuePtr front = nullptr, rear = nullptr;
} LinkQueue;
void printUI()
{
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 << "********* 9.初始化并创建队列 *********" << endl;
cout << "********* 10.输出队列元素 *********" << endl;
cout << "********* 11.退出 *********" << endl;
cout << "*******************************************************" << endl;
}
/**
* @brief 初始化
*
* @param Q
* @return true
* @return false
*/
bool InitQueue(LinkQueue &Q)
{
Q.front = Q.rear = new QNode;
Q.front->next = nullptr;
return true;
}
/**
* @brief 销毁队列
*
* @param Q
* @return true
* @return false
*/
bool DestroyQueue(LinkQueue &Q)
{
while (Q.front)
{
Q.rear = Q.front->next;
delete Q.front;
Q.front = Q.rear;
}
return true;
}
/**
* @brief 判断队列是否为空
*
* @param Q
* @return true
* @return false
*/
bool QueueEmpty(LinkQueue Q)
{
return Q.front == Q.rear;
}
/**
* @brief 获取头节点
*
* @param Q
* @param e
* @return true
* @return false
*/
bool GetHead(LinkQueue Q, QElemType &e)
{
if (Q.front == Q.rear)
return false;
e = Q.front->next->data;
return true;
}
/**
* @brief 入队
*
* @param Q
* @param e
* @return true
* @return false
*/
bool EnQueue(LinkQueue &Q, QElemType e)
{
QNode *p = new QNode;
p->data = e;
p->next = nullptr;
Q.rear->next = p;
Q.rear = p;
return true;
}
/**
* @brief 出队
*
* @param Q
* @param e
* @return true
* @return false
*/
bool DeQueue(LinkQueue &Q, QElemType &e)
{
if (Q.front == Q.rear)
return false;
QNode *p = Q.front->next;
e = p->data;
Q.front->next = p->next;
if (Q.rear == p)
Q.rear = Q.front;
delete p;
return true;
}
/**
* @brief 清空队列
*
* @param Q
* @return true
* @return false
*/
bool ClearQueue(LinkQueue &Q)
{
while (Q.front != Q.rear)
{
QueuePtr p = Q.front->next;
Q.front->next = p->next;
if (Q.rear == p)
Q.rear = Q.front;
delete p;
}
return true;
}
/**
* @brief 队列元素个数
*
* @param Q
* @return int
*/
int QueueLength(LinkQueue Q)
{
int i = 0;
QueuePtr p = Q.front;
while (p->next)
{
i++;
p = p->next;
}
return i;
}
/**
* @brief 输出队列元素
*
* @param Q
*/
void PrintElem(LinkQueue Q)
{
QueuePtr p = Q.front;
while (p->next)
{
cout << p->next->data << " ";
p = p->next;
}
cout << endl;
}
int main()
{
int mode;
QElemType e;
LinkQueue Q;
printUI();
while (1)
{
cout << "请输入选择:";
cin >> mode;
if (mode == 11)
break;
if (mode < 1 || mode > 11)
continue;
if ((!Q.front || !Q.rear) && mode != 1 && mode != 9)
{
cout << "尚未初始化" << endl;
continue;
}
switch (mode)
{
case 1:
if (InitQueue(Q))
cout << "初始化成功" << endl;
break;
case 2:
if (DestroyQueue(Q))
cout << "销毁成功" << endl;
break;
case 3:
if (ClearQueue(Q))
cout << "清空成功" << endl;
break;
case 4:
if (QueueEmpty(Q))
cout << "队列为空" << endl;
else
cout << "队列不为空" << endl;
break;
case 5:
cout << "队列元素个数为" << QueueLength(Q) << endl;
break;
case 6:
if (GetHead(Q, e))
cout << "队列头节点为" << e << endl;
else
cout << "获取头节点失败" << endl;
break;
case 7:
cout << "请输入您要插入的元素:";
cin >> e;
if (EnQueue(Q, e))
cout << "插入成功" << endl;
else
cout << "插入失败" << endl;
break;
case 8:
if (DeQueue(Q, e))
cout << "队头元素" << e << "已出队" << endl;
else
cout << "出队失败" << endl;
break;
case 9:
int n;
InitQueue(Q);
cout << "请输入队列元素个数:";
cin >> n;
cout << "请输入元素:";
while (n--)
{
cin >> e;
EnQueue(Q, e);
}
cout << "初始化创建成功" << endl;
break;
case 10:
PrintElem(Q);
break;
}
}
return 0;
}