ImageVerifierCode 换一换
格式:DOCX , 页数:40 ,大小:30.11KB ,
资源ID:25444353      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/25444353.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(数据结构实验报告.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

数据结构实验报告.docx

1、数据结构实验报告 数据结构实验报告 实验一 栈和队列一、 实验目的通过几段代码的编写,熟悉栈和队列二、实验内容1.题目一读懂英文实验题目文档中的Task1中的程序(使用栈进行序列的顺序反转),并编译运行,通过此了解如果要实现一个栈类,里面需要的基本的成员函数。这个程序在书上也有。2.题目二、题目三题目如下:自己编程程序实现一个简单的栈,并用于替换题目1中对标准模板库中的栈的使用,同时对自己实现的栈的功能进行扩充,添加实现如下几个函数(a) clear (b)full (c) size。使用新添加的栈函数,显示在进行数字序列反转时输入的十进制数的个数。3.题目四这个题目的主要目的是熟悉队列这个数

2、据结构,而为了说明问题又用了一个模拟飞机场的程序,因此这个实验项目在程序的找错误调试编译,读源代码上对大家也是一个锻炼。仔细阅读教科书中关于模拟飞机场这一部分,阅读源代码。实验题目中的源代码并不完整并且有些语法等等的错误,其缺少生成随机数这一个类,附录一会把这个类给大家,有兴趣的话,可以看教科书中的附录,有些介绍。另外,也可以使用随机数生成函数,使用示例见附录二。三、实验代码/task 1#include #include using namespace std;int main( )/* Pre: The user supplies an integer n and n decimal nu

3、mbers.Post: The numbers are printed in reverse order.Uses: The STL class stack and its methods */ int n; double item; stack numbers; / declares and initializes a stack of numbers cout Type in an integer n followed by n decimal numbers. endl The numbers will be printed in reverse order. n; for (int i

4、 = 0; i item; numbers.push(item); cout endl endl; while (!numbers.empty( ) cout numbers.top( ) ; numbers.pop( ); cout endl;/task 2#include #include using namespace std;const int maxstack = 10; / small value for testingtypedef int Error_code ;#define underflow 2#define overflow 3#define success 0type

5、def int Stack_entry;class Stack public: Stack(); bool empty() const; Error_code pop(); Error_code top(Stack_entry &item) const; Error_code push(const Stack_entry &item); private: int count; Stack_entry entrymaxstack;Error_code Stack:push(const Stack_entry &item)/*Pre: None.Post: If the Stack is not

6、full, item is added to the topof the Stack. If the Stack is full,an Error_code of overflow is returned and the Stack is left unchanged.*/ Error_code outcome = success; if (count = maxstack) outcome = overflow; else entrycount+ = item; return outcome;Error_code Stack:pop() /*Pre: None.Post: If the St

7、ack is not empty, the top ofthe Stack is removed. If the Stackis empty, an Error_code of underflow is returned.*/ Error_code outcome = success; if (count = 0) outcome = underflow; else -count; return outcome;Error_code Stack:top(Stack_entry &item) const/*Pre: None.Post: If the Stack is not empty, th

8、e top ofthe Stack is returned in item. If the Stackis empty an Error_code of underflow is returned.*/ Error_code outcome = success; if (count = 0) outcome = underflow; else item = entrycount - 1; return outcome;bool Stack:empty() const/*Pre: None.Post: If the Stack is empty, true is returned.Otherwi

9、se false is returned.*/ bool outcome = true; if (count 0) outcome = false; return outcome;Stack:Stack()/*Pre: None.Post: The stack is initialized to be empty.*/ count = 0;int main() return 0;/题目四的代码略实验二 多项式加减法一、实验目的通过实现多项式的加法,对链表有更深入的了解二、实验内容问题描述:设计一个一元稀疏多项式简单的加减法计算器实现要求:一元稀疏多项式简单计算器的基本功能是:(1)输入并建立多

10、项式:;(2)输出多项式(3)多项式A和B相加,建立多项式CAB,并输出相加的结果多项式C(4)选作:多项式A和B相减,建立多项式CAB,并输出相加的结果多项式D/实验代码/polynomial.h文件 #ifndef _POLYNOMIAL_#define _POLYNOMIAL_struct Node;/单链表节点类型typedef struct Node *PNode;/节点指针类型struct Node/单链表节点结构 int coef;/系数 int exp;/指数 PNode next;/指针域;typedef struct Node * Linklist;/单链表类型/单链表操作

11、函数声明/创建头结点Linklist CreateNullList_link(void);Linklist input(Linklist llist);int print(Linklist llist);/void Display(Linklist llist);#endif/polynomial.c文件#include #include #include polynomial.h/创建头结点Linklist CreateNullList_link(void) Linklist head; head=(Linklist)malloc(sizeof(struct Node);/申请表头节点内存

12、if(head!=NULL) head-next=NULL; else printf(创建失败!); return head;Linklist input(Linklist llist) PNode p,c; int coef;/系数 int exp;/指数 c=llist; printf(请输入链表中的数据,输入0结束:n); scanf(%d ,&coef); scanf(%d,&exp); while(coef!=0) p=(PNode)malloc(sizeof(struct Node); c-next=p; p-coef=coef; p-exp= exp; p-next=NULL;

13、c=c-next; scanf(%d,&coef); scanf(%d,&exp); return llist;int print(Linklist llist) PNode p; printf(新创建的链表如下:n); p=llist-next; if (p=NULL) printf(空链表啦!); return 0; while(p!=NULL) /p=p-link; printf(%dn,p-coef); printf(%dn,p-exp); p=p-next; return 1;void Display(Linklist llist) PNode p; p=llist-next; wh

14、ile(p!=NULL) printf(%d*(x)%d+,p-coef,p-exp); p=p-next; /MainTest.c 文件#include#include#include polynomial.hint main() Linklist head1,head2; printf(创建第一个多项式n); head1 =CreateNullList_link(); input(head1); print( head1); Display(head1); printf(创建第二个多项式n); head2 =CreateNullList_link(); input(head2); prin

15、t( head2); Display(head2); return 0;实验三 走迷宫一、实验目的通过设计走迷宫的算法,了解递归程序设计,使用非递归和递归方法分别完成走迷宫的算法二、实验内容本次实验的题目详细见实验题目文档,要求为走迷宫设计两种算法,一种使用了递归,一种没有使用递归的算法。实验题目中详细讲述的算法是非递归算法,用栈为辅助的数据结构。这里可以使用标准模板库中的stack三 实验代码#include #include using namespace std;char maze_layout1212= 1,1,1,1,1,1,1,1,1,1,1,1, 1,0,1,0,1,1,1,0,

16、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 ;typedef struct Point int x; int y;P

17、oint, *PPoint;Point directPoint = 1,0,0,1,-1,0,0,-1,1,-1,1,1,-1,-1,-1,1; /下,右, 上,左, 左下, 右下, 左上,右上void displayMaze() coutt; for(int row=1;row11;row+) for(int col=1;col11;col+) if(maze_layoutrowcol= +) /把+ 消去 maze_layoutrowcol = 0; coutmaze_layoutrowcol ; if(row!=10) coutendlt; cout EXITendlendl;bool

18、solve(int i, int j) /递归 bool finish = false; maze_layoutij = X; if (i = 10 & j = 10) return true; / because were done int row, col; for(int k=0; k8; k+) /八个方向都走一遍 row = i + directPointk.x; col = j + directPointk.y; if(!finish & maze_layoutrowcol= 0) finish = solve(row, col); if (!finish) /无法走通,那么修改原

19、来设置的X符号,但是不设置为0,因为这个点现在已经走过 maze_layoutij = +; return finish;bool IsCanMove(Point point1, Point &point2) /point1是否可以移动, 能移动就把移动后的位置赋给point2 int row, col; maze_layoutpoint1.xpoint1.y = + ; if(point1.x = 10 & point1.y = 10) /走到终点 不可以再走了 return false; for(int i=0; i8; i+) row = point1.x + directPointi.

20、x; col = point1.y + directPointi.y; if(maze_layoutrowcol= 0) point2.x = row; point2.y = col; return true; return false;bool Maze() /非递归 stack s; Point point1, point2; point1.x = 1; point1.y = 1; while(IsCanMove(point1, point2) ) while(IsCanMove(point1, point2) s.push(point1); point1 = point2; if(poi

21、nt1.x = 10 & point1.y = 10) s.push(point1); break; while(!s.empty() & !IsCanMove(point1,point2) point1 = s.top(); s.pop(); if(s.empty() return false; while(!s.empty() maze_layouts.top().xs.top().y = X; s.pop(); return true;int main() solve(1,1); / Maze(); displayMaze(); return 0;实验四 按层次构造二叉树及二叉树遍历一、

22、实验目的设计数据结构和算法,实现树的构建掌握树的前根序、中根序和后根序遍历算法了解c+中类模板的定义和使用二、实验内容这个实验中要求用类模板来实现树的类。需要对类模板有些了解。注意,在编写代码的时候,要将所有模板信息放在一个头文件中,并在要使用这些模板的文件中包含该头文件(如果分别用.h和.cpp实现,那就必须在使用该类的地方同时包含该.h和.cpp文件)。因为模板不是函数,模板必须与特定的模板实例化请求一起使用。若将模板成员函数放置在一个独立的实现文件中将无法运行。1.二叉树的构建和遍历详细见实验题目文档。教科书上有一个用类模板实现的二叉树的类的定义,需要补充实现其中的二叉树的构建(要求使用

23、实验题目文档中的方法,但是实验题目文档中没有描述具体的算法,这里等下会简单讲述具体的算法)。在正确构建二叉树,实现这个类的基础上,对构建好的二叉树进行遍历(前根序、中根序和后根序,这三个遍历算法在教科书上都有详细的代码)。2.二叉树结点的插入详细见实验题目文档。简单的说就是根据二叉树的高度,进行结点的插入,把结点插入到高度最小的二叉树分叉上。这也是教科书中的一个习题。其中需要计算二叉树左右分支的高度。(1)计算树的高度的关键代码如下,采用的是递归调用:计算树的高度函数 如果寻找到的结点为空(空子树),则返回树的高度为0 否则进行下面的步骤1.计算左子树的高度2.计算右子树的高度3.返回左右子树

24、中高度最大的+1。(2)插入结点的算法如下,采用的是递归调用插入结点函数 如果找到的结点为空(空子树),则插入结点 否则进行下面的步骤如果右子树的高度小于左子树的高度,则往右子树插入结点如果右子树的高度大于等于左子树的高度,则往左子树插入结点注意事项:1、辅助构造树的队列中放的要是指向树结点的指针2、对于c不熟悉的同学,如果使用模板类,会有很大难度,建议大家如果对于c不熟悉,则不用模板类,用普通的类实现也是允许的三 实验代码#include #include using namespace std ;struct Binary_node/结构体 char data; Binary_node *

25、left; Binary_node *right;class Bintree private: Binary_node *root;public: Bintree() root=NULL; void buildtree(); /用户输入数据“#”结束 Bintree leftchild(Bintree); /查找并返回左子树 Bintree rightchild(Bintree);/查找并返回右子树 void preorder(Bintree); /先根遍历并输出 void inorder(Bintree); /中根 void postorder(Bintree); /后根 ;Bintree

26、Bintree:leftchild(Bintree t) Bintree tmp; /返回左子树 tmp.root=t.root-left; return tmp; Bintree Bintree:rightchild(Bintree t) Bintree tmp; tmp.root=t.root-right; /返回右子树 return tmp;void Bintree:preorder(Bintree t) /先根 if(t.root=NULL)return; coutdata; preorder(leftchild(t); preorder(rightchild(t);void Bintree:inorder(Bintree t) /中根 if(t.root=NULL)return; inorder(leftchild(t); coutdata; inorder(rightchild(t);void Bintree:postorder(B

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

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