news 2026/6/23 19:06:08

Linux环境下的C语言编程(四十)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Linux环境下的C语言编程(四十)

一、链队列

使用链表实现的队列,动态分配内存。

1. 结构定义

#include <stdio.h> #include <stdlib.h> // 链队列节点 typedef struct QueueNode { int data; struct QueueNode* next; } QueueNode; // 链队列 typedef struct { QueueNode* front; // 队头指针 QueueNode* rear; // 队尾指针 } LinkedQueue;

2. 基本操作实现

// 初始化队列 void initLinkedQueue(LinkedQueue* q) { q->front = q->rear = NULL; } // 判断队列是否为空 int isEmptyLinkedQueue(LinkedQueue* q) { return q->front == NULL; } // 入队操作 int enqueueLinked(LinkedQueue* q, int value) { QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode)); if (!newNode) { printf("内存分配失败!\n"); return 0; } newNode->data = value; newNode->next = NULL; if (isEmptyLinkedQueue(q)) { // 队列为空,新节点既是队头也是队尾 q->front = q->rear = newNode; } else { // 队列不为空,插入到队尾 q->rear->next = newNode; q->rear = newNode; } return 1; } // 出队操作 int dequeueLinked(LinkedQueue* q, int* value) { if (isEmptyLinkedQueue(q)) { printf("队列为空,无法出队!\n"); return 0; } QueueNode* temp = q->front; *value = temp->data; q->front = q->front->next; // 如果出队后队列为空,需要重置rear if (q->front == NULL) { q->rear = NULL; } free(temp); return 1; } // 获取队头元素 int getFrontLinked(LinkedQueue* q, int* value) { if (isEmptyLinkedQueue(q)) { printf("队列为空!\n"); return 0; } *value = q->front->data; return 1; } // 获取队列长度 int getLengthLinked(LinkedQueue* q) { int length = 0; QueueNode* current = q->front; while (current != NULL) { length++; current = current->next; } return length; } // 打印队列 void printLinkedQueue(LinkedQueue* q) { if (isEmptyLinkedQueue(q)) { printf("队列为空!\n"); return; } printf("队列内容(队头->队尾):"); QueueNode* current = q->front; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } // 销毁队列 void destroyLinkedQueue(LinkedQueue* q) { int value; while (!isEmptyLinkedQueue(q)) { dequeueLinked(q, &value); } }

3. 链队列特点

  • 优点:动态增长,不会溢出(除非内存不足)

  • 缺点:每个节点需要额外指针空间,内存开销较大

二、循环队列

使用数组实现的队列,通过循环利用空间解决假溢出问题。

1. 结构定义

#define MAX_SIZE 5 // 队列最大容量 typedef struct { int data[MAX_SIZE]; int front; // 队头指针 int rear; // 队尾指针 } CircularQueue;

2. 基本操作实现

// 初始化队列 void initCircularQueue(CircularQueue* q) { q->front = 0; q->rear = 0; } // 判断队列是否为空 int isEmptyCircularQueue(CircularQueue* q) { return q->front == q->rear; } // 判断队列是否已满 int isFullCircularQueue(CircularQueue* q) { return (q->rear + 1) % MAX_SIZE == q->front; } // 入队操作 int enqueueCircular(CircularQueue* q, int value) { if (isFullCircularQueue(q)) { printf("队列已满,无法入队!\n"); return 0; } q->data[q->rear] = value; q->rear = (q->rear + 1) % MAX_SIZE; return 1; } // 出队操作 int dequeueCircular(CircularQueue* q, int* value) { if (isEmptyCircularQueue(q)) { printf("队列为空,无法出队!\n"); return 0; } *value = q->data[q->front]; q->front = (q->front + 1) % MAX_SIZE; return 1; } // 获取队头元素 int getFrontCircular(CircularQueue* q, int* value) { if (isEmptyCircularQueue(q)) { printf("队列为空!\n"); return 0; } *value = q->data[q->front]; return 1; } // 获取队列长度 int getLengthCircular(CircularQueue* q) { return (q->rear - q->front + MAX_SIZE) % MAX_SIZE; } // 打印队列 void printCircularQueue(CircularQueue* q) { if (isEmptyCircularQueue(q)) { printf("队列为空!\n"); return; } printf("队列内容(队头->队尾):"); int i = q->front; while (i != q->rear) { printf("%d ", q->data[i]); i = (i + 1) % MAX_SIZE; } printf("\n"); }

