news 2026/6/10 19:25:36

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/10 12:28:12

如何利用Agent Framework新版本构建更智能的AI代理?

Agent Framework作为构建、编排和部署AI代理与多代理工作流的强大框架&#xff0c;支持Python和.NET双语言生态。随着新版本的发布&#xff0c;开发者现在能够更轻松地创建复杂的人工智能应用。本文将为您揭示如何充分利用新版本的优势&#xff0c;构建更智能、更高效的AI代理系…

作者头像 李华
网站建设 2026/6/10 14:28:40

LabelPlus:漫画翻译工具的历史回顾与现代替代方案指南

LabelPlus&#xff1a;漫画翻译工具的历史回顾与现代替代方案指南 【免费下载链接】LabelPlus Easy tool for comic translation. 项目地址: https://gitcode.com/gh_mirrors/la/LabelPlus 在漫画翻译领域&#xff0c;LabelPlus曾经是一款备受关注的辅助工具&#xff0c…

作者头像 李华
网站建设 2026/6/9 17:37:22

PyArmor 混淆方法详解它到底“混”了什么,又是怎么“护”的?

1. PyArmor 在做的事&#xff1a;把“源码可读”变成“运行可用、理解困难” PyArmor 的核心思路是&#xff1a; 发布产物仍然是 .py&#xff0c;可以“无缝替换”原脚本&#xff08;大多数情况下&#xff09;(GitHub)但 .py 里不再是你的业务逻辑源码&#xff0c;而是一个引…

作者头像 李华
网站建设 2026/6/10 14:32:17

资源一号卫星参数详情

资源一号&#xff08;ZY-1&#xff09;卫星是中国和巴西合作研制的“资源”系列地球资源卫星中的第一代&#xff0c;也被称为中巴地球资源卫星&#xff08;CBERS&#xff09;。它开创了中国传输型对地观测卫星的新纪元。一、 卫星项目概况名称&#xff1a; 资源一号 / 中巴地球…

作者头像 李华
网站建设 2026/6/10 14:35:51

VideoSrt视频字幕生成工具:3大场景化解决方案彻底告别手动打字幕

VideoSrt视频字幕生成工具&#xff1a;3大场景化解决方案彻底告别手动打字幕 【免费下载链接】video-srt-windows 这是一个可以识别视频语音自动生成字幕SRT文件的开源 Windows-GUI 软件工具。 项目地址: https://gitcode.com/gh_mirrors/vi/video-srt-windows 你是否曾…

作者头像 李华