数据结构实验报告.docx

上传人:b****7 文档编号:25444353 上传时间:2023-06-08 格式:DOCX 页数:40 大小:30.11KB
下载 相关 举报
数据结构实验报告.docx_第1页
第1页 / 共40页
数据结构实验报告.docx_第2页
第2页 / 共40页
数据结构实验报告.docx_第3页
第3页 / 共40页
数据结构实验报告.docx_第4页
第4页 / 共40页
数据结构实验报告.docx_第5页
第5页 / 共40页
点击查看更多>>
下载资源
资源描述

数据结构实验报告.docx

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

数据结构实验报告.docx

数据结构实验报告

数据结构实验报告

实验一栈和队列

一、实验目的

通过几段代码的编写,熟悉栈和队列

二、实验内容

1.题目一

读懂英文实验题目文档中的Task1中的程序(使用栈进行序列的顺序反转),并编译运行,通过此了解如果要实现一个栈类,里面需要的基本的成员函数。

这个程序在书上也有。

2.题目二、题目三

题目如下:

自己编程程序实现一个简单的栈,并用于替换题目1中对标准模板库中的栈的使用,同时对自己实现的栈的功能进行扩充,添加实现如下几个函数(a)clear(b)full(c)size。

使用新添加的栈函数,显示在进行数字序列反转时输入的十进制数的个数。

3.题目四

这个题目的主要目的是熟悉队列这个数据结构,而为了说明问题又用了一个模拟飞机场的程序,因此这个实验项目在程序的找错误调试编译,读源代码上对大家也是一个锻炼。

仔细阅读教科书中关于模拟飞机场这一部分,阅读源代码。

实验题目中的源代码并不完整并且有些语法等等的错误,其缺少生成随机数这一个类,附录一会把这个类给大家,有兴趣的话,可以看教科书中的附录,有些介绍。

另外,也可以使用随机数生成函数,使用示例见附录二。

三、实验代码

//task1

#include

#include

usingnamespacestd;

intmain()

/*Pre:

Theusersuppliesanintegernandndecimalnumbers.

Post:

Thenumbersareprintedinreverseorder.

Uses:

TheSTLclassstackanditsmethods*/

{

intn;

doubleitem;

stacknumbers;//declaresandinitializesastackofnumbers

cout<<"Typeinanintegernfollowedbyndecimalnumbers."

<

<<"Thenumberswillbeprintedinreverseorder."

<

cin>>n;

for(inti=0;i

cin>>item;

numbers.push(item);

}

cout<

while(!

numbers.empty()){

cout<

numbers.pop();

}

cout<

}

//task2

#include

#include

usingnamespacestd;

constintmaxstack=10;//smallvaluefortesting

typedefintError_code;

#defineunderflow2

#defineoverflow3

#definesuccess0

typedefintStack_entry;

classStack{

public:

Stack();

boolempty()const;

Error_codepop();

Error_codetop(Stack_entry&item)const;

Error_codepush(constStack_entry&item);

private:

intcount;

Stack_entryentry[maxstack];

};

Error_codeStack:

:

push(constStack_entry&item)

/*

Pre:

None.

Post:

IftheStackisnotfull,itemisaddedtothetop

oftheStack.IftheStackisfull,

anError_codeofoverflowisreturnedandtheStackisleftunchanged.

*/

{

Error_codeoutcome=success;

if(count>=maxstack)

outcome=overflow;

else

entry[count++]=item;

returnoutcome;

}

Error_codeStack:

:

pop()

/*

Pre:

None.

Post:

IftheStackisnotempty,thetopof

theStackisremoved.IftheStack

isempty,anError_codeofunderflowisreturned.

*/

{

Error_codeoutcome=success;

if(count==0)

outcome=underflow;

else--count;

returnoutcome;

}

Error_codeStack:

:

top(Stack_entry&item)const

/*

Pre:

None.

Post:

IftheStackisnotempty,thetopof

theStackisreturnedinitem.IftheStack

isemptyanError_codeofunderflowisreturned.

*/

{

Error_codeoutcome=success;

if(count==0)

outcome=underflow;

else

item=entry[count-1];

returnoutcome;

}

boolStack:

:

empty()const

/*

Pre:

None.

Post:

IftheStackisempty,trueisreturned.

Otherwisefalseisreturned.

*/

{

booloutcome=true;

if(count>0)outcome=false;

returnoutcome;

}

Stack:

:

Stack()

/*

Pre:

None.

Post:

Thestackisinitializedtobeempty.

*/

{

count=0;

}

intmain()

{

return0;

}

//题目四的代码略

实验二多项式加减法

一、实验目的

通过实现多项式的加法,对链表有更深入的了解

二、实验内容

问题描述:

设计一个一元稀疏多项式简单的加减法计算器

实现要求:

一元稀疏多项式简单计算器的基本功能是:

(1)输入并建立多项式:

(2)输出多项式

(3)多项式A和B相加,建立多项式C=A+B,并输出相加的结果多项式C

