Linux C 数据结构---链表(单向链表)

 上一篇我们讲到了线性表,线性表就是数据元素都一一对应,除只有唯一的前驱,唯一的后继。

       线性表存储结构分为顺序存储、链式存储。

       顺序存储的优点:

       顺序存储的缺点:

       链表就是典型的链式存储,将线性表L = (a0,a1,a2,........an-1)中个元素分布在存储器的不同存储块,成为结点(Node),通过地址或指针建立他们之间的练习,所得到的存储结构为链表结构。表中元素ai的结点形式如下:

其中,结点的data域存放数据元素ai,而next域是一个指针,指向ai的直接后继a(i+1)所在的结点。于是,线性表L=(a0,a1,......an-1)的结构如图:

 

一、节点类型描述:

[cpp]  view plain  copy
  1. typedef struct node_t  
  2. {  
  3.     data_t data; //节点的数据域  
  4.     struct node_t *next;//节点的后继指针域  
  5. }linknode_t,*linklist_t;  

也可这样表示:

[cpp]  view plain  copy
  1. struct node_t  
  2. {  
  3.     data_t data;   
  4.     struct node_t *next;  
  5. }  
  6. typedef struct node_t linknode_t;  
  7. typedef struct node_t *linklist_t;  

若说明

linknode_t  A;

linklist_t p  = &A;

则结构变量A为所描述的节点,而指针变量P为指向此类型节点的指针(p的值为节点的地址);

这样看来 linknode_t  linklist_t 的作用是一样的,那为什么我们要定义两个数据类型(同一种)呢?主要为了代码的可读性,我们要求标识符要望文识义,便于理解;

1、linknode_t  *pnode  指向一个节点;

2、linklist_t list  指向一个整体


二、头结点 head

        我们在前篇提到的顺序存储线性表,如何表达一个空表{ },是通过list->last = -1来表现的,所谓的空表就是数据域为NULL,而我们的链表有数据域和指针域,我们如何表现空链表呢?这时,就引入了头结点的概念,头结点和其他节点数据类型一样,只是数据域为NULL,head->next = NULL,下面我们看一个创建空链表的函数,如何利用头结点来创建一个空链表:

[cpp]  view plain  copy
  1. linklist_t CreateEmptyLinklist()  
  2. {  
  3.     linklist_t list;  
  4.   
  5.     list = (linklist_t)malloc(sizeof(linknode_t));  
  6.     if (NULL != list) {  
  7.         list->next = NULL;  
  8.     }  
  9.     return list;  
  10. }  

只要头结点,链表就还在!

 

三、链表基本运算的相关算法

         链表的运算除了上面的创建空链表,还有数据的插入,删除,查找等函数,链表的运算有各种实现方法,如何写出一个高效的,封装性较好的函数是我们要考虑的,比如数据插入函数,我们就要尽可能考虑所有能出现的结果,比如:1)如果需插入数据的链表是个空表;2)所插入的位置超过了链表的长度;如果我们的函数能包含所有能出现的情况,不仅能大大提高我们的开发效率,也会减少代码的错误率。下面,我们来看看下面的这个链表的插入函数的实现:

