0x00数据结构——C语言实现(双链表)
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #ifndef DOUBLE_LINKED_LIST |
| #define DOUBLE_LINKED_LIST |
| |
| |
| typedef int T; |
| #define MAXLEN 100 |
| typedef enum { |
| false = 0, |
| true |
| } BOOL; |
| |
| |
| struct node; |
| typedef struct node dnode; |
| typedef struct node *to_node; |
| typedef to_node d_list; |
| typedef to_node pos; |
| |
| |
| d_list create_list(void); |
| |
| |
| BOOL set_empty(d_list l); |
| |
| |
| int calc_length(d_list l); |
| |
| |
| d_list head_addr(d_list l); |
| |
| |
| pos search(d_list l, T x); |
| |
| |
| pos locate(d_list l, int i); |
| |
| |
| T get_val(d_list l, int i); |
| |
| |
| BOOL change_val(d_list l, int i, const T x); |
| |
| |
| BOOL insert_val(d_list l, int i, const T x); |
| |
| |
| BOOL delete_val(d_list l, int i, T *x); |
| |
| |
| BOOL isempty(d_list l); |
| |
| |
| void output(d_list l); |
| |
| |
| BOOL sort(d_list l); |
| |
| #endif |
| |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include "double_linked_list.h" |
| |
| |
| |
| |
| |
| struct node{ |
| T val; |
| struct node *prev; |
| struct node *next; |
| }; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| d_list create_list(void) |
| { |
| d_list l = (d_list)malloc(sizeof(dnode)); |
| l->next = NULL; |
| l->prev = NULL; |
| l->val = 0; |
| return l; |
| } |
| |
| |
| |
| BOOL set_empty(d_list l) |
| { |
| pos tmp = l->next, ttemp; |
| while(tmp!=NULL){ |
| ttemp = tmp->next; |
| free(tmp); |
| tmp = ttemp; |
| } |
| l->next = NULL; |
| l->prev = NULL; |
| l->val = 0; |
| return true; |
| } |
| |
| |
| int calc_length(d_list l) |
| { |
| return l->val; |
| } |
| |
| |
| pos head_addr(d_list l) |
| { |
| return l; |
| } |
| |
| |
| pos search(d_list l, T x) |
| { |
| int i = 1; |
| pos tmp; |
| tmp = l->next; |
| while(tmp != NULL && tmp->val != x && i<=l->val){ |
| tmp = tmp->next; |
| i++; |
| } |
| if(i>l->val) return NULL; |
| else return tmp; |
| } |
| |
| |
| pos locate(d_list l, int i) |
| { |
| if(i<=l->val && i>0){ |
| pos tmp; |
| tmp = l; |
| while(--i>=0 && tmp!=NULL){ |
| tmp = tmp->next; |
| } |
| return tmp; |
| } else { |
| printf("can not access the %d'th element\n", i); |
| return NULL; |
| } |
| } |
| |
| |
| T get_val(d_list l, int i) |
| { |
| if(i<=l->val && i>0){ |
| pos tmp; |
| tmp = l; |
| while(--i>=0 && tmp!=NULL){ |
| tmp = tmp->next; |
| } |
| return tmp->val; |
| } else { |
| printf("can not access the %d'th element\n", i); |
| return -1; |
| } |
| } |
| |
| |
| BOOL change_val(d_list l, int i, const T x) |
| { |
| if(i<=l->val && i>0){ |
| pos tmp; |
| tmp = l; |
| while(--i>=0 && tmp!=NULL){ |
| tmp = tmp->next; |
| } |
| tmp->val = x; |
| return true; |
| } else { |
| return false; |
| } |
| } |
| |
| |
| BOOL insert_val(d_list l, int i, const T x) |
| { |
| if(i<=l->val && i>=0){ |
| pos tmp; |
| tmp = l; |
| while(--i>=0 && tmp!=NULL){ |
| tmp = tmp->next; |
| } |
| pos t = (pos)malloc(sizeof(dnode)); |
| t->next = tmp->next; |
| if(tmp->next != NULL) |
| (tmp->next)->prev = t; |
| t->prev = tmp; |
| tmp->next = t; |
| t->val = x; |
| l->val++; |
| return true; |
| } else { |
| return false; |
| } |
| } |
| |
| |
| BOOL delete_val(d_list l, int i, T *x) |
| { |
| if(i<=l->val && i>0){ |
| i--; |
| pos tmp; |
| tmp = l; |
| while(--i>=0 && tmp!=NULL){ |
| tmp = tmp->next; |
| } |
| *x = (tmp->next)->val; |
| |
| tmp->next = (tmp->next)->next; |
| (tmp->next)->prev = tmp; |
| l->val--; |
| return true; |
| } else { |
| return false; |
| } |
| } |
| |
| |
| BOOL isempty(const d_list l) |
| { |
| return (l->val == 0 ? true:false); |
| } |
| |
| |
| void output(const d_list l) |
| { |
| pos tmp = NULL; |
| int i = 1; |
| tmp = l->next; |
| while(tmp!=NULL){ |
| printf("the %dth element is %d\n", i++, tmp->val); |
| tmp = tmp->next; |
| } |
| } |
| |
| |
| BOOL sort(const d_list l) |
| { |
| if(l->val < 2) |
| return true; |
| |
| pos tmp, tmp1, tmp2; |
| int i; |
| tmp = locate(l, l->val); |
| while(tmp->prev != l) { |
| |
| tmp1 = l->next; |
| tmp2 = tmp1->next; |
| while(tmp1 != tmp) { |
| if(tmp1->val > tmp2->val) { |
| i = tmp1->val; |
| tmp1->val = tmp2->val; |
| tmp2->val = i; |
| } |
| tmp1 = tmp1->next; |
| tmp2 = tmp2->next; |
| } |
| |
| tmp = tmp->prev; |
| } |
| return true; |
| |
| |
| |
| |
| |
| } |
【推荐】国内首个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)