帶前端節點的單鏈表-------C語言實現

來源:互聯網
上載者:User

標籤:lis   lib   size   erro   ase   www   author   class   node   

  1 /*****************************************************  2 Author:Simon_Kly    Version:0.1    Date:20170520   3 Description:帶頭接點的單鏈表  4 Mail:[email protected]  5 Funcion List:   6 *****************************************************/  7   8 #include <stdio.h>  9 #include <stdlib.h> 10  11 typedef struct node 12 { 13     int data; 14     struct node *next; 15 }Node, *Link; 16  17 /*判斷malloc是否正確執行*/ 18 void is_malloc_ok(Link head) 19 { 20     if (head == NULL) 21     { 22         printf("malloc error!\n"); 23         exit(-1); 24     } 25 } 26  27 /*建立鏈表*/ 28 void create_link(Link * head) 29 { 30     *head = (Link)malloc(sizeof(Node)); 31     is_malloc_ok(*head); 32     (*head)->next = NULL; 33 } 34  35 /*建立節點*/ 36 void create_node(Link * new_node) 37 { 38     *new_node = (Link)malloc(sizeof(Node)); 39     is_malloc_ok(*new_node); 40 } 41  42 /*插入節點尾插*/ 43 void insert_node_tail(Link head, Link new_node) 44 { 45     Link p = NULL; 46      47     p = head; 48  49     while (p->next != NULL) 50     { 51         p = p->next; 52     } 53     p->next = new_node; 54     new_node->next = NULL; 55 } 56  57 /*插入節點頭插*/ 58 void insert_node_head(Link head, Link new_node) 59 { 60     Link p = NULL; 61  62     p = head; 63  64     new_node->next = head->next; 65     head->next = new_node; 66 } 67  68 /*列印節點*/ 69 void output_link(Link head) 70 { 71     Link p = NULL; 72  73  74     if (head == NULL) 75     {//空鏈 76         printf("link is empty!\n"); 77         return ; 78     } 79  80     p = head->next; 81     while (p != NULL) 82     { 83         printf("%d\n", p->data); 84         p = p->next; 85     } 86 } 87  88 /*置空鏈*/ 89 void make_empty_link(Link *head) 90 { 91     Link p = NULL; 92  93     p = (*head)->next; 94  95     while ((*head)->next != NULL) 96     { 97         (*head)->next = (*head)->next->next; 98         free(p); 99         p = (*head)->next;100     }101 }102 103 /*釋放鏈表*/104 void release_link(Link * head)105 {106     make_empty_link(head);107     free(*head);108     *head = NULL;109 }110 111 int main()112 {113     int i;114 115     Link head = NULL;116     Link new_node = NULL;117 118     create_link(&head);119     120     /*尾插*/121     for (i = 0; i < 10; i++)122     {123         create_node(&new_node);124         new_node->data = i + 1;125         insert_node_tail(head, new_node);126     }127     output_link(head);128 129     /*頭插*/130     create_node(&new_node);131     new_node->data = 20;132     insert_node_head(head, new_node);133     output_link(head);134 135     /*釋放鏈表階段*/136     release_link(&head);137     output_link(head);138     return 0;139 }

不帶頭結點代碼傳送門:http://www.cnblogs.com/SimonKly/p/6890287.html

可以從代碼中看出,不帶頭結點的代碼中插入節點的函數insert*中的參數是二級指標,因為頭指標的指向可能會發生變化,需要用二級指標帶回一級指標的地址。

而在帶頭結點的代碼中在相同的插入節點的函數insert*中的參數是一級指標並沒有使用二級指標,因為帶頭結點的鏈表中頭結點是真真實實存在,在create*中分配空間於它,只是它的範圍沒有值,改變的是它的指標域的指向,其指標域的地址可以由它自己帶回。

帶頭結點的鏈表比不帶頭結點的鏈表處理起來更加的簡單,不要考慮頭指標的指向是否發生變化的問題。

帶前端節點的單鏈表-------C語言實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.