头插法链表拆分

 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 //1.建立空链表
12 NODE * createList()
13 {
14     NODE * head = (NODE *)malloc(sizeof(NODE));
15     head->next = NULL;
16 
17     return head;
18 }
19 //2.插入节点
20 void insertNode(NODE *head,int nodeData)
21 {
22     NODE * cur = (NODE *)malloc(sizeof(NODE));
23     cur->data = nodeData;
24 
25     cur->next = head->next;//新来的结点指向头结点的下一个结点。
26     head->next = cur;       //将新来的结点连入链表。
27 }
28 //遍历输出插入节点后的链表数据
29 void traverList(NODE *head)
30 {
31     head = head->next;
32     while(head)
33     {
34         printf("%d\n",head->data);
35         head = head->next;
36     }
37 }
38 int main(void)
39 {
40 
41     NODE * head = createList();
42     for(int i = 0;i<50;i++)
43     {
44         insertNode(head,i);
45     }
46     traverList(head);
47     
48     return 0;
49 }

 

posted on 2018-09-23 22:17  王朝马汉  阅读(259)  评论(0编辑  收藏  举报

导航