单循环列表的合并

时间:2023-01-01 23:14:50
 1 #include<stdio.h>
2 #include<stdlib.h>
3
4 typedef struct Node
5 {
6 char a;
7 struct Node *next;
8 }Node,*list;
9
10 void p(list L);
11 list m(list A,list B);
12
13 int main()
14 {
15 list L,s,A,B,x,c;
16 L=(list)malloc(sizeof(Node));
17 L->next=L;
18 A=(list)malloc(sizeof(Node));
19 A->next=A;
20 B=(list)malloc(sizeof(Node));
21 B->next=B;
22 p(L);
23 s=L;
24 while(s->next!=L)
25 {
26 s=s->next;
27 printf("%c",s->a);
28 }
29 printf("\n");
30
31 p(A);
32 x=A;
33 p(B);
34 c=B;
35 m(x,c);
36 while(x->next!=A)
37 {
38 x=x->next;
39 printf("%c",x->a);
40 }
41 printf("\n");
42 return 0;
43 }
44
45 void p(list L)
46 {
47 list q,s;
48 char c;
49 q=L;
50 c=getchar();
51 while(c!='\n')
52 {
53 s=(list)malloc(sizeof(Node));
54 s->a=c;
55 q->next=s;
56 q=s;
57 c=getchar();
58 }
59 q->next=L;
60 }
61
62 list m(list A,list B)
63 {
64 list p,q;
65 p=A;
66 q=B;
67 while(p->next!=A)
68 p=p->next;
69 while(q->next!=B)
70 q=q->next;
71 q->next=A; //使B的尾指针指向A的头结点
72 p->next=B->next;//使A的尾指针指向B的第一个节点
73 free(B);
74 return A;
75 }