【SDUT】【チェーンテーブル】2120-データ構造実験のチェーンテーブル5:シングルチェーンテーブルの分割
11294 ワード
Problem Description
N個の整数の順序を入力して、単一チェーンテーブルを作成し、この単一チェーンテーブルを2つのサブチェーンテーブルに分割し、第1のサブチェーンテーブルはすべての偶数を保存し、第2のサブチェーンテーブルはすべての奇数を保存した.2つのサブチェーンテーブルのデータの相対順序は、元のチェーンテーブルと一致します.
Input
1行目は整数Nを入力する.
2行目にN個の整数を順次入力します.
Output
第1行は、偶数チェーンテーブルと奇数チェーンテーブルの要素個数をそれぞれ出力する.
2行目は偶数サブチェーンテーブルのすべてのデータを順次出力する.
3行目は奇数サブチェーンテーブルのすべてのデータを順次出力します.
Sample Input
Sample Output
Hint
配列は使用できません!
Source
N個の整数の順序を入力して、単一チェーンテーブルを作成し、この単一チェーンテーブルを2つのサブチェーンテーブルに分割し、第1のサブチェーンテーブルはすべての偶数を保存し、第2のサブチェーンテーブルはすべての奇数を保存した.2つのサブチェーンテーブルのデータの相対順序は、元のチェーンテーブルと一致します.
Input
1行目は整数Nを入力する.
2行目にN個の整数を順次入力します.
Output
第1行は、偶数チェーンテーブルと奇数チェーンテーブルの要素個数をそれぞれ出力する.
2行目は偶数サブチェーンテーブルのすべてのデータを順次出力する.
3行目は奇数サブチェーンテーブルのすべてのデータを順次出力します.
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
Hint
配列は使用できません!
Source
1 #include
2 #include
3 #include
4 #include
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;
27 tail = head;
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 //
38 void arr_prin(struct node *head)
39 {
40 struct node *p=NULL;
41 p = head ->next;
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 //
56 void arr_split(struct node *head1,struct node *head2)
57 {
58
59 struct node *p1=NULL,*p2=NULL,*tail=NULL;
60 p1 = head1;
61 p2 = head2;
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;
85 struct node *head1=NULL,*head2=NULL;
86 head1 = arr_mal(head1);
87 head2 = arr_mal(head2);
88 scanf("%d",&n);
89 arr_create(head1,n);
90 arr_split(head1,head2);
91 printf("%d %d
",cnt2,cnt1);
92 arr_prin(head2);
93 arr_prin(head1);
94 return 0;
95 }