数据结构实验报告 顺序表Word文档格式.docx

上传人:b****3 文档编号:18464415 上传时间:2022-12-16 格式:DOCX 页数:18 大小:737.49KB
下载 相关 举报
数据结构实验报告 顺序表Word文档格式.docx_第1页
第1页 / 共18页
数据结构实验报告 顺序表Word文档格式.docx_第2页
第2页 / 共18页
数据结构实验报告 顺序表Word文档格式.docx_第3页
第3页 / 共18页
数据结构实验报告 顺序表Word文档格式.docx_第4页
第4页 / 共18页
数据结构实验报告 顺序表Word文档格式.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

数据结构实验报告 顺序表Word文档格式.docx

《数据结构实验报告 顺序表Word文档格式.docx》由会员分享,可在线阅读,更多相关《数据结构实验报告 顺序表Word文档格式.docx(18页珍藏版)》请在冰豆网上搜索。

数据结构实验报告 顺序表Word文档格式.docx

2.用已有的顺序表初始化另一个顺序表

3.输入顺序表

基本操作:

1.在第i个元素之前插入一个元素

2.判断顺序表是否为空

3.求顺序表中元素的个数

4.取第i个元素

5.查找第一个与之满足compare()关系的元素序号

6.返回某元素的前驱

7.返回某元素的后继

8.删除第i个元素

9.把一个顺序表复制给另一个顺序表

10.把顺序表置空

11.顺序表的运用

六.实验心得

熟悉最基本的数据类型——顺序表,同时我们让我们熟练C++的基本操作,模板的使用,以及模块化的设计思想。

同时也运用顺序表做一些简单的运用,比如顺序表的并交差运算,学生管理系统等等。

在这次的实验中,我掌握了很多C++的特性,在运用顺序表时,由于水平的原因,只是把简单的并交差运算写完,我想通过以后的学习,我们能够将其实现学生管理系统。

在实验中我也遇到很多的问题,输入输出的重载,输出格式的控制等等,在以后的实验中吸取经验和教训,提高自己的水平。

五.实验代码

基类:

SqList.h

//myhead.h包含自己设定的一些常量和类型

#ifndefMYHEAD_H

#defineMYHEAD_H

//#include"

D:

\数据结构C++\实验2\myhead.h"

#include"

\Users\fclz\Documents\VisualStudio2010\Projects\数据结构C++\实验2\myhead.h"

#endif

//顺序表的一些常量说明

#defineLIST_MAX_SIZE100

#defineLISTINCERMENT10

//随机数生成必须

#define_CRT_RAND_S

#include<

stdlib.h>

stdio.h>

limits.h>

//顺序表数据结构的C++类的声明(基类)

template<

typenameElemType>

classSqList

{

protected:

ElemType*elem;

intlistSize;

intn;

public:

//构造函数,析构函数,拷贝构造函数的声明

SqList();

virtual~SqList();

SqList(constSqList<

ElemType>

&

otherL);

//顺序表的方法

//有序顺序表的折半查找

intbin_Search(ElemTypekey);

//把顺序表置空

voidclear();

//删除第i个元素

StatusdeleteElem(inti,ElemType&

e);

//取第i个元素

intgetElem(inti,ElemType&

//求顺序表中元素的个数

intgetLength();

//求顺序表存储空间的大小

intgetListSize();

//在第i个元素之前插入一个元素

Statusinsert(inti,ElemTypee);

//判断顺序表是否为空

boolisEmpty();

//查找第1个与e满足compare关系的元素的序号

intlocateElem(ElemTypee,Status(*compare)(ElemType,ElemType));

//返回某个元素的后继

StatusnextElem(ElemTypee,ElemType&

next_e);

//重载复制运算符

SqList<

operator=(SqList<

rightL);

//返回某个元素的前驱

StatuspriorElem(ElemTypee,ElemType&

prior_e);

//在顺序表中顺序查找某个元素、

intsequentialSearch(ElemTypee);

};

//顺序表的方法

//有序顺序表的折半查找

intSqList<

:

bin_Search(ElemTypekey)

intlow,mid,high;

//查找区域的起始、中间以及最后一个元素的下标

low=0,high=n-1;

while(low<

=high)

{

mid=(low+high)/2;

if(elem[mid]==key)

returnmid+1;

elseif(elem[mid]<

key)

low=mid+1;

elsehigh=mid-1;

}

return0;

}

