对于队列的链式存储的练习
/*****************************************
* 对于队列的链式存储结构的练习
* 主要包括 队列的初始化
* 队列的判空和判满
* 入队
* 出队
* 获取队头元素
*****************************************/
#include "stdio.h"
#include "stdlib.h"
#define MAX_SIZE 10
typedef int ElemType;
typedef struct QNode{
ElemType data;
struct QNode* next;
};
typedef struct {
QNode* front;
QNode* rear;
}LinkQueue;
void showMenu();
bool initQueue(LinkQueue &linkQueue);
bool isEmptyQueue(LinkQueue &linkQueue);
bool enQueue(LinkQueue &linkQueue,int value);
bool outQueue(LinkQueue &linkQueue, int &outValue);
bool getTopValue(LinkQueue &linkQueue,int &topValue);
void getValues(LinkQueue &linkQueue);
int main() {
bool isExit = false;
//定义队列
LinkQueue linkQueue;
while (!isExit) {
showMenu();
int option;
scanf("%d", &option);
switch (option) {
case 1:
//初始化队列
if (initQueue(linkQueue)) {
printf("初始化队列成功\n");
}
break;
case 2:
//判断队列是否为空
if (isEmptyQueue(linkQueue)) {
printf("当前队列为空\n");
} else {
printf("当前队列非空\n");
}
break;
case 3:
//入队
printf("请输入要入队的元素");
int value;
scanf("%d", &value);
if (enQueue(linkQueue, value)) {
printf("入队成功\n");
} else {
printf("入队失败\n");
}
break;
case 4:
//出队
int outValue;
if (outQueue(linkQueue, outValue)) {
printf("出队成功 %d\n", outValue);
} else {
printf("出队失败\n");
}
break;
case 5: {
//获取队头元素
int topValue;
if (getTopValue(linkQueue, topValue)) {
printf("队头元素为 %d\n", topValue);
} else {
printf("获取队头元素失败\n");
}
break;
}
case 6:
//获取队中的所有的元素
getValues(linkQueue);
break;
case 7:
isExit = true;
break;
default:
printf("输入错误请重新输入\n");
break;
}
}
}
/**
* 查看当前队列中的所有的元素
*/
void getValues(LinkQueue &linkQueue){
//定义辅助指针
QNode* qNode = linkQueue.front->next;
printf("队列中的值为 ");
while (qNode){
printf(" %d",qNode->data);
qNode = qNode->next;
}
printf("\n");
}
/**
* 查看对头的元素
*/
bool getTopValue(LinkQueue &linkQueue,int &topValue){
if(!isEmptyQueue(linkQueue)){
topValue = linkQueue.front->next->data;
return true;
}
return false;
}
/**
* 出队操作
*/
bool outQueue(LinkQueue &linkQueue, int &outValue){
if(!isEmptyQueue(linkQueue)){
//该队列不为空,则进行出队操作
//定义辅助指针
QNode* qNode = linkQueue.front->next;
outValue = qNode->data;
linkQueue.front->next = qNode->next;
if(qNode == linkQueue.rear){
//如果队列中只有一个元素
//此时应当让队尾指针指向队头,否则可能将队尾指针释放掉
linkQueue.rear = linkQueue.front;
}
free(qNode);
return true;
}
return false;
}
/**
* 入队操作
*入队操作主要是采用单链表的尾插法即可
*/
bool enQueue(LinkQueue &linkQueue,int value){
//创建队列结点
QNode* qNode = (QNode*)malloc(sizeof(QNode));
qNode->data = value;
qNode->next = nullptr;
linkQueue.rear->next = qNode;
linkQueue.rear = qNode;
return true;
}
/**
* 判断队列是否为空
* 当队列的头指针和尾指针都指向同一个位置的时候可认定该队列为空
*/
bool isEmptyQueue(LinkQueue &linkQueue) {
if(linkQueue.front == linkQueue.rear){
return true;
}
return false;
}
/**
* 队列的链式存储结构的初始化
* 本次练习的是带头结点的队列
* 因此在初始化的时候要创建好头结点
* @param linkQueue 队列
* @return
*/
bool initQueue(LinkQueue &linkQueue) {
//创建头结点
QNode* qNode = (QNode*)malloc(sizeof(QNode));
//初始化头结点
qNode->data = 0;
qNode->next = nullptr;
linkQueue.front = qNode;
linkQueue.rear = qNode;
return true;
}
void showMenu() {
printf(" 对于队列的链式存储结构的操作\n");
printf("1 初始化队列\n");
printf("2 判断队列是否为空\n");
printf("3 入队\n");
printf("4 出队\n");
printf("5 获取对头元素\n");
printf("6 获取队列中的所有的元素\n");
printf("7 退出");
printf("请输入你的选择 ");
}