# 【SDUT】【链表】2120 - 数据结构实验之链表五：单链表的拆分

#### Sample Input

```10
1 3 22 8 15 999 9 44 6 1001```

#### Sample Output

```4 6
22 8 44 6
1 3 15 999 9 1001```

#### Source

``` 1 #include <iostream>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <algorithm>
5
6 using namespace std;
7
8 struct node
9 {
10     int data;
11     struct node *next;
12 };
13
14 int cnt1,cnt2;
15
16 //申请空间
17 struct node *arr_mal(struct node *p)
18 {
19     p = (struct node *)malloc(sizeof(struct node));
20     return p;
21 }
22
23 //创建链表
24 void arr_create(struct node *head,int n)
25 {
26     struct node *p=NULL,*tail=NULL;
28     while(n--)
29     {
30         p = arr_mal(p);
31         scanf("%d",&p ->data);
32         tail ->next = p;
33         tail = tail ->next;
34     }
35 }
36
37 //输出链表
39 {
40     struct node *p=NULL;
42     if(p != NULL)
43     {
44         printf("%d",p ->data);
45         p = p ->next;
46     }
47     while(p != NULL)
48     {
49         printf(" %d",p ->data);
50         p = p ->next;
51     }
52     printf("
");
53 }
54
55 //拆分
57 {
58
59     struct node *p1=NULL,*p2=NULL,*tail=NULL;
62     while(p1 ->next != NULL)
63     {
64         if(p1 ->next->data%2)
65         {
66             p1 = p1 ->next;
67             cnt1++;
68         }
69         else
70         {
71             tail = p1 ->next;
72             p1 ->next = tail ->next;
73             tail ->next = NULL;
74             p2 ->next = tail;
75             p2 = p2 ->next;
76             cnt2++;
77         }
78     }
79 }
80
81 int main()
82 {
83     int n;
84     cnt1=cnt2=0;