//把顺序表置空

voidSqList<

clear()

n=0;

//删除第i个元素

StatusSqList<

deleteElem(inti,ElemType&

e)

if(i<

1||i>

n)returnERROR;

e=elem[i-1];

for(intj=i+1;

j<

=n;

++j)

elem[j-2]=elem[j-1];

--n;

returnOK;

//取第i个元素

getElem(inti,ElemType&

//求顺序表中元素的个数

getLength()

returnn;

//取顺序表存储空间的大小

getListSize()

returnlistSize;

//在第i元素之前插入一个元素

insert(inti,ElemTypee)

ElemType*newbase;

n+1)

returnERROR;

if(n>

=listSize)

newbase=newElemType[listSize+LISTINCERMENT];

assert(newbase!

=0);

for(intj=1;

n;

newbase[j-1]=elem[j-1];

delete[]elem;

elem=newbase;

listSize+=LISTINCERMENT;

for(intj=n;

j>

=i;

--j)

elem[j]=elem[j-1];

elem[i-1]=e;

++n;

//判断顺序表是否为空

boolSqList<

isEmpty()

returnn?

false:

true;

//查找第1个与某元素e满足compare()关系元素

locateElem(ElemTypee,Status(*compare)(ElemType,ElemType))

inti;

for(i=1;

i<

=n&

!

(*compare)(elem[i-1],e);

++i);

=n)

returni;

else

return0;

//返回某个元素的后继

nextElem(ElemTypee,ElemType&

next_e)

inti=locateElem(e,equal);

1||i==n)

getElem(i+1,next_e);

//重载运算符的定义

SqList<

operator=(SqList<

rightL)

if(this!

=&

if(listSize<

rightL.listSize)

{

delete[]elem;

elem=newElemType[rightL.listSize];

assert(elem!

listSize=rightL.listSize;

}

n=rightL.n;

for(inti=1;

++i)

elem[i-1]=rightL.elem[i-1];

return*this;

//返回某个元素的前驱

priorElem(ElemTypee,ElemType&

prior_e)

=1)

getElem(i-1,prior_e);

//在顺序表中顺序查找某个元素

sequentialSearch(ElemTypekey)

for(inti=1;

n&

key!

=elem[i-1];

i++);

else

//构造函数的实现

SqList()

elem=newElemType[LIST_MAX_SIZE];

assert(elem!

listSize=LIST_MAX_SIZE;

//析构函数的实现

~SqList()

delete[]elem;

//拷贝构造函数的实现

SqList(constSqList<

otherL)

elem=newElemType[otherL.listSize];

listSize=otherL.listSize;

n=otherL.n;

i++)

elem[i-1]=otherL.elem[i-1];

派生类MySqList.h

#ifndefSQLIST_H

#defineSQLIST_H

SqList.h"

#include<

iosfwd>

classMySqList:

publicSqList<

//读输入数据

voidread(istream&

in);

//显示数据

voiddisplay(ostream&

out)const;

//生成随机顺序表

StatusrandSql(intn,MySqList<

otherS);

//派生类的实现

//输入

voidMySqList<

read(istream&

in)

cout<

<

"

请输入要建立的顺序表的元素个数:

;

cin>

>

for(inti=0;

//cout<

n<

endl;

cout<

请输入顺序表的第"

i+1<

个元素:

in>

elem[i];

//n++;

istream&

operator>

(istream&

in,MySqList<

iL)

{

iL.read(in);

returnin;

//输出

display(ostream&

out)const

out<

["

]"

\t"

out<

elem[i]<

endl<

ostream&

/*MySqList<

*/operator<

(ostream&

out,constMySqList<

oL)

oL.display(out);

returnout;

//生成随机数顺序表

StatusMySqList<

randSql(intn,MySqList<

otherS)

errno_terr;

unsignedintnumber;

intmax=100;

if(n<

1||n>

otherS.listSize)returnERROR;

for(inti=0;

err=rand_s(&

number);

if(err!

=0)

{

printf_s("

Therand_sfunctionfailed!

\n"

);

}

otherS.elem[i]=(unsignedint)((double)number/((double)UINT_MAX+1)*100.0)+1;

otherS.n=n;

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

当前位置:首页 > 医药卫生 > 基础医学

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

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