合肥工业大学数据结构试验一实验报告Word文档下载推荐.docx

上传人:b****4 文档编号:17074705 上传时间:2022-11-28 格式:DOCX 页数:19 大小:49.73KB
下载 相关 举报
合肥工业大学数据结构试验一实验报告Word文档下载推荐.docx_第1页
第1页 / 共19页
合肥工业大学数据结构试验一实验报告Word文档下载推荐.docx_第2页
第2页 / 共19页
合肥工业大学数据结构试验一实验报告Word文档下载推荐.docx_第3页
第3页 / 共19页
合肥工业大学数据结构试验一实验报告Word文档下载推荐.docx_第4页
第4页 / 共19页
合肥工业大学数据结构试验一实验报告Word文档下载推荐.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

合肥工业大学数据结构试验一实验报告Word文档下载推荐.docx

《合肥工业大学数据结构试验一实验报告Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《合肥工业大学数据结构试验一实验报告Word文档下载推荐.docx(19页珍藏版)》请在冰豆网上搜索。

合肥工业大学数据结构试验一实验报告Word文档下载推荐.docx

(软件类实验:

所采用的核心方法、框架或流程图及程序清单)

实验准备方案:

构建库函数:

定义了链表结构中的指针类型为link,结点类型为node,并定义了部分常用运算,如构建链表,显示链表,读取链表,访问链表等;

流程:

实验内容

一、实验用仪器、设备:

个人计算机

C-free5.0

二、实验内容与步骤(过程及数据记录):

<

1>

求链表中第i个结点的指针(函数),若不存在,则返回NULL。

实验测试数据基本要求:

第一组数据:

链表长度n≥10,i分别为5,n,0,n+1,n+2

第二组数据:

链表长度n=0,i分别为0,2

node*list:

:

address(inti)

{

node*p=head->

next;

intn=1;

while(n!

=i&

&

p!

=NULL)

{

p=p->

n++;

}

if(p!

=NULL)returnp;

elsereturnNULL;

}

第一组数据

第二组数据

2>

在第i个结点前插入值为x的结点。

链表长度n≥10,x=100,i分别为5,n,n+1,0,1,n+2

链表长度n=0,x=100,i=5

errorcodelist:

insert(constinti,constintx)

node*p;

p=head;

if(i<

1||i>

length()+1)returnrangeerror;

node*s=newnode;

s->

data=x;

next=p->

p->

next=s;

count++;

returnsuccess;

3>

删除链表中第i个元素结点。

链表长度n≥10,i分别为5,n,1,n+1,0

链表长度n=0,i=5

delete_ele(constinti)

if(i<

1||i>

count)returnrangeerror;

node*u;

u=p->

next=u->

count--;

deleteu;

4>

在一个递增有序的链表L中插入一个值为x的元素,并保持其递增有序特性。

链表元素为(10,20,30,40,50,60,70,80,90,100),

x分别为25,85,110和8

orderinsert(intx)

node*p=head;

