数据结构(2) - 单向链表实例代码

单项链表

单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始;链表是使用指针进行构造的列表;又称为结点列表,因为链表是由一个个结点组装起来的;其中每个结点都有指针成员变量指向列表中的下一个结点;链表是由结点构成,head指针指向第一个成为表头结点,而终止于最后一个指向NULL的指针.

代码实例

  1 /**
  2  * License - MIT.
  3 */
  4 
  5 #include <stdio.h>
  6 #include <stdlib.h>
  7 
  8 #define MAX_TEST_NUM            5
  9 
 10 
 11 typedef struct _SINGLYLIST {
 12     int data;
 13     struct _SINGLYLIST *next;
 14 } SINGLYLIST, *LPSINGLYLIST;
 15 
 16 
 17 /**
 18  * singlylist_empty - Determine if the list is empty.
 19 */
 20 int singlylist_empty(LPSINGLYLIST lpHead)
 21 {
 22     return (lpHead->next == NULL);
 23 }
 24 
 25 /**
 26  * singlylist_insert - Add the specified data at specified node next location.
 27 */
 28 int singlylist_insert(LPSINGLYLIST node, LPSINGLYLIST newnode)
 29 {
 30     newnode->next = node->next;
 31     node->next = newnode;
 32 
 33     return 0;
 34 }
 35 
 36 /**
 37  * singlylist_del - Deletes the specified data by specified node next location.
 38 */
 39 int singlylist_del(LPSINGLYLIST node)
 40 {
 41     LPSINGLYLIST tmp;
 42 
 43     tmp = node->next;
 44     node->next = tmp->next;
 45 
 46     free(tmp);
 47     tmp = NULL;
 48 
 49     return 0;
 50 }
 51 
 52 /**
 53  * singlylist_init - Initialize singly list.
 54 */
 55 int singlylist_init(LPSINGLYLIST *lpHead)
 56 {
 57     *lpHead = (LPSINGLYLIST)malloc(sizeof(SINGLYLIST));
 58     if (NULL == *lpHead) {
 59         return -1;
 60     }
 61 
 62     (*lpHead)->next = NULL;
 63 
 64     return 0;
 65 }
 66 
 67 /**
 68  * singlylist_exit - clear singly list.
 69 */
 70 int singlylist_exit(LPSINGLYLIST lpHead)
 71 {
 72     LPSINGLYLIST pos;
 73 
 74     if (!singlylist_empty(lpHead)) {
 75         for (pos = lpHead; (pos != NULL) && (pos->next != NULL); pos = pos->next)
 76             singlylist_del(pos);
 77     }
 78 
 79     pos = NULL;
 80 
 81     free(lpHead);
 82     lpHead = NULL;
 83 
 84     return 0;
 85 }
 86 
 87 /**
 88  * test_show - Show the singly list data.
 89 */
 90 int test_show(LPSINGLYLIST lpHead)
 91 {
 92     LPSINGLYLIST pos;
 93 
 94     for (pos = lpHead->next; pos != NULL; pos = pos->next)
 95         printf("%d ", pos->data);
 96 
 97     printf("\n");
 98 
 99     return 0;
100 }
101 
102 /**
103  * test_sort - Sort the data.
104 */
105 int test_sort(LPSINGLYLIST lpHead)
106 {
107     LPSINGLYLIST pos, tmp;
108 
109     pos = lpHead->next;
110     lpHead->next = NULL;
111 
112     while (pos != NULL)
113     {
114         tmp = pos;
115         pos = pos->next;
116 
117         singlylist_insert(lpHead, tmp);
118     }
119 
120     return 0;
121 }
122 
123 /**
124  * test_create - Create singly list example.
125 */
126 int test_create(LPSINGLYLIST lpHead)
127 {
128     int i;
129     LPSINGLYLIST newNode, tmp = lpHead;
130 
131     for (i = 0; i < MAX_TEST_NUM; i++) {
132         newNode = (LPSINGLYLIST)malloc(sizeof(SINGLYLIST));
133         if (NULL == newNode) {
134             return -1;
135         }
136 
137         newNode->data = i + 1;
138         singlylist_insert(tmp, newNode);
139         tmp = tmp->next;
140     }
141 
142     return 0;
143 }
144 
145 /**
146  * Reverse sort data: 1,2,3,4,5 --> 5,4,3,2,1
147 */
148 int main(void)
149 {
150     LPSINGLYLIST lphead;
151 
152     singlylist_init(&lphead);
153 
154     test_create(lphead);
155     test_show(lphead);
156     test_sort(lphead);
157     test_show(lphead);
158 
159     singlylist_exit(lphead);
160 
161     return 0;
162 }

完整代码请参考github: [Link] [https://github.com/Phoebus-Ma/C-Helper/tree/main/Class-1/List.C]

posted @ 2021-09-05 15:46  this毛豆  阅读(85)  评论(0编辑  收藏  举报