[cpp]  view plain  copy
  1. int InsertLinklist(linklist_t list, int at, data_t x)  
  2. {  
  3.     linknode_t *node_prev, *node_at, *node_new;  
  4.     int pos_at;  
  5.     int found = 0;  
  6.   
  7.     if (NULL == list) return -1;  
  8.   
  9.     /* at must >= 0  */  
  10.     if (at < 0) return -1;  
  11.       
  12.     /*第一步、分配空间*/  
  13.     node_new = malloc(sizeof(linknode_t));  
  14.     if (NULL == node_new)   
  15.     {  
  16.         return -1;  
  17.     }  
  18.     node_new->data = x; /* assigned value */  
  19.     node_new->next = NULL; /*节点如果插入超过链表长度的位置,会接到尾节点后面,这样,node_new成了尾节点,node_new->next = NULL */  
  20.   
  21.     /*第二步、定位*/  
  22.     node_prev = list;//跟随指针,帮助我们更好的定位  
  23.     node_at = list->next; //遍历指针  
  24.     pos_at = 0;  
  25.     while (NULL != node_at)   
  26.     {  
  27.         if (pos_at == at)  
  28.         {  
  29.             found = 1; //找到正确的位置,跳出循环  
  30.             break;            
  31.         }  
  32.   
  33.         /* move to the next pos_at */  
  34.         node_prev = node_at; //跟随指针先跳到遍历指针的位置  
  35.         node_at = node_at->next;//遍历指针跳到下一个节点的位置  
  36.         pos_at++;  
  37.     }  
  38.   
  39.     /*第三步、插入*/    
  40.     if (found)   
  41.     {  
  42.         /* found = 1,找到正确的位置,插入  */  
  43.         node_new->next = node_at;//插入的节点next指向node_at  
  44.         node_prev->next = node_new;//插入节点的前一个节点  
  45.     }   
  46.     else   
  47.     {  
  48.         /*若是没找到正确的位置,即所插入位置超越了链表的长度,则接到尾节点的后面,同样,这样适用于{ }即空链表,这样我们可以建立一个空链表,利用这个函数,实现链表的初始化*/  
  49.         node_prev->next = node_new;  
  50.     }  
  51.       

这个插入函数可利用性就非常高。

 

下面讲一个完整链表代码贴出:

listlink.h

[cpp]  view plain  copy
  1. #ifndef _LNK_LIST_H_  
  2. #define _LNK_LIST_H_  
  3.   
  4. typedef int data_t;  
  5.   
  6. typedef struct node_t {  
  7.     data_t data;  
  8.     struct node_t *next;  
  9. } linknode_t, *linklist_t;  
  10.   
  11. linklist_t CreateEmptyLinklist();  
  12.   
  13. void DestroyLinklist(linklist_t list);  
  14.   
  15. void ClearLinklist(linklist_t list);  
  16.   
  17. int EmptyLinklist(linklist_t list);  
  18.   
  19. int LengthLinklist(linklist_t list);  
  20.   
  21. int GetLinklist(linklist_t list, int at, data_t *x);  
  22.   
  23. int SetLinklist(linklist_t list, int at, data_t x);  
  24.   
  25. int InsertLinklist(linklist_t list, int at, data_t x);  
  26.   
  27. int DeleteLinklist(linklist_t list, int at);  
  28.   
  29. linklist_t ReverseLinklist(linklist_t list);  
  30.   
  31. #endif /* _LNK_LIST_H_ */  

linklist.c

[cpp]  view plain  copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include "linklist.h"  
  4.   
  5. linklist_t CreateEmptyLinklist()  
  6. {  
  7.     linklist_t list;  
  8.     list = (linklist_t)malloc(sizeof(linknode_t));  
  9.   
  10.     if (NULL != list) {  
  11.         list->next = NULL;  
  12.     }  
  13.   
  14.     return list;  
  15. }  
  16.   
  17. void DestroyLinklist(linklist_t list)  
  18. {  
  19.     if (NULL != list) {  
  20.         ClearLinklist(list);  
  21.         free(list);  
  22.     }  
  23. }  
  24.   
  25. void ClearLinklist(linklist_t list)  
  26. {  
  27.     linknode_t *node; /* pointer to the node to be removed */  
  28.     if (NULL == list) return;  
  29.   
  30.     while (NULL != list->next) {  
  31.         node = list->next;  
  32.         list->next = node->next;  
  33.         free(node);  
  34.     }  
  35.     return;  
  36. }  
  37.   
  38. int LengthLinklist(linklist_t list)  
  39. {  
  40.     int len = 0;  
  41.     linknode_t *node; //iterate pointer  
  42.   
  43.     if (NULL == list) return -1;  
  44.   
  45.     node = list->next; // node points to the first data node  
  46.     while (NULL != node) {  
  47.         len++;  
  48.         node = node->next;  
  49.     }  
  50.     return len;  
  51. }  
  52.   
  53. int EmptyLinklist(linklist_t list)  
  54. {  
  55.     if (NULL != list) {  
  56.         if (NULL == list->next) {  
  57.             return 1;  
  58.         } else {  
  59.             return 0;  
  60.         }  
  61.     } else {  
  62.         return -1;  
  63.     }  
  64. }  
  65.   
  66. int GetLinklist(linklist_t list, int at, data_t *x)  
  67. {  
  68.     linknode_t *node;   /* used for iteration */  
  69.     int pos;        /* used for iteration and compare with */  
  70.   
  71.     if (NULL == list) return -1;  
  72.     /* at must >= 0 */  
  73.     if (at < 0) return -1;  
  74.     /* start from the first element */  
  75.     node = list->next;  
  76.     pos = 0;  
  77.     while (NULL != node) {  
  78.         if (at == pos) {  
  79.             if (NULL != x) {  
  80.                 *x = node->data;  
  81.             }  
  82.             return 0;             
  83.         }  
  84.         /* move to the next */  
  85.         node = node->next;  
  86.         pos++;  
  87.     }  
  88.     return -1;  
  89. }  
  90.   
  91.   
  92.   
  93. int SetLinklist(linklist_t list, int at, data_t x)  
  94. {  
  95.     linknode_t *node; /* used for iteration */  
  96.     int pos;  
  97.     int found = 0;  
  98.   
  99.     if (!list) return -1;  
  100.     /* at must >= 0 */  
  101.     if (at < 0) return -1;  
  102.     /* start from the first element */  
  103.     node = list->next;  
  104.     pos = 0;  
  105.     while (NULL != node) {  
  106.         if (at == pos) {   
  107.             found = 1; /* found the position */  
  108.             node->data = x;  
  109.             break;            
  110.         }  
  111.         /* move to the next */  
  112.         node = node->next;  
  113.         pos++;  
  114.     }  
  115.     if (1 == found) {  
  116.         return 0;  
  117.     } else {  
  118.         return -1;  
  119.     }  
  120. }  
  121.   
  122. int InsertLinklist(linklist_t list, int at, data_t x)  
  123. {  
  124.     /*  
  125.      * node_at and pos_at are used to locate the position of node_at. 
  126.      * node_prev follows the node_at and always points to previous node  
  127.      *  of node_at. 
  128.      * node_new is used to point to the new node to be inserted. 
  129.      */  
  130.     linknode_t  *node_prev, *node_at, *node_new;  
  131.     int     pos_at;  
  132.     int         found = 0;  
  133.   
  134.     if (NULL == list) return -1;  
  135.   
  136.     /* at must >= 0 */  
  137.     if (at < 0) return -1;  
  138.   
  139.     node_new = malloc(sizeof(linknode_t));  
  140.     if (NULL == node_new) {  
  141.         return -1;  
  142.     }  
  143.     node_new->data = x; /* assigned value */  
  144.     node_new->next = NULL;  
  145.   
  146.     node_prev = list;  
  147.     node_at = list->next;  
  148.     pos_at = 0;  
  149.     while (NULL != node_at) {  
  150.         if (pos_at == at) {  
  151.             /*  
  152.              * found the node 'at' 
  153.              */   
  154.             found = 1;  
  155.             break;            
  156.         }  
  157.         /* move to the next pos_at */  
  158.         node_prev = node_at;  
  159.         node_at = node_at->next;  
  160.         pos_at++;  
  161.     }  
  162.       
  163.     if (found) {  
  164.         /* insert */  
  165.         node_new->next = node_at;  
  166.         node_prev->next = node_new;  
  167.     } else {  
  168.         /*  
  169.          * If not found, means the provided "at" 
  170.          * exceeds the upper limit of the list, just  
  171.          * append the new node to the end of the list. 
  172.          */  
  173.         node_prev->next = node_new;  
  174.     }  
  175.     return 0;  
  176. }  
  177.   
  178. int DeleteLinklist(linklist_t list, int at)  
  179. {  
  180.     /*  
  181.      * node_at and pos_at are used to locate the position of node_at. 
  182.      * node_prev follows the node_at and always points to previous node  
  183.      *  of node_at. 
  184.      */  
  185.   
  186.     linknode_t  *node_prev, *node_at;  
  187.     int     pos_at;  
  188.     int         found = 0;  
  189.   
  190.     if (!list) return -1;  
  191.     /* at must >= 0 */  
  192.     if (at < 0) return -1;  
  193.   
  194.     node_prev = list;  
  195.     node_at = list->next;  
  196.     pos_at = 0;   
  197.   
  198.     while (NULL != node_at) {  
  199.         if (pos_at == at) {  
  200.             /*  
  201.              * found the node 'at' 
  202.              */   
  203.             found = 1;  
  204.             break;            
  205.         }  
  206.         /* move to the next pos_at */  
  207.         node_prev = node_at;  
  208.         node_at = node_at->next;  
  209.         pos_at++;  
  210.     }  
  211.     if (found) {  
  212.         /* remove */  
  213.         node_prev->next = node_at->next;  
  214.         free(node_at);  
  215.         return  0;  
  216.     } else {  
  217.         return -1;  
  218.     }  
  219. }  
  220.   
  221. linklist_t ReverseLinklist(linklist_t list)  
  222. {  
  223.     linknode_t *node;   /* iterator */  
  224.     linknode_t *node_prev;  /* previous node of iterator */  
  225.     linknode_t *node_next;  /* next node of iterator,  
  226.                  * used to backup next of iterator  
  227.                  */  
  228.     if (NULL == list) return NULL;  
  229.     node_prev = NULL;  
  230.     node = list->next;  
  231.     while (NULL != node) {  
  232.         /* 
  233.          * step1: backup node->next 
  234.          * due to the next of iterator will be 
  235.          * modified in step2 
  236.          */  
  237.         node_next = node->next;  
  238.         /*  
  239.          * when iterator reaches the last node  
  240.          * of original list, make the list head 
  241.          * point to the last node, so the original 
  242.          * last one becomes the first one. 
  243.          */  
  244.   
  245.         if (NULL == node_next) {  
  246.             list->next = node;  
  247.         }  
  248.   
  249.         /*  
  250.          * step2: reverse the linkage between nodes 
  251.          * make the node pointer to the previous node, 
  252.          * not the next node 
  253.          */       
  254.         node->next = node_prev;        
  255.         /*  
  256.          * step3: move forward  
  257.          */  
  258.   
  259.         node_prev = node;  
  260.         node = node_next;  
  261.     }  
  262.     return list;  
  263. }  

main.c

[cpp]  view plain  copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include "linklist.h"  
  4.   
  5. int main()  
  6. {  
  7.     int i;  
  8.     data_t x;  
  9.     linklist_t p;  
  10.     p = CreateEmptyLinklist();  
  11.     data_t a[10] = {1,3,5,7,9,11,13,15,17,19};  
  12.   
  13.     for(i = 0;i < 10;i++)  
  14.     {  
  15.         InsertLinklist(p,i,a[i]);  
  16.     }  
  17.   
  18.     ReverseLinklist(p);  
  19.     printf("The length of the list is:%d\n",LengthLinklist(p));  
  20.       
  21.     GetLinklist(p,4,&x);  
  22.     printf("The NO.4 of this list is:%d\n",x);  
  23.   
  24.     SetLinklist(p,4,100);  
  25.     GetLinklist(p,4,&x);  
  26.     printf("After updating!The No.4 0f this list is:%d\n",x);  
  27.   
  28.     DeleteLinklist(p,4);  
  29.     printf("After updating!The length of the list is:%d\n",LengthLinklist(p));  
  30.     GetLinklist(p,4,&x);  
  31.     printf("After updating!The No.4 0f this list is:%d\n",x);  
  32.   
  33.     ReverseLinklist(p);  
  34.       
  35.     ClearLinklist(p);  
  36.     if(EmptyLinklist(p))  
  37.         printf("This list is empty!\n");  
  38.     DestroyLinklist(p);  
  39.     printf("This list is destroyed!\n");  
  40.   
  41.     return 0;  
  42.       
  43. }  

执行结果如下:

[cpp]  view plain  copy
  1. fs@ubuntu:~/qiang/list/list2$ ./Test  
  2. The length of the list is:10  
  3. The NO.4 of this list is:11  
  4. After updating!The No.4 0f this list is:100  
  5. After updating!The length of the list is:9  
  6. After updating!The No.4 0f this list is:9  
  7. This list is empty!  
  8. This list is destroyed!  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux C数据结构是用来组织和存储数据的方式。常见的数据结构包括数组和哈希表。 数组是一种线性数据结构,它可以存储一组相同类型的数据。在C语言,我们可以使用数组来存储自定义的数据结构,例如表示学生英语成绩的结构体。通过定义一个数组,我们可以方便地存储和访问多个学生的成绩信息。\[1\] 哈希表是一种根据关键字直接访问数据的数据结构。在C语言,我们可以使用哈希表来存储学生信息等数据。哈希表通过计算关键字的哈希值,将数据存储在对应的位置上,从而实现快速的查找和插入操作。在上面的例子,我们使用哈希表来存储学生信息,通过计算学生ID的哈希值,将学生信息插入到对应的链表。\[2\] 数据结构的实现可以编译成动态链接库,方便在不同的项目复用。这样可以将数据结构的代码与业务逻辑分离,使得代码更加模块化和可维护。在Linux,我们可以使用Makefile来编译和链接数据结构的代码,生成可执行文件或者链接库。\[1\]\[2\] 总之,数据结构Linux C起着重要的作用,它们可以帮助我们组织和管理数据,提高程序的效率和可维护性。 #### 引用[.reference_title] - *1* *2* *3* [数据结构Linux环境C语言版)](https://blog.csdn.net/geek_liyang/article/details/129909307)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值