顺序表

C语言生成两个不同的数值由小到大的表,将两个表的数值一一比对,将数值比较小的写入到第三个表中,最后将某一剩余表中的数值写入第三个表中,代码是:

#include
#include
#define MAXSIZE 100
typedef struct{ int data[MAXSIZE]; int last; }Setlist; Seqlist *build_seqlist(); Seqlist *merge(Seqlist *aseqlist ,Seqlist *bseqlist); int main() { printf("stdio.h--stdio.h--stdio.h--\n"); Seqlist* aSeqlist = build_seqlist(); Seqlist *bSeqlist = build_seqlist(); Seqlist *cSeqlist = merge(aSeqlist ,bSeqlist); } Seqlist *build_seqlist(){ Seqlist *seqlist = malloc(sizeof(Seqlist)); seqlist->last = 9; for(int i = 0;i<=seqlist->last;) { int num = rand()%100; if(i > 0) { int j = 0; for(;jdata[j] >= num) break; } if(j == i) seqlist->data[i] = num; else continue; }else seqlist->data[i] = num; if(seqlist->data[i] > 90+i) continue; printf("build[%d] = %d,num = %d\n",i,seqlist->data[i],num); i++; } printf("\n"); for(int i = 0;i<=seqlist->last;i++) printf("seqlist=%d,",seqlist->data[i]); printf("\n"); return seqlist; } Seqlist * merge(Seqlist *aseqlist ,Seqlist *bseqlist){ Seqlist *cSeqlist = malloc(sizeof(Seqlist)); int i=0 ,j=0 ,k=0; while(i <= aseqlist->last && j <= bseqlist->last) { if(aseqlist->data[i] < bseqlist->data[j]) cSeqlist->data[k++] = aseqlist->data[i++]; else cSeqlist->data[k++] = bseqlist->data[j++]; } printf("\n"); printf("cSeqlist->data[0] == %d",cSeqlist->data[0]); while(i <= aseqlist->last) cSeqlist->data[k++] = aseqlist->data[i++]; while(j <= bseqlist->last) cSeqlist->data[k++] = bseqlist->data[j++]; cSeqlist->last = k-1; printf("\n"); for(int k=0;k<=cSeqlist->last;k++) printf("cSeqlist = %d,",cSeqlist->data[k]); printf("\n"); return cSeqlist; }

你可能感兴趣的:(顺序表)