3. 循环队列特点

  • 优点:内存连续,访问速度快,内存开销小

  • 缺点:固定大小,需要预分配空间

三、两种队列的比较

特性链队列循环队列
存储结构链表数组
内存分配动态静态/固定
空间利用率较低(有指针开销)较高
最大容量理论上无限制固定大小
操作时间复杂度O(1)O(1)
适用场景大小不确定的队列大小确定的队列

四、使用示例

int main() { printf("=== 链队列测试 ===\n"); LinkedQueue lq; initLinkedQueue(&lq); // 入队测试 enqueueLinked(&lq, 10); enqueueLinked(&lq, 20); enqueueLinked(&lq, 30); printLinkedQueue(&lq); // 出队测试 int value; dequeueLinked(&lq, &value); printf("出队元素:%d\n", value); printLinkedQueue(&lq); printf("\n=== 循环队列测试 ===\n"); CircularQueue cq; initCircularQueue(&cq); // 入队测试 enqueueCircular(&cq, 1); enqueueCircular(&cq, 2); enqueueCircular(&cq, 3); enqueueCircular(&cq, 4); printCircularQueue(&cq); // 队列满测试 if (!enqueueCircular(&cq, 5)) { printf("队列已满,5无法入队\n"); } // 出队测试 dequeueCircular(&cq, &value); printf("出队元素:%d\n", value); printCircularQueue(&cq); // 再次入队 enqueueCircular(&cq, 5); printCircularQueue(&cq); // 销毁链队列 destroyLinkedQueue(&lq); return 0; }
区别使用
  1. 选择链队列

    • 队列大小不确定或变化较大

    • 内存充足,更注重灵活性

    • 不需要随机访问队列元素

  2. 选择循环队列

    • 队列大小固定或可预估

    • 对性能要求高

    • 需要连续内存存储

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/19 1:01:44

矮冬瓜矮砧密植:水肥一体化系统铺设全攻略

瓜地里&#xff0c;老陈的矮冬瓜长得圆润均匀&#xff0c;挂果整齐。“这套水肥系统让我种瓜省心不少&#xff0c;”他指着藤蔓下的滴灌带说&#xff0c;“不仅瓜形周正&#xff0c;产量还提高了四成。”认识矮冬瓜矮砧密植矮冬瓜矮砧密植&#xff0c;简单说就是选择矮蔓品种&a…

作者头像 李华
网站建设 2026/6/20 2:54:27

P11960 [GESP202503 五级] 平均分配

难度普及/提高− 题目描述 小 A 有 2n 件物品&#xff0c;小 B 和小 C 想从小 A 手上买走这些物品。对于第 i 件物品&#xff0c;小 B 会以 bi​ 的价格购买&#xff0c;而小 C 会以 ci​ 的价格购买。为了平均分配这 2n 件物品&#xff0c;小 A 决定小 B 和小 C 各自只能买走…

作者头像 李华
网站建设 2026/6/23 18:24:25

PINNs-Torch:实现9倍加速的物理信息神经网络框架

技术挑战&#xff1a;PINNs在工程应用中面临的计算瓶颈 【免费下载链接】pinns-torch PINNs-Torch, Physics-informed Neural Networks (PINNs) implemented in PyTorch. 项目地址: https://gitcode.com/gh_mirrors/pi/pinns-torch 物理信息神经网络&#xff08;PINNs&a…

作者头像 李华
网站建设 2026/6/22 18:20:33

ChromePass:三分钟掌握Chrome密码提取的终极指南

ChromePass&#xff1a;三分钟掌握Chrome密码提取的终极指南 【免费下载链接】chromepass Get all passwords stored by Chrome on WINDOWS. 项目地址: https://gitcode.com/gh_mirrors/chr/chromepass 还在为忘记网站密码而烦恼吗&#xff1f;ChromePass这款开源神器让…

作者头像 李华
网站建设 2026/6/19 19:56:57

【方法】IP66.net:如何查到自己的IP?

一、直接登录网站&#xff0c;直接显示&#xff08;所有设备适用&#xff09; 1、浏览器直接搜索“我的IP的地址查询” 方法&#xff1a; ① 打开任意浏览器&#xff0c;都行&#xff0c;自带的也可以&#xff0c;有网就可以 ② 在搜索栏输入“我的IP的地址查询”并回车 ③ …

作者头像 李华