数据结构链表的基本操作附答案.docx

上传人:b****3 文档编号:4869262 上传时间:2022-12-11 格式:DOCX 页数:12 大小:17.42KB
下载 相关 举报
数据结构链表的基本操作附答案.docx_第1页
第1页 / 共12页
数据结构链表的基本操作附答案.docx_第2页
第2页 / 共12页
数据结构链表的基本操作附答案.docx_第3页
第3页 / 共12页
数据结构链表的基本操作附答案.docx_第4页
第4页 / 共12页
数据结构链表的基本操作附答案.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

数据结构链表的基本操作附答案.docx

《数据结构链表的基本操作附答案.docx》由会员分享,可在线阅读,更多相关《数据结构链表的基本操作附答案.docx(12页珍藏版)》请在冰豆网上搜索。

数据结构链表的基本操作附答案.docx

数据结构链表的基本操作附答案

实验三链表的基本操作

一、实验目的

1.进一步掌握用VisualC++6.0上机调试程序的方法。

2.掌握单链表的各种基本操作,包括单链表的建立和查找。

二、实验内容

1.创建一个链表,结点包括:

学号、姓名

2.链表的插入

3.链表的删除

4.显示链表中结点

三、参考代码

#include

#include

#defineNULL0

#defineTYPEstructstu

#defineLENsizeof(structstu)

structstu

{

intnum;

charname[15];

structstu*next;

};

TYPE*creat(intn)

