尾插法链表拆分

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 /*
 4 尾插法链表拆分:1.建立空链表。2.插入节点。3.遍历并输出插入节点后的链表数据
 5 */
 6 typedef struct node
 7 {
 8     int data;
 9     struct node * next;
10 }NODE;
11 //建立空链表
12 NODE * createList()
13 {
14     NODE * head = (NODE *)malloc(sizeof(NODE));
15     head->next = NULL;
16 
17     return head;
18 }
19 //插入节点
20 NODE * insertNode(NODE * pt,int nodeData)
21 {
22     NODE * cur = (NODE *)malloc(sizeof(NODE));
23     cur->data = nodeData;
24 
25     pt->next = cur;
26     cur->next = NULL;
27     pt = cur;
28 
29     return pt;
30 }
31 //遍历并输出插入节点后的链表元素
32 void traverList(NODE *head)
33 {
34     head = head->next;
35     while(head)
36     {
37         printf("%d\n",head->data);
38         head = head->next;
39     }
40 }
41 
42 int main(void)
43 {
44     //建立空链表
45     NODE * head = createList();
46     //插入节点
47     NODE * pt = head;//定义pt保存尾节点的地址
48     for(int i=0;i<50;i++)
49     {
50         pt = insertNode(pt,i);
51     }
52     //遍历并输出插入节点后的链表元素
53     traverList(head);
54     return 0;
55 }

猜你喜欢

转载自www.cnblogs.com/wangchaomahan/p/9693975.html
今日推荐