1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| #include <stdio.h> #include <stdlib.h> #include <malloc.h>
typedef struct node { int date; struct node *next; }Linklist;
Linklist* toujiedian(Linklist *L) { L=(Linklist*)malloc(sizeof(Linklist)); L->next=NULL; return L;
} Linklist* chushihualianbiao (int e) { int d=1,r; Linklist *L=toujiedian(L),*a,*b; a=L; int i; for(i=0;i<e;i++,d++) { b=(Linklist*)malloc(sizeof(Linklist)); printf("输入第%d个数",d); scanf("%d",&r); b->date=r; b->next=NULL; a->next=b; a=a->next;
} return L; }
void spelling(Linklist *L) { Linklist *a; a=L->next; printf("你输入的链表为:\n"); while(a) { printf("%d\t",a->date); a=a->next; } }
Linklist* MergeList(Linklist *La,Linklist *Lb) { Linklist *Lc; Linklist *pa,*pb,*pc,*q; pa=La->next; pb=Lb->next; Lc=pc=La; while(pa && pb) {if(pa->date<pb->date){pc->next=pa;pc=pa;pa=pa->next;} else if(pa->date>pb->date) {pc->next=pb; pc=pb; pb=pb->next;} else { pc->next=pa;pc=pa;pa=pa->next; q=pb->next;free (pb) ;pb =q; } } pc->next=pa?pa:pb; free (Lb); return Lc; }
int main() { Linklist *L; int e; toujiedian(L); printf("输入第一个递增链表长度"); scanf("%d",&e) ; L=chushihualianbiao (e); spelling(L); Linklist *P,*O; int k; toujiedian(P); printf("\n输入第二个递增链表长度"); scanf("%d",&k) ; P=chushihualianbiao (k); spelling(P); printf("\n两个链表合成后为:\n"); O=MergeList(L,P); spelling(O); return 0; }
|