{

structstu*head,*pf,*pb;

inti;

for(i=0;i

{

pb=(TYPE*)malloc(LEN);

printf("inputNumberandName\n");

scanf("%d%s",&pb->num,&pb->name);

if(i==0)

pf=head=pb;

elsepf->next=pb;

pb->next=NULL;

pf=pb;

}

return(head);

}

TYPE*delet(TYPE*head,intnum)

{

TYPE*pf,*pb;

if(head==NULL)

{printf("\nemptylist!

\n");

gotoend;}

pb=head;

while(pb->num!

=num&&pb->next!

=NULL)

{pf=pb;_______________;}

if(pb->num==num)

{if(pb==head)head=pb->next;

else__________________

printf("Thenodeisdeleted\n");}

else

free(pb);

printf("Thenodenotbeenfound!

\n");

end:

returnhead;

}

TYPE*insert(TYPE*head,TYPE*pi)

{

TYPE*pb,*pf;

pb=head;

if(head==NULL)

{head=pi;

_______________________;}

else

{

while((pi->num>pb->num)&&(pb->next!

=NULL))

{pf=pb;

pb=pb->next;}

if(pi->num<=pb->num)

{if(head==pb)head=pi;

elsepf->next=pi;

____________________;}

else

{pb->next=pi;

__________________}

}

returnhead;

}

voidprint(TYPE*head)

{

printf("Number\t\tName\n");

while(head!

=NULL)

{

printf("%d\t\t%s\n",head->num,head->name);

head=head->next;

}

}

Voidmain(void)

{

TYPE*head,*pnum;

intn,num;

printf("inputnumberofnode:

");

scanf("%d",&n);/*输入所建链表的结点数*/

__________________/*建立链表并把头指针返回给head*/

print(head);

printf("Inputthedeletednumber:

");

scanf("%d",&num);/*输入待删结点的学号*/

__________________/*删除一个结点*/

print(head);/*输出链表*/

printf("Inputtheinsertednumberandname:

");

pnum=(TYPE*)malloc(LEN);/*分配一个结点的内存空间*/

scanf("%d%s",&pnum->num,&pnum->name);/*输入待插入结点的数据域值*/

___________________/*插入pnum所指的结点*/

print(head);/*再次调用print函数输出链表*/

}

四、心得体会

#include

#include

typedefstructPolynode

{intcoef;

intexp;

structPolynode*next;

}Polynode,*Polylist;

PolylistPolyCreate()

{Polynode*head;

Polynode*rear,*s;

intc,e;

head=(Polynode*)malloc(sizeof(Polynode));

rear=head;/*rear始终指向单链表的尾,便于尾插法建表*/

printf("输入一元多项式(以0为结束标记):

\n");

printf("要求:

1.按幂从小到大输入各结点:

\n");

printf("2.没有两个结点具有相同的幂:

\n");

printf("pleaseinputcande:

\n");

scanf("%d,%d",&c,&e);/*键入多项式的系数和指数项*/

while(c!

=0)/*若c=0,则代表多项式的输入结束*/

{

s=(Polynode*)malloc(sizeof(Polynode));/*申请新的结点*/

s->coef=c;

s->exp=e;

rear->next=s;/*在当前表尾做插入*/

rear=s;

printf("pleaseinputcande:

again\n");

scanf("%d,%d",&c,&e);

}

rear->next=NULL;/*将表的最后一个结点的next置NULL,以示表结束*/

returnhead;}

PolylistPolyAdd(Polylistpolya,Polylistpolyb)

/*此函数用于将两个多项式相加,然后将和多项式存放在多项式polya中,并将多项式ployb删除*/

{

Polynode*p,*q,*pre,*temp;

intsum;

p=polya->next;/*令p和q分别指向polya和polyb多项式链表中的第一个结点*/

q=polyb->next;

pre=polya;/*r指向和多项式的尾结点*/

while(p!

=NULL&&q!

=NULL)/*当两个多项式均未扫描结束时*/

{

if(p->expexp)

/*如果p指向的多项式项的指数小于q的指数,将p结点加入到和多项式中*/

{

pre->next=p;

pre=p;

p=p->next;

}

else

if(p->exp==q->exp)/*若指数相等,则相应的系数相加*/

{

sum=p->coef+q->coef;

if(sum!

=0)

{

p->coef=sum;//__________________;

pre->next=p;

pre=p;

p=p->next;

temp=q;

q=q->next;//___________________;

free(temp);

}

else

{

temp=p;

p=p->next;

free(temp);

/*若系数和为零,则删除结点p与q,并将指针指向下一个结点*/

temp=q;

q=q->next;//___________________________

free(temp);

}

}

else

{

pre->next=q;

pre=q;/*将q结点加入到和多项式中*/

q=q->next;//_________________;

}

}

if(p!

=NULL)/*多项式A中还有剩余,则将剩余的结点加入到和多项式中*/

pre->next=p;

else/*否则,将B中的结点加入到和多项式中*/

pre->next=q;

return(polya);}

voiddisplay(Polylisthead)

{intfirst=1;

head=head->next;

while(head!

=NULL)

{if(first)

{if(head->exp==1)

printf("%dX",head->coef);

elseif(head->exp==0)

printf("%d",head->coef);

elseprintf("%dX^%d",head->coef,head->exp);

first=0;

}

else{if(head->exp==1)

printf("+%dX",head->coef);

elseif(head->exp==0)printf("+%d",head->coef);

elseprintf("+%dX^%d",head->coef,head->exp);

}

head=head->next;

}printf("\n");

}

voidmain()

{Polynode*poly1,*poly2,*poly3;

poly1=PolyCreate();

display(poly1);

poly2=PolyCreate();

display(poly2);

poly3=PolyAdd(poly1,poly2);

display(poly3);

}

实验四《链表的应用》------求两个一元多项式之和

(一)实验目的

掌握单链表的各种基本操作,包括单链表的建立和查找。

(二)实验内容与要求

1、用单链表存储一元多项式,将两个存储一元多项式的单链表相加产生结果

单链表。

2、假设要求用户按幂从小到大次序输入各结点,并且没有两个结点具有相

同幂,这样一个一元多项式的链表是按幂exp从小到大顺序排列的。

将这

两个有序单链表poly1和poly2按幂值相加得到一个新链表。

(三)算法

1、数据类型定义

结点存储结构定义为:

structpolynode{

intcoef;系数

intexp;幂

structpolynode*next;

};

2、参考代码

#include

#include

typedefstructPolynode

{intcoef;

intexp;

structPolynode*next;

}Polynode,*Polylist;

PolylistPolyCreate()

{Polynode*head;

Polynode*rear,*s;

intc,e;

head=(Polynode*)malloc(sizeof(Polynode));

_____________________/*rear始终指向单链表的尾,便于尾插法建表*/

printf("输入一元多项式(以0为结束标记):

\n");

printf("要求:

1.按幂从小到大输入各结点:

\n");

printf("2.没有两个结点具有相同的幂:

\n");

printf("pleaseinputcande:

\n");

scanf("%d,%d",&c,&e);/*键入多项式的系数和指数项*/

while(c!

=0)/*若c=0,则代表多项式的输入结束*/

{

s=(Polynode*)malloc(sizeof(Polynode));/*申请新的结点*/

s->coef=c;

s->exp=e;

_________________________/*在当前表尾做插入*/

rear=s;

printf("pleaseinputcande:

again\n");

scanf("%d,%d",&c,&e);

}

rear->next=NULL;/*将表的最后一个结点的next置NULL,以示表结束*/

returnhead;}

PolylistPolyAdd(Polylistpolya,Polylistpolyb)

/*此函数用于将两个多项式相加,然后将和多项式存放在多项式polya中,并将多项式ployb删除*/

{

Polynode*p,*q,*pre,*temp;

intsum;

p=polya->next;/*令p和q分别指向polya和polyb多项式链表中的第一个结点*/

q=polyb->next;

pre=polya;/*r指向和多项式的尾结点*/

while(p!

=NULL&&q!

=NULL)/*当两个多项式均未扫描结束时*/

{

if(p->expexp)

/*如果p指向的多项式项的指数小于q的指数,将p结点加入到和多项式中*/

{

pre->next=p;

pre=p;

______________________-

}

else

if(p->exp==q->exp)/*若指数相等,则相应的系数相加*/

{

sum=p->coef+q->coef;

if(sum!

=0)

{

__________________;

pre->next=p;

pre=p;

p=p->next;

temp=q;

___________________;

free(temp);

}

else

{

temp=p;

p=p->next;

free(temp);

/*若系数和为零,则删除结点p与q,并将指针指向下一个结点*/

temp=q;

___________________________

free(temp);

}

}

else

{

pre->next=q;

pre=q;/*将q结点加入到和多项式中*/

_________________;

}

}

if(p!

=NULL)/*多项式A中还有剩余,则将剩余的结点加入到和多项式中*/

pre->next=p;

else/*否则,将B中的结点加入到和多项式中*/

pre->next=q;

return(polya);}

voiddisplay(Polylisthead)

{intfirst=1;

head=head->next;

while(_________________________)

{if(first)

{if(head->exp==1)

printf("%dX",head->coef);

elseif(head->exp==0)

printf("%d",head->coef);

elseprintf("%dX^%d",head->coef,head->exp);

first=0;

}

else{if(head->exp==1)

printf("+%dX",head->coef);

elseif(head->exp==0)printf("+%d",head->coef);

elseprintf("+%dX^%d",head->coef,head->exp);

}

head=head->next;

}printf("\n");

}

voidmain()

{Polynode*poly1,*poly2,*poly3;

poly1=PolyCreate();

display(poly1);

poly2=PolyCreate();

display(poly2);

poly3=PolyAdd(poly1,poly2);

display(poly3);

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > IT计算机 > 计算机软件及应用

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1