news 2026/5/26 15:40:58

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/5/21 7:58:20

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

一、队列时间复杂度分析1. 链队列时间复杂度// 链队列节点 typedef struct QueueNode {int data;struct QueueNode* next; // 额外指针开销 } QueueNode;// 链队列结构 typedef struct {QueueNode* front; // 队头指针QueueNode* rear; // 队尾指针 } LinkedQueue;入队操作…

作者头像 李华
网站建设 2026/5/25 8:37:43

java计算机毕业设计人事管理系统的设计与实现 基于SpringBoot的教职工综合信息管理平台 面向高校的人事与薪酬一体化服务系统

计算机毕业设计人事管理系统的设计与实现49zx59&#xff08;配套有源码 程序 mysql数据库 论文&#xff09; 本套源码可以在文本联xi,先看具体系统功能演示视频领取&#xff0c;可分享源码参考。 高校人事科每天都在“三线作战”&#xff1a;纸质档案翻查、Excel工资条拼接、微…

作者头像 李华
网站建设 2026/5/25 17:40:07

DNA和蛋白质序列分析

DNA和蛋白质序列分析DNA和蛋白质序列分析是生物学研究中关键的技术手段&#xff0c;涉及通过测序、比对和解析基因组DNA以及由基因编码的蛋白质序列&#xff0c;进而揭示生命体的遗传信息及其生物学功能。DNA序列分析主要用于解读基因组中携带的遗传信息&#xff0c;包括基因突…

作者头像 李华
网站建设 2026/5/24 14:13:37

终极指南:3步彻底卸载Windows 10 OneDrive的完整方案

终极指南&#xff1a;3步彻底卸载Windows 10 OneDrive的完整方案 【免费下载链接】OneDrive-Uninstaller Batch script to completely uninstall OneDrive in Windows 10 项目地址: https://gitcode.com/gh_mirrors/one/OneDrive-Uninstaller 你是否曾经遇到过这样的困扰…

作者头像 李华