(4)选作:

多项式A和B相减,建立多项式C=A-B,并输出相加的结果多项式D

//实验代码

//polynomial.h文件

#ifndef__POLYNOMIAL__

#define__POLYNOMIAL__

structNode;//单链表节点类型

typedefstructNode*PNode;//节点指针类型

structNode//单链表节点结构

{

intcoef;//系数

intexp;//指数

PNodenext;//指针域

};

typedefstructNode*Linklist;//单链表类型

//单链表操作函数声明

//创建头结点

LinklistCreateNullList_link(void);

Linklistinput(Linklistllist);

intprint(Linklistllist);

//voidDisplay(Linklistllist);

#endif

//polynomial.c文件

#include

#include

#include"polynomial.h"

//创建头结点

LinklistCreateNullList_link(void)

{

Linklisthead;

head=(Linklist)malloc(sizeof(structNode));//申请表头节点内存

if(head!

=NULL)

{

head->next=NULL;

}

else

{

printf("创建失败!

");

}

returnhead;

}

Linklistinput(Linklistllist)

{

PNodep,c;

intcoef;//系数

intexp;//指数

c=llist;

printf("请输入链表中的数据,输入0结束:

\n");

scanf("%d",&coef);

scanf("%d",&exp);

while(coef!

=0)

{

p=(PNode)malloc(sizeof(structNode));

c->next=p;

p->coef=coef;

p->exp=exp;

p->next=NULL;

c=c->next;

scanf("%d",&coef);

scanf("%d",&exp);

}

returnllist;

}

intprint(Linklistllist)

{

PNodep;

printf("新创建的链表如下:

\n");

p=llist->next;

if(p==NULL)

{

printf("空链表啦!

");

return0;

}

while(p!

=NULL)

{

//p=p->link;

printf("%d\n",p->coef);

printf("%d\n",p->exp);

p=p->next;

}

return1;

}

voidDisplay(Linklistllist)

{

PNodep;

p=llist->next;

while(p!

=NULL)

{

printf("%d*(x)^%d+",p->coef,p->exp);

p=p->next;

}

}

//MainTest.c文件

#include

#include

#include"polynomial.h"

intmain()

{

Linklisthead1,head2;

printf("创建第一个多项式\n");

head1=CreateNullList_link();

input(head1);

print(head1);

Display(head1);

printf("创建第二个多项式\n");

head2=CreateNullList_link();

input(head2);

print(head2);

Display(head2);

return0;

}

实验三走迷宫

一、实验目的

通过设计走迷宫的算法,了解递归程序设计,使用非递归和递归方法分别完成走迷宫的算法

二、实验内容

本次实验的题目详细见实验题目文档,要求为走迷宫设计两种算法,一种使用了递归,一种没有使用递归的算法。

实验题目中详细讲述的算法是非递归算法,用栈为辅助的数据结构。

这里可以使用标准模板库中的stack

三实验代码

#include

#include

usingnamespacestd;

charmaze_layout[12][12]={

{'1','1','1','1','1','1','1','1','1','1','1','1'},

{'1','0','1','0','1','1','1','0','0','0','0','1'},

{'1','0','1','0','0','1','0','0','0','0','0','1'},

{'1','0','0','0','0','1','0','1','1','0','0','1'},

{'1','1','1','1','0','1','0','1','0','0','0','1'},

{'1','0','0','1','0','0','0','1','0','1','1','1'},

{'1','0','0','1','0','0','0','1','0','1','1','1'},

{'1','0','1','1','0','1','0','1','0','0','0','1'},

{'1','0','0','0','0','1','0','1','1','0','0','1'},

{'1','0','0','0','0','1','0','1','1','0','0','1'},

{'1','0','0','0','0','1','0','1','1','0','0','1'},//->Exit

{'1','1','1','1','1','1','1','1','1','1','1','1'}

};

typedefstructPoint

{

intx;

inty;

}Point,*PPoint;

PointdirectPoint[]={{1,0},{0,1},{-1,0},{0,-1},{1,-1},{1,1},{-1,-1},{-1,1}};

//下,右,上,左,左下,右下,左上,右上

voiddisplayMaze()

{

cout<<"Enter->\t";

for(introw=1;row<11;row++)

{

for(intcol=1;col<11;col++)

{

if(maze_layout[row][col]=='+')//把'+'消去

{

maze_layout[row][col]='0';

}

cout<

}

if(row!

=10)

{

cout<

}

}

cout<<"-->EXIT"<

}

boolsolve(inti,intj)//递归

{

boolfinish=false;

maze_layout[i][j]='X';

if(i==10&&j==10)

returntrue;//becausewe'redone

introw,col;

for(intk=0;k<8;k++)//八个方向都走一遍

{

row=i+directPoint[k].x;

col=j+directPoint[k].y;

if(!

finish&&maze_layout[row][col]=='0')

{

finish=solve(row,col);

}

}

if(!

finish)//无法走通,那么修改原来设置的X符号,但是不设置为0,因为这个点现在已经走过

{

maze_layout[i][j]='+';

}

returnfinish;

}

boolIsCanMove(Pointpoint1,Point&point2)

//point1是否可以移动,能移动就把移动后的位置赋给point2

{

introw,col;

maze_layout[point1.x][point1.y]='+';

if(point1.x==10&&point1.y==10)//走到终点不可以再走了

{

returnfalse;

}

for(inti=0;i<8;i++)

{

row=point1.x+directPoint[i].x;

col=point1.y+directPoint[i].y;

if(maze_layout[row][col]=='0')

{

point2.x=row;

point2.y=col;

returntrue;

}

}

returnfalse;

}

boolMaze()//非递归

{

stacks;

Pointpoint1,point2;

point1.x=1;

point1.y=1;

while(IsCanMove(point1,point2))

{

while(IsCanMove(point1,point2))

{

s.push(point1);

point1=point2;

}

if(point1.x==10&&point1.y==10)

{

s.push(point1);

break;

}

while(!

s.empty()&&!

IsCanMove(point1,point2))

{

point1=s.top();

s.pop();

}

}

if(s.empty())

{

returnfalse;

}

while(!

s.empty())

{

maze_layout[s.top().x][s.top().y]='X';

s.pop();

}

returntrue;

}

intmain()

{

solve(1,1);

//Maze();

displayMaze();

return0;

}

实验四按层次构造二叉树及二叉树遍历

一、实验目的

设计数据结构和算法,实现树的构建

掌握树的前根序、中根序和后根序遍历算法

了解c++中类模板的定义和使用

二、实验内容

这个实验中要求用类模板来实现树的类。

需要对类模板有些了解。

注意,在编写代码的时候,要将所有模板信息放在一个头文件中,并在要使用这些模板的文件中包含该头文件(如果分别用.h和.cpp实现,那就必须在使用该类的地方同时包含该.h和.cpp文件)。

因为模板不是函数,模板必须与特定的模板实例化请求一起使用。

若将模板成员函数放置在一个独立的实现文件中将无法运行。

1.二叉树的构建和遍历

详细见实验题目文档。

教科书上有一个用类模板实现的二叉树的类的定义,需要补充实现其中的二叉树的构建(要求使用实验题目文档中的方法,但是实验题目文档中没有描述具体的算法,这里等下会简单讲述具体的算法)。

在正确构建二叉树,实现这个类的基础上,对构建好的二叉树进行遍历(前根序、中根序和后根序,这三个遍历算法在教科书上都有详细的代码)。

2.二叉树结点的插入

详细见实验题目文档。

简单的说就是根据二叉树的高度,进行结点的插入,把结点插入到高度最小的二叉树分叉上。

这也是教科书中的一个习题。

其中需要计算二叉树左右分支的高度。

(1)计算树的高度的关键代码如下,采用的是递归调用:

计算树的高度函数

{

如果寻找到的结点为空(空子树),则返回树的高度为0

否则进行下面的步骤

1.计算左子树的高度

2.计算右子树的高度

3.返回左右子树中高度最大的+1。

}

(2)插入结点的算法如下,采用的是递归调用

插入结点函数

{

如果找到的结点为空(空子树),则插入结点

否则进行下面的步骤

{

如果右子树的高度小于左子树的高度,则往右子树插入结点

如果右子树的高度大于等于左子树的高度,则往左子树插入结点

}

}

注意事项:

1、辅助构造树的队列中放的要是指向树结点的指针

2、对于c++不熟悉的同学,如果使用模板类,会有很大难度,建议大家如果对于c++不熟悉,则不用模板类,用普通的类实现也是允许的

三实验代码

#include

#include

usingnamespacestd;

structBinary_node//结构体

{

chardata;

Binary_node*left;

Binary_node*right;

};

classBintree

{

private:

Binary_node*root;

public:

Bintree()

{

root=NULL;

}

voidbuildtree();//用户输入数据“#”结束

Bintreeleftchild(Bintree);//查找并返回左子树

Bintreerightchild(Bintree);//查找并返回右子树

voidpreorder(Bintree);//先根遍历并输出

voidinorder(Bintree);//中根

voidpostorder(Bintree);//后根

};

BintreeBintree:

:

leftchild(Bintreet)

{

Bintreetmp;//返回左子树

tmp.root=t.root->left;

returntmp;

}

BintreeBintree:

:

rightchild(Bintreet)

{

Bintreetmp;

tmp.root=t.root->right;////返回右子树

returntmp;

}

voidBintree:

:

preorder(Bintreet)//先根

{

if(t.root==NULL)return;

cout<data;

preorder(leftchild(t));

preorder(rightchild(t));

}

voidBintree:

:

inorder(Bintreet)//中根

{

if(t.root==NULL)return;

inorder(leftchild(t));

cout<data;

inorder(rightchild(t));

}

voidBintree:

:

postorder(B

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

当前位置:首页 > 初中教育 > 初中作文

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

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