/* * 链表数组归并(不删除数组中元素). */ #include <stdio.h> #include <malloc.h> main() { int a[] = {2,3,5,6,7,8,11,13,15,19}; int b[] = {1,2,4,5,7,9,10,12,14,20}; int length = sizeof(a)/sizeof(int); typedef struct node { int data; struct node *next; }ArrayNode, *ArrayPoint; ArrayPoint ahead, ap; ahead = (ArrayPoint)malloc(sizeof(ArrayNode)); ahead->next = NULL; for(int i=9; i>=0; --i) { ap = (ArrayPoint)malloc(sizeof(ArrayNode)); ap->data = a[i]; ap->next = ahead->next; ahead->next = ap; } ArrayPoint bhead, bp; bhead = (ArrayPoint)malloc(sizeof(ArrayNode)); bhead->next = NULL; for(int i=9; i>=0; --i) { bp = (ArrayPoint)malloc(sizeof(ArrayNode)); bp->data = b[i]; bp->next = bhead->next; bhead->next = bp; } ArrayPoint chead, cp; ap = ahead->next; bp = bhead->next; chead = ahead; cp = ahead; while(ap && bp) { if((ap->data)<=(bp->data)) { cp->next = ap; cp = ap; ap = ap->next; } else { cp->next = bp; cp = bp; bp = bp->next; } } cp->next = ap? ap:bp; chead = chead->next; for(int i=0; i<20; i++) { printf("%d/n",chead->data); chead = chead->next; } }