数据结构链表的实现和应用.docx
- 文档编号:23166524
- 上传时间:2023-05-15
- 格式:DOCX
- 页数:26
- 大小:71.71KB
数据结构链表的实现和应用.docx
《数据结构链表的实现和应用.docx》由会员分享,可在线阅读,更多相关《数据结构链表的实现和应用.docx(26页珍藏版)》请在冰豆网上搜索。
数据结构链表的实现和应用
江南大学物联网工程学院上机报告
课程名称数据结构上机名称链表的实现和应用上机日期2016-3-11
班级计科1505姓名学号
上机报告要求1.上机名称2.上机要求3.上机环境4.程序清单(写明运行结果)5.上机体会
1.上机名称
链表的实现和应用
2.上机要求
⑴定义线性表的链式存储表示;
⑵基于所设计的存储结构实现线性表的基本操作;
⑶编写一个主程序对所实现的线性表进行测试;
⑷线性表的应用:
①设线性表L1和L2分别代表集合A和B,试设计算法求A和B的并集C,并用线性表L3代表集合C;②设线性表L1和L2中的数据元素为整数,且均已按值非递减有序排列,试设计算法对L1和L2进行合并,用线性表L3保存合并结果,要求L3中的数据元素也按值非递减有序排列。
⑸设计一个一元多项式计算器,要求能够:
①输入并建立多项式;②输出多项式;③执行两个多项式相加;④执行两个多项式相减;⑤(选做)执行两个多项式相乘。
3.上机环境
VC6.0
4.程序清单(写明运行结果)
程序1:
#include
#include
#include
#include
#defineTRUE1
#defineFALSE0
#defineOK1
#defineERROR0
#defineINFEASIBLE-1
typedefintStatus;
typedefintBoolean;
typedefintElemType;
structLNode
{
ElemTypedata;
structLNode*next;
};
typedefstructLNode*LinkList;
StatusInitList(LinkList*L)
{
*L=(LinkList)malloc(sizeof(structLNode));
if(!
*L)
exit(OVERFLOW);
(*L)->next=NULL;
returnOK;
}
StatusDestroyList(LinkList*L)
{
LinkListq;
while(*L)
{
q=(*L)->next;
free(*L);
*L=q;
}
returnOK;
}
StatusClearList(LinkListL)
{
LinkListp,q;
p=L->next;
while(p)
{
q=p->next;
free(p);
p=q;
}
L->next=NULL;
returnOK;
}
StatusListEmpty(LinkListL)
{
if(L->next)
returnFALSE;
else
returnTRUE;
}
intListLength(LinkListL)
{
inti=0;
LinkListp=L->next;
while(p)
{
i++;
p=p->next;
}
returni;
}
StatusGetElem(LinkListL,inti,ElemType*e)
{
intj=1;
LinkListp=L->next;
while(p&&j
{
p=p->next;
j++;
}
if(!
p||j>i)
returnERROR;
*e=p->data;
returnOK;
}
intLocateElem(LinkListL,ElemTypee,Status(*compare)(ElemType,ElemType))
{
inti=0;
LinkListp=L->next;
while(p)
{
i++;
if(compare(p->data,e))
returni;
p=p->next;
}
return0;
}
StatusPriorElem(LinkListL,ElemTypecur_e,ElemType*pre_e)
{
LinkListq,p=L->next;
while(p->next)
{
q=p->next;
if(q->data==cur_e)
{
*pre_e=p->data;
returnOK;
}
p=q;
}
returnINFEASIBLE;
}
StatusNextElem(LinkListL,ElemTypecur_e,ElemType*next_e)
{
LinkListp=L->next;
while(p->next)
{
if(p->data==cur_e)
{
*next_e=p->next->data;
returnOK;
}
p=p->next;
}
returnINFEASIBLE;
}
StatusListInsert(LinkListL,inti,ElemTypee)
{
intj=0;
LinkListp=L,s;
while(p&&j { p=p->next; j++; } if(! p||j>i-1) returnERROR; s=(LinkList)malloc(sizeof(structLNode)); s->data=e; s->next=p->next; p->next=s; returnOK; } StatusListDelete(LinkListL,inti,ElemType*e) { intj=0; LinkListp=L,q; while(p->next&&j { p=p->next; j++; } if(! p->next||j>i-1) returnERROR; q=p->next; p->next=q->next; *e=q->data; free(q); returnOK; } StatusListTraverse(LinkListL,void(*vi)(ElemType)) { LinkListp=L->next; while(p) { vi(p->data); p=p->next; } printf("\n"); returnOK; } Statusequal(ElemTypec1,ElemTypec2) { if(c1==c2) returnTRUE; else returnFALSE; } voidUnion(LinkListLa,LinkListLb) { ElemTypee; intLa_len,Lb_len; inti; La_len=ListLength(La); Lb_len=ListLength(Lb); for(i=1;i<=Lb_len;i++) { GetElem(Lb,i,&e); if(! LocateElem(La,e,equal)) ListInsert(La,++La_len,e); } } voidprint(ElemTypec) { printf("%d",c); } voidmain() { LinkListLa,Lb; Statusi; intj; i=InitList(&La); if(i==1) for(j=1;j<=5;j++) i=ListInsert(La,j,j); printf("La="); ListTraverse(La,print); InitList(&Lb); for(j=1;j<=5;j++) i=ListInsert(Lb,j,2*j); printf("Lb="); ListTraverse(Lb,print); Union(La,Lb); printf("newLa="); ListTraverse(La,print); } 运行结果: 程序2: #include #include #include #include #include #defineTRUE1 #defineFALSE0 #defineOK1 #defineERROR0 #defineINFEASIBLE-1 typedefintStatus; typedefintBoolean; typedefintElemType; structLNode { ElemTypedata; structLNode*next; }; typedefstructLNode*LinkList; StatusInitList(LinkList*L) { *L=(LinkList)malloc(sizeof(structLNode)); if(! *L) exit(OVERFLOW); (*L)->next=NULL; returnOK; } StatusDestroyList(LinkList*L) { LinkListq; while(*L) { q=(*L)->next; free(*L); *L=q; } returnOK; } StatusClearList(LinkListL) { LinkListp,q; p=L->next; while(p) { q=p->next; free(p); p=q; } L->next=NULL; returnOK; } StatusListEmpty(LinkListL) { if(L->next) returnFALSE; else returnTRUE; } intListLength(LinkListL) { inti=0; LinkListp=L->next; while(p) { i++; p=p->next; } returni; } StatusGetElem(LinkListL,inti,ElemType*e) { intj=1; LinkListp=L->next; while(p&&j { p=p->next; j++; } if(! p||j>i) returnERROR; *e=p->data; returnOK; } intLocateElem(LinkListL,ElemTypee,Status(*compare)(ElemType,ElemType)) { inti=0; LinkListp=L->next; while(p) { i++; if(compare(p->data,e)) returni; p=p->next; } return0; } StatusPriorElem(LinkListL,ElemTypecur_e,ElemType*pre_e) { LinkListq,p=L->next; while(p->next) { q=p->next; if(q->data==cur_e) { *pre_e=p->data; returnOK; } p=q; } returnINFEASIBLE; } StatusNextElem(LinkListL,ElemTypecur_e,ElemType*next_e) { LinkListp=L->next; while(p->next) { if(p->data==cur_e) { *next_e=p->next->data; returnOK; } p=p->next; } returnINFEASIBLE; } StatusListInsert(LinkListL,inti,ElemTypee) { intj=0; LinkListp=L,s; while(p&&j { p=p->next; j++; } if(! p||j>i-1) returnERROR; s=(LinkList)malloc(sizeof(structLNode)); s->data=e; s->next=p->next; p->next=s; returnOK; } StatusListDelete(LinkListL,inti,ElemType*e) { intj=0; LinkListp=L,q; while(p->next&&j { p=p->next; j++; } if(! p->next||j>i-1) returnERROR; q=p->next; p->next=q->next; *e=q->data; free(q); returnOK; } StatusListTraverse(LinkListL,void(*vi)(ElemType)) { LinkListp=L->next; while(p) { vi(p->data); p=p->next; } printf("\n"); returnOK; } voidMergeList(LinkListLa,LinkListLb,LinkList*Lc) { inti=1,j=1,k=0; intLa_len,Lb_len; ElemTypeai,bj; InitList(Lc); La_len=ListLength(La); Lb_len=ListLength(Lb); while(i<=La_len&&j<=Lb_len) { GetElem(La,i,&ai); GetElem(Lb,j,&bj); if(ai<=bj) { ListInsert(*Lc,++k,ai); ++i; } else { ListInsert(*Lc,++k,bj); ++j; } } while(i<=La_len) { GetElem(La,i++,&ai); ListInsert(*Lc,++k,ai); } while(j<=Lb_len) { GetElem(Lb,j++,&bj); ListInsert(*Lc,++k,bj); } } voidprint(ElemTypec) { printf("%d",c); } voidmain() { LinkListLa,Lb,Lc; intj,a[4]={3,5,8,11},b[7]={2,6,8,9,11,15,20}; InitList(&La); for(j=1;j<=4;j++) ListInsert(La,j,a[j-1]); printf("La="); ListTraverse(La,print); InitList(&Lb); for(j=1;j<=7;j++) ListInsert(Lb,j,b[j-1]); printf("Lb="); ListTraverse(Lb,print); MergeList(La,Lb,&Lc); printf("Lc="); ListTraverse(Lc,print); } 运行结果: 程序3: #include #include typedefstructPolynomial{ floatcoef; intexpn; structPolynomial*next; }*Polyn,Polynomial; voidInsert(Polynp,Polynh){ if(p->coef==0)free(p); else { Polynq1,q2; q1=h; q2=h->next; while(q2&&p->expn { q1=q2; q2=q2->next; } if(q2&&p->expn==q2->expn) { q2->coef+=p->coef; free(p); if(! q2->coef) { q1->next=q2->next; free(q2); } } else { p->next=q2; q1->next=p; } } } PolynCreatePolyn(Polynhead,intm){ inti; Polynp; p=head=(Polyn)malloc(sizeof(structPolynomial)); head->next=NULL; for(i=0;i { p=(Polyn)malloc(sizeof(structPolynomial)); printf("请输入第%d项的系数与指数: ",i+1); scanf("%f%d",&p->coef,&p->expn); Insert(p,head); } returnhead; } voidDestroyPolyn(Polynp){ Polynq1,q2; q1=p->next; q2=q1->next; while(q1->next) { free(q1); q1=q2; q2=q2->next; } } voidPrintPolyn(PolynP){ Polynq=P->next; intflag=1; if(! q) { putchar('0'); printf("\n"); return; } while(q) { if(q->coef>0&&flag! =1)putchar('+'); if(q->coef! =1&&q->coef! =-1) { printf("%g",q->coef); if(q->expn==1)putchar('X'); elseif(q->expn)printf("X^%d",q->expn); } else { if(q->coef==1) { if(! q->expn)putchar('1'); elseif(q->expn==1)putchar('X'); elseprintf("X^%d",q->expn); } if(q->coef==-1) { if(! q->expn)printf("-1"); elseif(q->expn==1)printf("-X"); elseprintf("-X^%d",q->expn); } } q=q->next; flag++; } printf("\n"); } intcompare(Polyna,Polynb){ if(a&&b) { if(! b||a->expn>b->expn)return1; elseif(! a||a->expn elsereturn0; } elseif(! a&&b)return-1; elsereturn1; } PolynAddPolyn(Polynpa,Polynpb){ Polynqa=pa->next; Polynqb=pb->next; Polynheadc,hc,qc; hc=(Polyn)malloc(sizeof(structPolynomial)); hc->next=NULL; headc=hc; while(qa||qb) { qc=(Polyn)malloc(sizeof(structPolynomial)); switch(compare(qa,qb)){ case1: { qc->coef=qa->coef; qc->expn=qa->expn; qa=qa->next; break; } case0: { qc->coef=qa->coef+qb->coef; qc->expn=qa->expn; qa=qa->next; qb=qb->next; break; } case-1: { qc->coef=qb->coef; qc->expn=qb->expn; qb=qb->next; break; } } if(qc->coef! =0) { qc->next=hc->next; hc->next=qc; hc=qc; } elsefree(qc); } returnheadc; } PolynSubtractPolyn(Polynpa,Polynpb){ Polynh=pb; Polynp=pb->next; Polynpd; while(p) { p->coef*=-1; p=p->next; } pd=AddPolyn(pa,h); for(p=h->next;p;p=p->next) p->coef*=-1; returnpd; } PolynMul
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 实现 应用