当前位置: 首页 > news >正文

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. 选择循环队列

    • 队列大小固定或可预估

    • 对性能要求高

    • 需要连续内存存储

http://www.cnnetsun.cn/news/23892.html

相关文章:

  • 矮冬瓜矮砧密植:水肥一体化系统铺设全攻略
  • P11960 [GESP202503 五级] 平均分配
  • PINNs-Torch:实现9倍加速的物理信息神经网络框架
  • GPT-5.2发布!这些超强新功能,快来看看它是怎么让你的工作更轻松的!
  • ChromePass:三分钟掌握Chrome密码提取的终极指南
  • 【方法】IP66.net:如何查到自己的IP?
  • 南京大学开源SteadyDancer模型实现完美动作迁移,首帧保留彻底解决身份漂移难题
  • 机器视觉相机参数
  • springboot基于vue的观赏鱼养殖互助商城系统的设计与实现_1vlf0334
  • 压差式静力水准仪液体选择必看!从充液到排气:沉降监测系统安装全流程避雷手册
  • 构建可靠数据库连接:人大金仓JDBC驱动8.6.0实战指南
  • 嵌入式零基础到就业年班
  • 如何快速提取Chrome密码:跨平台开源工具完整指南
  • 5分钟掌握RichTextKit:SwiftUI富文本编辑器终极指南
  • 如何有效准备编程竞赛?五个阶段科学备考方法
  • BG3模组管理器终极指南:5分钟快速上手博德之门3模组管理
  • 6、黑客必备:Linux 网络技能与软件管理
  • Font Awesome 7全面解析:现代化图标解决方案的革新之路
  • MySQL业务数据量增长到单表成为瓶颈时,该如何做?
  • 13、Linux 系统日志处理与服务使用技巧
  • Paperzz 论文查重:从 “重复率焦虑” 到 “合规清晰”,学术新人如何用工具搞定论文的 “终稿安检”
  • Bananas屏幕共享:3分钟学会零门槛跨平台协作
  • 使用二进制文件方式部署kubernetes(1)
  • 如何在Mac上安装KeyCastr:5步搞定按键可视化工具
  • 小学生学C++编程 (位运算精讲)
  • 鸿蒙投屏工具HOScrcpy深度实战:突破传统镜像的进阶玩法
  • 基于MATLAB的胃癌检测实现方案
  • 图像分割新利器:预训练骨干网络快速构建高质量分割模型
  • 论文重复率 / AI 率双超?paperxie 的 “精准优化” 功能:如何在不碰专业内容的前提下过检测?
  • 36、Linux 系统安全防护全攻略