标签:
#include <stdio.h> #include <malloc.h> #include <stdlib.h> #define MaxSize 50 typedef struct { int data[MaxSize]; int length; }SqList; void ListInsert(SqList *L,int i,int e) { int j; if(i<1||i>L->length+1) exit(-1); if(L->length>=MaxSize) exit(-1); for(j=L->length;j>=i;j--) L->data[j]=L->data[j-1]; L->data[i-1]=e; L->length++; } void DispList(SqList *L) { int i; for(i=0;i<L->length;i++) printf("%d ",L->data[i]); printf("\n"); } void Exchange(SqList *A,SqList *B,SqList *C) { int i=0,j=0,k=0; while(i<A->length&&j<B->length) { if(A->data[i]<B->data[j]) C->data[k++]=A->data[i++]; else if(A->data[i]>B->data[j]) C->data[k++]=B->data[j++]; } while(i<A->length) C->data[k++]=A->data[i++]; while(j<B->length) C->data[k++]=B->data[j++]; C->length=k; } void main() { SqList *A,*B,*C; A=(SqList*)malloc(sizeof(SqList)); A->length=0; B=(SqList*)malloc(sizeof(SqList)); B->length=0; C=(SqList*)malloc(sizeof(SqList)); C->length=0; ListInsert(A,1,1); ListInsert(A,2,3); ListInsert(A,3,5); ListInsert(A,4,7); ListInsert(A,5,9); ListInsert(B,1,2); ListInsert(B,2,4); ListInsert(B,3,6); ListInsert(B,4,8); ListInsert(B,5,10); ListInsert(B,6,12); Exchange(A,B,C); printf("顺序表A:"); DispList(A); printf("顺序表B:"); DispList(B); printf("顺序表C:"); DispList(C); }
标签:
原文地址:http://www.cnblogs.com/919czzl/p/4436917.html