1#include "stdio.h"
2#include "string.h"
3#include "stdlib.h"
4
5typedef struct student
6{
7 int stuno;
8 int age;
9 struct student *next;
10};
11
12int num;
13student* createLink();
14student* merge(student *pA, student *pB);
15void sort(student *head, int num);
16void print(student *head);
17
18/************************************************************************/
19/* main
20/************************************************************************/
21int main()
22{
23 num = 0;
24 student *head_A, *head_B, *head_C;
25 head_A=head_B=head_C=NULL;
26
27 head_A = createLink();
28 head_B = createLink();
29
30 printf("link list A:\n");
31 print(head_A);
32 printf("link list B:\n");
33 print(head_B);
34
35 head_C = merge(head_A, head_B);
36 printf("merge A with B:\n");
37 print(head_C);
38
39 sort(head_C, num);
40 printf("merge A with B (sort with ASC):\n");
41 print(head_C);
42 return 0;
43}
44/************************************************************************/
45/* create link list
46/************************************************************************/
47student* createLink()
48{
49 student *head, *p1, *p2;
50 head=p1=p2=NULL;
51
52 printf("************************\n stuno:0 will exit:\n************************\n");
53
54 while(1)
55 {
56 p1 = (student *)malloc(sizeof(*p1));
57 if (p1== NULL)
58 {
59 printf("malloc fail!");
60 exit(0);
61 }
62
63 printf("input stuno:");
64 scanf("%d", &p1->stuno);
65
66 //if (strcmp(p1->stuno, "0") == 0)
67 if (p1->stuno == 0)
68 {
69 break;
70 }
71
72 printf("input age:");
73 scanf("%d", &p1->age);
74
75 if (head == NULL)
76 {
77 head = p1;
78 }
79 else
80 {
81 p2->next = p1;
82 }
83
84 p2 = p1;
85 p2->next = NULL;
86
87 num++;
88 }
89
90 return head;
91}
92/************************************************************************/
93/* output link list
94/************************************************************************/
95void print(student *head)
96{
97 printf("\nstuno age\n----- ----\n");
98 student *p = head;
99 while(p != NULL)
100 {
101 printf("%-5d %-4d\n", p->stuno, p->age);
102 p = p->next;
103 }
104 printf("\n");
105}
106/************************************************************************/
107/* merge A and B
108/************************************************************************/
109student* merge(student *pA, student *pB)
110{
111 student *pa, *pb, *pc, *pchead;
112
113 pchead = pc = pA; //initial pc;
114 pa = pA->next;
115 pb = pB;
116 while(pa != NULL)
117 {
118 pc->next=pa;
119 pc=pa;
120 pa=pa->next;
121 }
122 while(pb != NULL)
123 {
124 pc->next = pb;
125 pc = pb;
126 pb = pb->next;
127 }
128 pc->next = NULL;
129
130// while(pa!=NULL && pb!=NULL)
131// {
132// if(pa->stuno < pb->stuno)
133// {
134// pc->next=pa;
135// pc=pa;
136// pa=pa->next;
137// }
138// else
139// {
140// pc->next=pb;
141// pc=pb;
142// pb=pb->next;
143// }
144// }
145//
146// if(pa)
147// pc->next=pa;
148// else
149// pc->next=pb;
150
151 return pchead;
152}
153/************************************************************************/
154/* sort ASC 冒泡排序法
155/************************************************************************/
156void sort(student *head, int num)
157{
158 int i, j, t;
159 student *p;
160
161 for (i = 0; i < num - 1; i++) //总的轮次
162 {
163 p = head;
164 for (j = 0; j < num - i - 1; j++) //相邻大小进行排序,大的往最后冒
165 {
166 if (p->stuno > p->next->stuno)
167 {
168 t = p->stuno;
169 p->stuno = p->next->stuno;
170 p->next->stuno = t;
171 }
172 p = p->next;
173 }
174 }
175}