From fe6ba4f5e2f9210c13199c44d91edd133d5a998b Mon Sep 17 00:00:00 2001 From: lwy Date: Fri, 31 Mar 2023 15:34:24 +0800 Subject: [PATCH] add LinkQueue.c --- LinearList/LinkList.c | 1 + Queue/LinkQueue.c | 61 +++++++++++++++++++++++++++++++++++++++++++ main.c | 53 +++++++++++++++++++++++++++++-------- 3 files changed, 104 insertions(+), 11 deletions(-) create mode 100644 Queue/LinkQueue.c diff --git a/LinearList/LinkList.c b/LinearList/LinkList.c index 2e4abc6..8f9621f 100644 --- a/LinearList/LinkList.c +++ b/LinearList/LinkList.c @@ -5,6 +5,7 @@ typedef struct ListNode { int data; struct ListNode *next; } ListNode; + //typedef ListNode *LinkList; //LinkList head; //ListNode *p; diff --git a/Queue/LinkQueue.c b/Queue/LinkQueue.c new file mode 100644 index 0000000..966b44e --- /dev/null +++ b/Queue/LinkQueue.c @@ -0,0 +1,61 @@ +#include +#include + +typedef struct Node { + int data; // 存储队列元素 + struct Node *next; // 指向下一个节点的指针 +} QNode, *QueuePtr; + +typedef struct { + QueuePtr front; // 队列头指针,指向队列头节点 + QueuePtr rear; // 队列尾指针,指向队列尾节点的下一个位置 +} LinkQueue; + +// 初始化队列 +void initQueue(LinkQueue *queue) { + queue->front = queue->rear = (QueuePtr) malloc(sizeof(QNode)); // 创建头节点 + if (!queue->front) { + exit(-1); // 内存分配失败,退出程序 + } + queue->front->next = NULL; // 头节点的下一个节点为空 +} + +// 判断队列是否为空 +int queueEmpty(LinkQueue *queue) { + if (queue->front == queue->rear) { + return 1; // 队列为空 + } else { + return 0; // 队列不为空 + } +} + +// 入队操作 +void enQueue(LinkQueue *queue, int x) { + QueuePtr p = (QueuePtr) malloc(sizeof(QNode)); // 创建一个新节点 + if (!p) { + exit(-1); // 内存分配失败,退出程序 + } + p->data = x; // 新节点存储元素值 + p->next = NULL; // 新节点的下一个节点为空 + + queue->rear->next = p; // 将新节点插入队尾 + queue->rear = p; // 修改队尾指针 +} + +// 出队操作 +int deQueue(LinkQueue *queue) { + if (queueEmpty(queue)) { + exit(-1); // 队列为空,无法出队,退出程序 + } + + QueuePtr p = queue->front->next; // 获取队头节点 + int x = p->data; // 获取队头节点的元素值 + + queue->front->next = p->next; // 将队头节点从队列中删除 + if (queue->rear == p) { // 如果队头节点是队列中的最后一个节点 + queue->rear = queue->front; // 修改队尾指针 + } + free(p); // 释放队头节点的内存 + + return x; // 返回队头元素值 +} diff --git a/main.c b/main.c index 8ea00d8..22d135d 100644 --- a/main.c +++ b/main.c @@ -3,28 +3,59 @@ //#include "LinearList/LinkList.c" //#include "Stack/SeqStack.c" //#include "Stack/LinkStack.c" -#include "Queue/SeqQueue.c" +//#include "Queue/SeqQueue.c" +#include "Queue/LinkQueue.c" int main() { - //顺序队列 - SeqQueue seqQueue; - initQueue(&seqQueue); - enQueue(&seqQueue,1); - enQueue(&seqQueue,2); -// deQueue(&seqQueue); +// int a =20; +// int *p =&a; +// printf("%d--",*p); + + LinkQueue queue; // 定义一个链队列 + initQueue(&queue); // 初始化队列 + + // 入队操作,向队列中添加元素 + enQueue(&queue, 1); + enQueue(&queue, 2); + enQueue(&queue, 3); + + // 判断队列是否为空 + if (queueEmpty(&queue)) { + printf("Queue is empty\n"); + } else { + printf("Queue is not empty\n"); + } - int x = deQueue(&seqQueue); + // 出队操作,从队列中删除元素 + int x = deQueue(&queue); printf("Delete element: %d\n", x); - int y = deQueue(&seqQueue); - printf("Delete element: %d\n", y); + // 再次判断队列是否为空 - if (queueEmpty(&seqQueue)) { + if (queueEmpty(&queue)) { printf("Queue is empty\n"); } else { printf("Queue is not empty\n"); } + //顺序队列 +// SeqQueue seqQueue; +// initQueue(&seqQueue); +// enQueue(&seqQueue,1); +// enQueue(&seqQueue,2); +//// deQueue(&seqQueue); +// +// int x = deQueue(&seqQueue); +// printf("Delete element: %d\n", x); +// int y = deQueue(&seqQueue); +// printf("Delete element: %d\n", y); +// // 再次判断队列是否为空 +// if (queueEmpty(&seqQueue)) { +// printf("Queue is empty\n"); +// } else { +// printf("Queue is not empty\n"); +// } + //链栈 // LinkStack *stack = NULL; // stack = push(stack, 'a');