数据结构源程序.docx
- 文档编号:12144950
- 上传时间:2023-04-17
- 格式:DOCX
- 页数:49
- 大小:21.96KB
数据结构源程序.docx
《数据结构源程序.docx》由会员分享,可在线阅读,更多相关《数据结构源程序.docx(49页珍藏版)》请在冰豆网上搜索。
数据结构源程序
第一章绪论
程序1(抽象数据类型的实现)
#include
#include
#include
typedefintelemtype;
typedefelemtype*triplet;
intinittriplet(triplet&T,elemtypev1,elemtypev2,elemtypev3)
{
T=(elemtype*)malloc(3*sizeof(elemtype));
if(!
T)exit(-2);
T[0]=v1;T[1]=v2;T[2]=v3;
return1;
}
intdestroytriplet(triplet&T)
{
free(T);T=NULL;
return1;
}
intget(tripletT,inti,elemtype&e)
{
if(i<1||i>3)return-1;
e=T[i-1];
return1;
}
intput(triplet&T,inti,elemtypee)
{
if(i<1||i>3)return-1;
T[i-1]=e;
return1;
}
intisascending(tripletT)
{
return(T[0]<=T[1])&&(T[1]<=T[2]);
}
intisdescending(tripletT)
{
return(T[0]>=T[1])&&(T[1]>=T[2]);
}
intmax(tripletT,elemtype&e)
{
e=(T[0]>=T[1])?
((T[0]>=T[2])?
T[0]:
T[2]):
((T[1]>=T[2])?
T[1]:
T[2]);
return1;
}
intmin(tripletT,elemtype&e)
{
e=(T[0]<=T[1])?
((T[0]<=T[2])?
T[0]:
T[2]):
((T[1]<=T[2])?
T[1]:
T[2]);
return1;
}
main()
{
tripletT;
intv1,v2,v3,i,e;
printf("pleasev1v2v3\n");
scanf("%d%d%d",&v1,&v2,&v3);
if(inittriplet(T,v1,v2,v3)&&max(T,e))
printf("%d",e);
}
第二章线性表
程序2(线性表的顺序实现)
#include
#include
#include
#definelist_size100
#definelistincrement10
typedefintelemtype;
typedefstruct
{elemtype*elem;
intlength;
intlistsize;
}sqlist;
voidinitlist(sqlist&L)
{
L.elem=(elemtype*)malloc(list_size*sizeof(elemtype));
if(!
L.elem)exit(-2);
L.length=0;
L.listsize=list_size;
}
voidcreatelist(sqlist&L)
{
inti,m;
printf("pleaseinputm!
\n");
scanf("%d",&m);
for(i=0;i } intlocateelem(sqlistL,inte) {inti; for(i=0;i if(L.elem[i]==e)return1; return0; } voidlistinsert(sqlist&L,inti,inte) {elemtype*newbase; intj; if(i<1||i>L.length+1){printf("errror");exit(-2);} if(L.length>=L.listsize){newbase=(elemtype*)realloc(L.elem,(L.listsize+listincrement)*sizeof(elemtype)); if(! newbase)exit(-2); L.elem=newbase; L.listsize+=L.listsize+listincrement;} for(j=L.length-1;j>=i-1;j--)L.elem[j+1]=L.elem[j]; L.elem[i-1]=e; L.length++; } voidlistdelete(sqlist&L,inti) { intj; if(i<1||i>3){printf("error");exit(-2);} for(j=i-1;j L.length--; } voidoutputlist(sqlistL) { inti; printf("\n"); for(i=0;i printf("\n"); } voidgetelem(sqlistL,inti,elemtype&x) { if(i<1||i>L.length){printf("error! \n");exit(-2);} x=L.elem[i-1]; } voidmergelist(sqlistLa,sqlistLb,sqlist&Lc) { inti,j,k,x,y; initlist(Lc); i=1;j=1;k=0; while((i<=La.length)&&(j<=Lb.length)) { getelem(La,i,x); getelem(Lb,j,y); if(x<=y){listinsert(Lc,++k,x);++i;} else{listinsert(Lc,++k,y);++j;} } while(i<=La.length){getelem(La,i++,x);listinsert(Lc,++k,x);} while(j<=Lb.length){getelem(Lb,j++,y);listinsert(Lc,++k,y);} } voidunionlist(sqlist&La,sqlistLb) { inti,x; for(i=1;i<=Lb.length;i++) { getelem(Lb,i,x); if(! locateelem(La,x)) listinsert(La,++La.length,x); } } main() {intj,e; sqlistLa,Lb,Lc; initlist(La); createlist(La); outputlist(La); initlist(Lb); createlist(Lb); outputlist(Lb); mergelist(La,Lb,Lc); outputlist(Lc); unionlist(La,Lb); outputlist(La); printf("pleaseinputinsertingjande\n"); scanf("%d%d",&j,&e); listinsert(Lc,j,e); outputlist(Lc); printf("pleaseinputdeletingj\n"); scanf("%d",&j); listdelete(Lc,j); outputlist(Lc); getch(); } 程序3(线性表的链式实现) #include #include #include typedefintelemtype; typedefstructLnode {elemtypedata; structLnode*next; }Lnode,*linklist; voidcreatelist(linklist&L,intn) {inti; linklistp; L=(linklist)malloc(sizeof(Lnode)); L->next=NULL; for(i=n;i>0;--i) { p=(linklist)malloc(sizeof(Lnode)); scanf("%d",&p->data); p->next=L->next;L->next=p; } } voidgetelem(linklistL,inti,elemtype&x) { intj; linklistp; p=L->next; j=1; while(p&&jnext;++j;} if(! p||j>i){printf("error! \n");exit(-2);} x=p->data; } intlocateelem(linklistL,elemtypee) {linklistp; p=L->next; while(p){if(p->data==e)return1;p=p->next;} return0; } voidlistinsert(linklist&L,inti,inte) {linklistp,s; intj; p=L; j=0; while(p&&j if(! p||j>i-1)exit(-2); s=(linklist)malloc(sizeof(Lnode)); s->data=e; s->next=p->next; p->next=s; } voidlistdelete(linklist&L,inti) {linklistp,q; intj; p=L;j=0; while(p->next&&j if(! (p->next)||j>i-1)exit(-2); q=p->next;p->next=q->next; free(q); } voidoutputlist(linklistL) { linklistp; p=L->next; while(p){printf("%d",p->data);p=p->next;} printf("\n"); } voidmergelist(linklist&La,linklist&Lb,linklist&Lc) { linklistpa,pb,pc; pa=La->next;pb=Lb->next; Lc=pc=La; while(pa&&pb) {if(pa->data<=pb->data) {pc->next=pa;pc=pa;pa=pa->next;} else{pc->next=pb;pc=pb;pb=pb->next;} } pc->next=pa? pa: pb; free(Lb); } main() {linklistL,La,Lb,Lc; inti,e,n; printf("pleaseinputn! \n"); scanf("%d",&n); createlist(L,n); outputlist(L); printf("pleaseinputiande! \n"); scanf("%d%d",&i,&e); listinsert(L,i,e); outputlist(L); printf("pleaseinputi! \n"); scanf("%d",&i); listdelete(L,i); outputlist(L); printf("pleaseoutputielementoflinklist! \n"); scanf("%d",&i); getelem(L,i,e); printf("the%delementoflinklistis%d! \n",i,e); printf("pleaseinputelementofsearcher! "); scanf("%d",&e); if(locateelem(L,e))printf("%dissearched! \n",e); elseprintf("notfound\n"); printf("pleaseinputn! \n"); scanf("%d",&n); createlist(La,n); printf("pleaseinputn! \n"); scanf("%d",&n); createlist(Lb,n); mergelist(La,Lb,Lc); outputlist(Lc); getch(); } 程序4(静态链表的实现) #include #include #include #definemaxsize100 typedefstruct {chardata; intcur; }component,slinklist[maxsize]; voidinitlist(slinklistL) {inti; for(i=0;i L[maxsize-1].cur=0; } intmalloclist(slinklistL) { inti; i=L[0].cur; if(L[0].cur)L[0].cur=L[i].cur; returni; } voidfreelist(slinklistL,intk) { L[k].cur=L[0].cur; L[0].cur=k; } voidoutputlist(slinklistL) {intk; k=L[1].cur; printf("\n"); while(k){printf("%c",L[k].data);k=L[k].cur;} printf("\n"); } voiddifference(slinklistL,int&s) { intr,m,n,i,j,b,p,k; initlist(L); s=malloclist(L); r=s; printf("pleaseinputmandn! \n"); scanf("%d%d",&m,&n); for(j=1;j<=m;++j) { i=malloclist(L); scanf("%s",&L[i].data); L[r].cur=i;r=i; } L[r].cur=0; outputlist(L); for(j=1;j<=n;++j) { scanf("%s",&b); p=s; k=L[s].cur; while(k! =L[r].cur&&L[k].data! =b) {p=k;k=L[k].cur;} if(k==L[r].cur) {i=malloclist(L);L[i].data=b;L[i].cur=L[r].cur;L[r].cur=i;} else {L[p].cur=L[k].cur;freelist(L,k); if(r==k)r=p; } } } main() { slinklistL; ints; difference(L,s); outputlist(L); getch(); } 程序5(多项式相加的实现) #include #include #include typedefstruct { floatcoef; intexpn; }term,elemtype; typedefstructLnode { elemtypedata; structLnode*next; }Lnode,*linklist; voidinitlist(linklist&L) { L=(linklist)malloc(sizeof(Lnode)); if(! L)exit(-2); L->next=NULL; } voidcreatelist(linklist&L,intm) { linklistp; inti,y; floatx; initlist(L); for(i=0;i { p=(linklist)malloc(sizeof(Lnode)); printf("pleaseinputdata.coefanddata.expn: forexample(1,19),(2,9),(3,3)! \n"); scanf("%f%d",&x,&y); p->data.coef=x; p->data.expn=y; p->next=L->next; L->next=p; } } voidoutputlist(linklistL) { linklistp; p=L->next; while(p) { printf("(%f,%d)\n",p->data.coef,p->data.expn); p=p->next; } } voidaddlist(linklist&La,linklist&Lb,linklist&Lc) { linklistpa,pb,pc; floatsum; pa=La->next; pb=Lb->next; pc=Lc=La; while(pa&&pb) { if(pa->data.expn elseif(pa->data.expn>pb->data.expn){pc->next=pb;pc=pb;pb=pb->next;} else {sum=pa->data.coef+pb->data.coef; if(sum==0.0){pa=pa->next;pb=pb->next;} else{pa->data.coef=sum;pc->next=pa;pc=pa;pa=pa->next;pb=pb->next;} } } pc->next=pa? pa: pb; free(Lb); } voidmain() { linklistLa,Lb,Lc; intm; printf("pleaseinputmofLa! \n"); scanf("%d",&m); createlist(La,m); outputlist(La); printf("pleaseinputmofLb! \n"); scanf("%d",&m); createlist(Lb,m); outputlist(Lb); printf("Lc\n"); addlist(La,Lb,Lc); outputlist(Lc); getch(); } 第三章栈和队列 程序6(栈的实现及其应用) #include #include #include #definestack_size100 #definestack_increment10 typedefintelemtype; typedefstruct { elemtype*base; elemtype*top; intstacksize; }sqstack; voidinitsqstack(sqstack&S) { S.base=(elemtype*)malloc(stack_size*sizeof(elemtype)); if(! S.base)exit(-2); S.top=S.base; S.stacksize=stack_size; } voidcreatesqstack(sqstack&S,intm) { inti,x; for(i=0;i { printf("pleaseinputelemtype! \n"); scanf("%d",&x); *S.top=x; S.top++; } } voidoutputsqstack(sqstackS) { while(S.top! =S.base)printf("%d",*--S.top); } voidgettop(sqstackS,elemtype&e) { if(S.top! =S.base)e=*(S.top-1); } voidpush(sqstack&S,elemtypee) { if(S.top-S.base>=S.stacksize) { S.base=(elemtype*)realloc(S.base,(S.stacksize+stack_increment)*sizeof(elemtype)); if(! S.base)exit(-2); S.top=S.base+S.stacksize; S.stacksize+=stack_increment; } *S.top++=e; } voidpop(sqstack&S,elemtype&e) { if(S.top! =S.base)e=*--S.top; } intstackempty(sqstackS) { if(S.top==S.base)return1; return0; } voidconversion() { sqstackT; intn,e; initsqstack(T); printf("\npleaseinputN! \n"); scanf("%d",&n); while(n){push(T,n%8);n=n/8;} while(! stackempty(T)){
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 源程序