while(p->

next!

if(p->

next->

data<

x)p=p->

elsebreak;

node*u=newnode;

u->

next=u;

5>

将单链表L中的奇数项和偶数项结点分解开,并分别连成一个带头结点的单链表,然后再将这两个新链表同时输出在屏幕上,并保留原链表的显示结果,以便对照求解结果。

链表元素为(1,2,3,4,5,6,7,8,9,10,20,30,40,50,60)

链表元素为(10,20,30,40,50,60,70,80,90,100)

voidseparate(list&

A,list&

B,list&

C){

node*LA;

node*LB;

node*p;

node*q;

node*u;

node*s;

LA=A.get_head();

LB=B.get_head();

q=LA;

p=LA->

s=LB;

if(p->

data%2==0){

u=p;

p=p->

q->

next=p;

s->

next=u;

s=s->

}

else{

p=p->

q=q->

6>

求两个递增有序链表L1和L2中的公共元素,并以同样方式连接成链表L3。

第一组

第一个链表元素为(1,3,6,10,15,16,17,18,19,20)

第二个链表元素为(1,2,3,4,5,6,7,8,9,10,18,20,30)

第二组

第二个链表元素为(2,4,5,7,8,9,12,22)

第三组

第一个链表元素为()

第二个链表元素为(1,2,3,4,5,6,7,8,9,10)

bingji(listA,listB,list&

node*LB;

node*LC;

node*a;

node*b;

LC=C.get_head();

a=LA->

b=LB->

while(a!

=NULL&

b!

=NULL){

if(a->

data<

b->

data)a=a->

elseif(a->

data>

data)b=b->

else{

node*c=newnode;

c->

data=a->

data;

LC->

next=c;

LC=c;

C.count++;

a=a->

b=b->

LC->

next=NULL;

CPP文件附加:

#include<

iostream.h>

#include<

math.h>

enumerror_code{success,arrange_error};

typedefstructnode{

intdata;

node*next;

}node;

classlist{

public:

list();

intlength()const;

~list(){};

node*get_element(intlocate)const;

node*locate(constintx)const;

error_codecharu(constinti);

error_codeinsert(constintlocate,constinti);

error_codedelete_element(constinti);

node*get_head(){returnhead;

voidseparate(list&

B);

intbingji(listA,listB,list&

C);

voidcreate_R();

voidlist:

show();

private:

intcount;

node*head;

node*rear;

};

list:

list(){

head=newnode;

head->

count=0;

}

intlist:

length()const{

node*p=head->

intcount=0;

while(p!

p=p->

returncount;

voidlist:

create_R(){

intx;

cout<

"

请输入链表中的数值,按-1后结束创建"

endl;

cin>

>

x;

node*rear=head;

while(x!

=-1){

node*s=newnode;

data=x;

rear->

next=s;

rear=s;

}

node*list:

get_element(intlocate)const{

if(count==0)return0;

else{

if(locate<

=0||locate>

=count)

return0;

else{

node*p=head;

intk=0;

k<

locate){

p=p->

k++;

returnp;

show(){

node*p=head;

while(p!

cout<

p->

\t"

;

error_codelist:

insert(constintlocate,constinti){

if(count==0){

node*s=newnode;

s->

data=i;

rear=s;

count=1;

returnsuccess;

if(locate<

1||locate>

count+1)

returnarrange_error;

node*p=head;

intj=0;

while(j!

=locate-1&

p!

j++;

next=p->

p->

count++;

charu(constinti){

while(p!

next!

if(p->

=i&

i<

=p->

data){

elsep=p->

if(p->

next==NULL){

delete_element(constinti){

node*p=head;

intj=0;

while(j!

=i-1&

if(i<

1||i>

count)

returnarrange_error;

node*u=newnode;

u=p->

next=u->

B){

intlist:

bingji(listA,listB,list&

returnsuccess;

intmain()

intchoice;

inti;

listA;

listB;

listC;

do

{//显示主菜单

\n"

主菜单\n"

***********************************************"

1-创建链表2-求第i个节点指针\n"

3-在第i个节点前插入一个数4-删除链表中的第i个节点\n"

5-分离链表6-求公共元素\n"

7-插入一个数8-退出\n"

Enterchoice:

cin>

choice;

switch(choice)

{

case1:

A.create_R();

B.create_R();

A.length();

B.length();

break;

}

case2:

intk;

cout<

qingshuruk\n"

cin>

k;

if(A.get_element(k)==NULL)

NULL<

else

A.get_element(k)->

break;

case3:

A.length();

inta,b;

请输入a,b\n"

a>

b;

A.insert(a,b);

A.show();

case4:

inti;

请输入一个值\n"

i;

if(i==0||i>

A.length())

NULL\n"

A.delete_element(i);

break;

case5:

A.show();

separate(A,B);

B.show();

case6:

A.bingji(A,B,C);

C.show();

case7:

{inti;

请输入一个数\n"

A.charu(i);

}

case8:

cout<

结束运行"

}while(choice!

=7);

return0;

三、实验结果分析、思考题解答∶

四、感想、体会、建议∶

实验成绩∶

指导教师签名:

年月日

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

当前位置:首页 > 工作范文 > 制度规范

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

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