《数据结构代码.docx》由会员分享,可在线阅读,更多相关《数据结构代码.docx(27页珍藏版)》请在第壹文秘上搜索。
1、数据结构代码P2,例2-1voidunion(List&La,UstLb)1.aJen=ListLength(La);LbJen=UStLerIgth(Lb);for(i=l;i=Lb_len;i+)GetElem(Lb,i,e);if(!LocateElem(La,e,equal)UStIrlSert(La,+Lajen,e);P21例2-2,将voidMergeLiSt(L运tLa,ListLb,List&Lc)InitList(Lc);i=j=l;k=O;1.aen=ListLength(La);Lb_len=ListLength(Lb);while(i=La_Len)&(j=Lb_le
2、n)GetElem(La,i,ai);GetElem(Lb,j,bj);if(ai=bj)ListInsert(Lc,+k,ai);+i;elseListInsert(Lc,+k,bj);+jwhile(i=LaJen)GetElem(La,i+,ai);ListInsert(Lc,+k,ai);while(j=Lb_len)GetElem(Lb,i+,bj);ListInsert(Lc,+k,bj);P22,线性表的依次存储结构#defineLIST_INIT_SIZE1OO#defineLISTINCREMENT10typedefstructElemType*elem;*线性表占用的数组空
3、间*/intlength;intlistsize;SqList;初始化操作StatusIniL运t_Sq(SqLiSt&L)1.elem=(ElemTypeA)malloc(LIST_INIT_SIZE*sizeof(ElemType);if(!L.elem)exit(OVERFLOW);1.Iength=O;1.listsize=LISTJNIT,SIZE;returnOK;P24,在依次表里插入一个元素StatusL运HnserJsq(SqLiSt&L,inti,ElemTypee)if(i=L.length+l)returnERROR;if(L.length=L.listsize)new
4、base=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizf(ElemType);if(!newbase)exit(OVERFLOW);1.elem=newbase;1.listsize+=LISTINCREMENT;q=&(L.elemi-l);for(p=&(L.elemL.length-l);p=q;p)*(p+l)=*p;*q=e;+L.length;returnOK;P24,在依次表里删除一个元素StatusLiStDelete_Sq(SqL运t&L,inti,ElemType&e)if(iL.length)return
5、ERROR;p=&(L.elemi-l);e=*p;q=L.elemL.length-1;for(+p;p=q;+p)*(p-l)=*p;L.length;returnOK;P25,在依次表里查找一个元素intLocatElem_Sq(SqListLjElemTypee,Status(*compare)(ElemType,ElemType)i=l;p=L.elem;while(i=L.length&!(*compare)(*p+,e)+i;if(i=L.length)returni;elsereturn0;P26,依次表的合并voidMergeL运t_Sq(SqL运tLa,SqListLb,S
6、qList&Lc)pa=La.elem;pb=Lb.elem;1.c.Iistsize=Lc.Iength=La.length+Lb.length;pc=Lc.elem=(ElemType*)malloc(Lc.listsize*sizeof(ElemTpe);if(!Lc.elem)exit(OVERFLOW);paast=La.elem+La.length-1;pb_last=Lb.elem+Lb.length-1;while(pa=pajast&pb=pbjast)if(*pa=*pb)*pc+=*pa+;else*pc+=*pb+;while(pa=pajast)*pc+=*pa+;w
7、hile(pbnext;j=l;while(p&jnext;+j;if(!pIIji)returnERROR;e=p-data;returnok;P29,在单链表中插入一个元素StatusListInsert_L(LinkList&L,inti,ElmeTypee)p=L;j=O;while(p&jnext;+j;if(!pIIji-l)returnERROR;s=(LinkList)malloc(sizf(LNode);s-data=e;s-next=p-next;p-next=s;returnOK;P30,在单链表中删除一个元素StatusListDelete_L(LinkList&L,i
8、nti,Elemtype&e)p=L;j=O;while(p-next&jnext;+j;if(!(p-next)ji-l)returnERROR;q=p-next;p-next=q-next;e=q-data;free(q);returnOK;P30,建立单链表voidCreateList_L(LinkList&L,intn)1.=(Linklist)malloc(sizeof(Lnode);1.-next=NULL;for(i=n;i0;i)p=(LinkList)malloc(sizeof(Lnode);scanf(&p-data);p-next=L-next;1.-next=p;P31
9、,合并单链表voidmergelist_L(LinkList&La,LinkList&Lb,LinkLiSt&Lc)pa=La-next;pb=Lb-next;1.c=pc=La;while(pa&pb)if(pa-datadata)pc-next=pa;pc=pa;pa=pa-next;)else(pc-next=pb;pc=pb;pb=pb-next;pc-next=pa?pa:pb;free(Lb);P3线性表的静态单链表存储结构#defineMAXSIZE1000typedefstructElemTypedata;intcur;component,SlinkListMAXSIZE;P3
10、2,定位函数intLocateElem_SL(SlinkLists,ElemTypee)i=sO.cur;while(i&si.data!=e)i=si.cur;returni;voidIniteSpace_SL(SlinkList&space)for(i=0;i=S.stacksize)S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType);if(!S.base)exit(OVERFLOW);S.top=S.base+S.stacksize;S.stacksize+=STACKINCRE
11、MENT;*S.top+=e;returnOK;StatusPop(SqStack&S,SelemType&e)if(S.top=S.base)returnERROR;e=*S.top;returnOK;P48,转换为8进制voidconversion()InitStack(三);SCanf(“d”,&N);while(N)(Push(s,N%8);N=N/8;while(!StackEmpty(s)Pop(S,e);Printf(%d,e);P55,移动圆盘voidhanoi(intn,charx,chary,charz)*将塔座X上按直径由小到大且至上而下编号为1至n的n个圆盘按规则搬到塔座Z,Y可用作协助塔座*/if(n=l)move(x,l,z);/*将编号为1的圆盘从X移动Z*/else(hanoi(n-l,x,z,y);/*将X上编号为1至II-I的圆盘移到Y,Z作协助塔*/move(x,n,z);/*将编号为n的圆盘从X移到Z*/hanoi(n-l,y,x,z);