0x00数据结构——C语言实现(单链表)
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #ifndef SINGLY_LINKED_LIST |
| #define SINGLY_LINKED_LIST |
| |
| |
| typedef int T; |
| #define MAXLEN 100 |
| typedef enum { |
| false = 0, |
| true |
| } BOOL; |
| |
| |
| struct node; |
| typedef struct node node; |
| typedef struct node *to_node; |
| typedef to_node link_list; |
| typedef to_node pos; |
| |
| |
| link_list create_list(void); |
| |
| |
| BOOL set_empty(link_list l); |
| |
| |
| int calc_length(link_list l); |
| |
| |
| link_list head_addr(link_list l); |
| |
| |
| pos search(link_list l, T x); |
| |
| |
| pos locate(link_list l, int i); |
| |
| |
| T get_val(link_list l, int i); |
| |
| |
| BOOL change_val(link_list l, int i, const T x); |
| |
| |
| BOOL insert_val(link_list l, int i, const T x); |
| |
| |
| BOOL delete_val(link_list l, int i, T *x); |
| |
| |
| BOOL isempty(link_list l); |
| |
| |
| void output(link_list l); |
| |
| |
| BOOL sort(link_list l); |
| |
| #endif |
| |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include "singly_linked_list.h" |
| |
| |
| |
| |
| |
| struct node{ |
| T val; |
| struct node *next; |
| }; |
| |
| |
| |
| |
| |
| |
| |
| |
| link_list create_list(void) |
| { |
| link_list l = (link_list)malloc(sizeof(node)); |
| l->next = NULL; |
| l->val = 0; |
| return l; |
| } |
| |
| |
| |
| BOOL set_empty(link_list l) |
| { |
| pos tmp = l->next, ttemp; |
| while(tmp!=NULL){ |
| ttemp = tmp->next; |
| free(tmp); |
| tmp = ttemp; |
| } |
| l->next = NULL; |
| l->val = 0; |
| return true; |
| } |
| |
| |
| int calc_length(link_list l) |
| { |
| return l->val; |
| } |
| |
| |
| link_list head_addr(link_list l) |
| { |
| return l; |
| } |
| |
| |
| pos search(link_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(link_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(link_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(link_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(link_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(node)); |
| t->next = tmp->next; |
| t->val = x; |
| tmp->next = t; |
| l->val++; |
| return true; |
| } else { |
| return false; |
| } |
| } |
| |
| |
| BOOL delete_val(link_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; |
| l->val--; |
| return true; |
| } else { |
| return false; |
| } |
| } |
| |
| |
| BOOL isempty(const link_list l) |
| { |
| return (l->val == 0 ? true:false); |
| } |
| |
| |
| void output(const link_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 link_list l) |
| { |
| if(l->val < 2) |
| return true; |
| |
| pos tmp, tmp1, tmp2; |
| int i; |
| tmp = l->next; |
| while(tmp->next != NULL) { |
| |
| tmp1 = l->next; |
| tmp2 = tmp1->next; |
| while(tmp2 != NULL) { |
| if(tmp1->val > tmp2->val) { |
| i = tmp1->val; |
| tmp1->val = tmp2->val; |
| tmp2->val = i; |
| } |
| tmp1 = tmp1->next; |
| tmp2 = tmp2->next; |
| } |
| |
| tmp = tmp->next; |
| } |
| 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)