《线性表的存储结构与顺序表的存储实现.docx》由会员分享,可在线阅读,更多相关《线性表的存储结构与顺序表的存储实现.docx(3页珍藏版)》请在第一文库网上搜索。
1、#inc1ude#inc1ude#inc1ude#define1IST_INIT_SIZE1OO#define1ISTJNCREMENTIO#defineOVERF1OW-2#defineOK1#defineERROR0#defineTRUEI#defineFA1SEOinti,n;IypedefintE1emType;typedefintStatus;/1typedefstructE1emType*e1em;int1ength;int1istsize;Sq1ist;Sq1ist1;/2StatusInit1ist_Sq(Sq1ist&1)(1.e1em=(E1emType*)ma11oc(1
2、IST_INIT_SIZE*sizeof(E1emType);if(!1,e1em)exit(OVERF1OW);1Jength=O;1jistsize=1ISTJNIT-SIZE;returnOK;/3voidCreate_Sq(Sq1ist&1)inti,n;Primf(创建一个有序表(从小到大):n);Printf(输入有序表元素的个数:”);scanf(,%d,fcn);1.1ength=n;fbr(i=O;in;i+)(Printf(输入第d个元素的值:,i+1);scanf(%d1,e1em1i);printf(,n);/4voidDisp_Sq(Sq1ist1)inti,n;n=
3、1.1ength;for(i=0;in;i+)(printf(,%5d,1,e1emiJ);)printf(nnn);/5int1ist1ength(Sq1ist1)(return1.1ength;)Status1iSt1nSert(Sq1iSI*1,inti,E1emTypee)(EIemType*newbase,*q,*p;if(i(*1).1ength+1)returnERROR:if(*1).Iength=(*1).1istsize)(newbase=(E1emTypc*)rea11oc(*1).e1em,(*1).1istsize+1ISTNCREMENT)*sizeof(E1emT
4、ype);if(!newbase)exit(OVERF1OW);(*1).e1em=newbase;(*1).1istsize+=1ISTJNCREMENT;)q=(*1).e1em+i-1;fbr(p=(*1).e1em+(*1).1ength-1;p=q;-p)*(p+1)=*p;*q=e;+(*1).Iength;returnOK;int1ocateE1cm(Sq1ist1,E1cmTypee,Status(*compare)(E1cmType,E1emType)E1emType*;inti=1;p=1.e1em;whi!e(i=1.1ength&!compare(*p+,e)+i;if
5、(i=1.1ength)returni;e1sereturnO;StatusGctE1em(Sq1ist1.inti.E1emTypc*e)if(i1.1ength)returnERROR;*e=*(1.e1em+i-1);returnOK;)Statusequa1(EIemTjpec1,EIemTypec2)(if(c1=c2)returnTRUE;e1sereturnFA1SE;)/5.5voidMerge1iSI(Sq1iS11a,Sq1ist1btSq1isi*1c)(inti=1j=1,k=O;int1aJen,1bJen;EIemTypeai,bj;1a-1en=1ist1engt
6、h(1a);1bJen=1ist1ength(1b);whi1e(i=1a_1en&j=1b_1en)(GetEIem(1a,i,&ai);GetEIem(1bJ,&bj);if(ai=bj)(1istInsert(1c,+k,ai);+i;)e1se(1iSt1nSert(1C,+k,bj);+j;whi1e(i=1aJen)(GetE1em(1a,i+,fcai);1istInsert(1c,+k,ai);)whiIe(j=1b-Ien)(GetE1em(1b,j+,&bj);1istInsert(1c,+k,bj);/6voidmain()Sq1ist1a,1b,1c;Init1ist-Sq(1a);Create_Sq(1a);printf(1a=);Disp_Sq(1a);Init1isCSq(1b);Create_Sq(1b);printf(1a=);Disp_Sq(1b);Init1ist_Sq(1c);Merge1iSt(1a,1b,&1c);1a=1c;Printf(合并后:nnnew1a=);Disp_Sq(1a);