单链表的常用操作

      直接贴代码了:

Code:
  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3. #include <assert.h>   
  4. #define Type int   
  5. #define bool int   
  6. #define true 0   
  7. #define false 1   
  8. struct link_node {   
  9.     Type data;   
  10.     struct link_node *next;   
  11. };   
  12.   
  13. /*  
  14. bool init_list(struct link_node **plist)  
  15. {  
  16.     *plist = (struct link_node *)malloc(sizeof(struct link_node));  
  17.     if (plist == NULL)  
  18.         return false;  
  19.     (*plist)->next = NULL;  
  20.     return true;  
  21. }  
  22. */  
  23. struct link_node *init_list()   
  24. {   
  25.     struct link_node *plist = (struct link_node *)malloc(sizeof (struct link_node));   
  26.     if (plist == NULL)   
  27.         return NULL;   
  28.     plist->next = NULL;   
  29.     return plist;   
  30. }   
  31.   
  32. /*add an element to the tail of list*/  
  33. bool add_list_tail(struct link_node *plist, Type data)   
  34. {   
  35.     assert(plist != NULL);   
  36.     struct link_node *new_node = (struct link_node *)malloc(sizeof(struct link_node));   
  37.     if (new_node == NULL)   
  38.         return false;   
  39.     new_node->data = data;   
  40.     new_node->next = NULL;   
  41.   
  42.     struct link_node *pnode = plist;   
  43.     while (pnode->next != NULL)   
  44.         pnode = pnode->next;   
  45.     pnode->next = new_node;   
  46.     return true;   
  47. }  
  48. /*add an elment to the head of list*/  
  49. bool add_list(struct link_node *plist, Type data)   
  50. {   
  51.     assert(plist != NULL);   
  52.     struct link_node *new_node = (struct link_node *)malloc(sizeof(struct link_node));   
  53.     if (new_node == NULL)   
  54.         return false;   
  55.     new_node->data = data;   
  56.        
  57.     if (plist->next == NULL) {   
  58.         plist->next = new_node;   
  59.         new_node->next = NULL;   
  60.     } else {   
  61.         new_node->next = plist->next;   
  62.         plist->next = new_node;   
  63.     }   
  64.     return true;   
  65. }   
  66.   
  67. int list_length(struct link_node *plist)   
  68. {   
  69.     int count = 0;   
  70.     if (plist == NULL) {
  71.         return count;   
  72.     } else {   
  73.         while (plist->next != NULL) {   
  74.             plist = plist->next;   
  75.             count++;   
  76.         }   
  77.     }   
  78.     return count;   
  79. }   
  80. /*insert an elment into list before the ith elment, i belong [1, length of list]*/  
  81. bool insert_list(struct link_node *plist, int i, Type data)   
  82. {   
  83.     int length = list_length(plist);   
  84.     if (length == 0 || i > length || i < 1) { 
  85.         return false;   
  86.     } else {   
  87.         int j = 0;   
  88.         while (j != i - 1) {   
  89.             plist = plist->next;   
  90.             j++;   
  91.         }   
  92.         struct link_node *new_node = (struct link_node *)malloc(sizeof(struct link_node));   
  93.         if (new_node == NULL)   
  94.             return false;   
  95.         new_node->data = data;   
  96.            
  97.         new_node->next = plist->next;   
  98.         plist->next = new_node;   
  99.     }   
  100.     return true;   
  101. }   
  102. /*delete the ith elment in list, i belong [1, length of list]*/  
  103. bool delete_list(struct link_node *plist, int i)   
  104. {   
  105.     int length = list_length(plist);   
  106.     if (length == 0 || i > length || i < 1) {
  107.         return false;   
  108.     } else {   
  109.         int j = 0;   
  110.         while (j != i - 1) {   
  111.             plist = plist->next;   
  112.             j++;   
  113.         }   
  114.         struct link_node *p = plist->next;   
  115.         plist->next = p->next;   
  116.         free(p);   
  117.     }   
  118.     return true;   
  119. }   
  120.   
  121. void print(struct link_node *plist)   
  122. {   
  123.     assert(plist != NULL);   
  124.     while (plist->next != NULL) {   
  125.         plist = plist->next;   
  126.         printf("%d ", plist->data);   
  127.     }   
  128. }   
  129.   
  130. struct link_node *destory_list(struct link_node *plist)   
  131. {   
  132.     struct link_node *p;   
  133.     while (plist != NULL) {   
  134.         p = plist->next;   
  135.         free(plist);   
  136.         plist = p;   
  137.     }   
  138.     return plist;   
  139. }   
  140. int main()   
  141. {   
  142.     struct link_node *list = NULL;   
  143.     list = init_list();   
  144.     int i;   
  145.     for (i = 1; i < 3; i++)   
  146.         add_list_tail(list, i);   
  147.   
  148.     insert_list(list, 1, 0);   
  149.     print(list);   
  150.     printf("/n");   
  151.     delete_list(list, 3);   
  152.     print(list);   
  153.     list = destory_list(list);   
  154.     return 0;   
  155. }   

      有一点可以说的是链表的初始化方式,有两种可以传地址:一种是采用指向指针的指针,也就是二级指针,另外一种是函数的返回指向内存块的指针。一般的数据结构教材上常用如下定义:
typedef struct LNode {
    Type data;
    struct LNode *next;
}LNode, *LinkList;
main中调用的方式:LinkList p = NULL;
                  init_list(&p);
初始化函数原型:init_list(LinkList *L)
      这里的L即是二级指针。另外注意的是,只有会改变的L值的操作才会使用二级指针。其他的比如计算链表的长度。这样即可:list_length(LinkList L)。
      个人原因宁可多敲下几个键盘,一般情况也不用typedef。因为引入别名会让程序理解起来复杂。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值