0x00数据结构——C语言实现(队列)
实现
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #ifndef QUEUE_H |
| #define QUEUE_H |
| |
| |
| typedef enum { |
| false = 0, |
| true |
| } BOOL; |
| |
| struct node; |
| typedef struct node node; |
| typedef node *pos; |
| typedef node *queue; |
| |
| |
| queue create_queue(void); |
| |
| |
| BOOL is_empty(queue Q); |
| |
| |
| BOOL is_full(queue Q); |
| |
| |
| BOOL set_empty(queue Q); |
| |
| |
| BOOL delete_queue(queue *Q); |
| |
| |
| int dequeue(queue Q); |
| |
| |
| int front_queue(queue Q); |
| |
| |
| void enqueue(queue Q, int x); |
| |
| |
| void print_queue(queue Q); |
| |
| #endif |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include "queue.h" |
| |
| #define MAXQUEUE 100 |
| |
| struct node { |
| int val; |
| struct node *next; |
| struct node *pre; |
| }; |
| |
| |
| |
| |
| |
| |
| |
| queue create_queue(void) |
| { |
| queue tmp = (queue)malloc(sizeof(node)); |
| tmp->val = 0; |
| tmp->next = NULL; |
| tmp->pre = NULL; |
| return tmp; |
| } |
| |
| |
| BOOL is_empty(queue Q) |
| { |
| return (Q->val == 0); |
| } |
| |
| |
| BOOL is_full(queue Q) |
| { |
| return (Q->val == MAXQUEUE); |
| } |
| |
| |
| BOOL set_empty(queue Q) |
| { |
| node *tmp = Q->next; |
| while(tmp!=NULL) { |
| (tmp->pre)->next = tmp->next; |
| (tmp->next)->pre = tmp->pre; |
| free(tmp); |
| (Q->val)--; |
| tmp = Q->next; |
| } |
| |
| return true; |
| } |
| |
| |
| BOOL delete_queue(queue *Q) |
| { |
| set_empty(*Q); |
| free(*Q); |
| *Q = NULL; |
| return true; |
| } |
| |
| |
| int dequeue(queue Q) |
| { |
| if(is_empty(Q)) { |
| printf("Q is empty\n"); |
| return -1; |
| } |
| node *tmp = Q->next; |
| int r = tmp->val; |
| Q->next = tmp->next; |
| if(tmp->next != NULL) { |
| (tmp->next)->pre = NULL; |
| } |
| (Q->val)--; |
| return r; |
| } |
| |
| |
| |
| int front_queue(queue Q) |
| { |
| return (Q->next)->val; |
| } |
| |
| |
| void enqueue(queue Q, int x) |
| { |
| if(is_full(Q)) { |
| printf("Q is full\n"); |
| } |
| node *tmp = (queue)malloc(sizeof(node)); |
| tmp->val = x; |
| if(Q->pre != NULL) { |
| (Q->pre)->next = tmp; |
| tmp->pre = (Q->pre); |
| tmp->next = NULL; |
| Q->pre = tmp; |
| } else { |
| tmp->pre = NULL; |
| tmp->next =NULL; |
| Q->pre = tmp; |
| Q->next = tmp; |
| } |
| (Q->val)++; |
| } |
| |
| |
| void print_queue(queue Q) |
| { |
| node *tmp = Q->next; |
| if(tmp == NULL) { |
| printf("->"); |
| } else { |
| while(tmp != NULL) { |
| printf("->%d", tmp->val); |
| tmp = tmp->next; |
| } |
| } |
| printf("\n"); |
| } |
实验
| #include <stdio.h> |
| #include <stdlib.h> |
| #include "queue.h" |
| |
| |
| int main() |
| { |
| queue Q; |
| int i; |
| Q = create_queue(); |
| for(i = 0; i<10; i++) { |
| print_queue(Q); |
| enqueue(Q, i); |
| |
| } |
| |
| |
| while(!is_empty(Q)) { |
| dequeue(Q); |
| print_queue(Q); |
| } |
| |
| |
| return 0; |
| } |
实验结果
| -> |
| ->0 |
| ->0->1 |
| ->0->1->2 |
| ->0->1->2->3 |
| ->0->1->2->3->4 |
| ->0->1->2->3->4->5 |
| ->0->1->2->3->4->5->6 |
| ->0->1->2->3->4->5->6->7 |
| ->0->1->2->3->4->5->6->7->8 |
| ->1->2->3->4->5->6->7->8->9 |
| ->2->3->4->5->6->7->8->9 |
| ->3->4->5->6->7->8->9 |
| ->4->5->6->7->8->9 |
| ->5->6->7->8->9 |
| ->6->7->8->9 |
| ->7->8->9 |
| ->8->9 |
| ->9 |
| -> |
| Time elapsed: 000:00:031 |
| Press any key to continue |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)