#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
/*
创建队列需要定义两个结构体
1.一个用来保存节点的链式结构
2、一个用来记录队头和队尾
*/
typedef int elementType;
typedef struct QueueListNode
{
elementType value;
struct QueueListNode* next;
}QNode;
typedef struct Queue_List
{
QNode* head;//队头
QNode* tail;//队尾
}Queue;
void print_menu();
void init_queue(Queue* QueueList);
bool is_empty(Queue QueueList);
void out_queue(Queue* QueueList);
void enter_queue(Queue* QueueList);
void print_queue(Queue QueueList);
int getNum_queue(Queue QueueList);
int main()
{
int order = 0;
Queue QueueList;
print_menu();
while (1)
{
printf("请输入操作指令:");
scanf(" %d", &order);
switch (order)
{
case 1:
//初始化队列
init_queue(&QueueList);
break;
case 2:
//判空
is_empty(QueueList);
break;
case 3:
//入队,即队列尾插
enter_queue(&QueueList);
break;
case 4:
//出列,即队列头删
out_queue(&QueueList);
break;
case 5:
//队列遍历
print_queue(QueueList);
break;
case 6:
//获取队列的元素个数
printf("队列有%d个元素\n", getNum_queue(QueueList));
break;
case 7:
//获取头节点元素
is_empty(QueueList) ? ptintf("空队列\n") : printf("队列头元素为:%d\n", QueueList.head->value);
break;
case 8:
//获取尾节点元素
is_empty(QueueList) ? ptintf("空队列\n") : printf("队列尾元素为:%d\n", QueueList.tail->value);
break;
case 9:
//退出
return;
default:
printf("输入指令有误,请重新输入\n");
break;
}
}
return 0;
}
void print_menu()
{
printf("操作指令如下:\n");
printf("1----初始化空队列\n");
printf("2----判断队列是否为空\n");
printf("3----入列(队尾插入)\n");
printf("4----出列(队头删除)\n");
printf("5----遍历队列\n");
printf("6----获取队列中元素个数\n");
printf("7----获取队头元素\n");
printf("8----获取队尾元素\n");
printf("9----退出\n");
}
//初始化空队列
void init_queue(Queue* QueueList)
{
assert(QueueList);
QueueList->head = NULL;
QueueList->tail = NULL;
printf("初始化成功\n");
}
//判空 空返回1 否则返回0
bool is_empty(Queue QueueList)
{
/*if (QueueList.tail == NULL)
{
return true;
}
return false;*/
//优化写法
return QueueList.tail == NULL;
}
//入队,尾插
void enter_queue(Queue* QueueList)
{
QNode* newnode = (QNode*)malloc(sizeof(QNode));
assert(newnode);
memset(newnode, 0, sizeof(QNode));
elementType val;
printf("请输入要入队的元素值:");
scanf(" %d", &val);
newnode->value = val;
newnode->next = NULL;
if (is_empty(*QueueList))
{
//如果队列是空
QueueList->head = newnode;
QueueList->tail = newnode;
}
else
{
//队列非空,尾插
QueueList->tail->next = newnode;//将新节点插到尾部
QueueList->tail = newnode;//更新尾节点
}
printf("入队成功\n");
}
//出队 头删
void out_queue(Queue* QueueList)
{
if (is_empty(*QueueList))
{
printf("队列为空,无法出列\n");
return;
}
QNode* temp = QueueList->head->next;//保存次头节点
if (temp == NULL)
{
//说明队列里面只有一个元素 删完后就为空队列
free(QueueList->head);
QueueList->head = NULL;
QueueList->tail = NULL;
}
else
{
free(QueueList->head);
QueueList->head = temp;
}
printf("出列成功\n");
}
//打印遍历队列
void print_queue(Queue QueueList)
{
if (is_empty(QueueList))
{
printf("空队列,无法打印\n");
return;
}
QNode* temp = QueueList.head;
while (temp != NULL)
{
printf(" %d ", temp->value);
temp = temp->next;
}
printf("\n");
}
//获取队列的个数
int getNum_queue(Queue QueueList)
{
int count = 0;
if (is_empty(QueueList))
{
return count;
}
QNode* temp = QueueList.head;
while (temp != NULL)
{
count++;
temp = temp->next;
}
return count;
}
C语言数据结构编程练习-通过单链表实现队列的操作
于 2025-01-21 20:20:38 首次发布