逗比丢数据结构.docx

上传人:b****6 文档编号:7588167 上传时间:2023-05-11 格式:DOCX 页数:30 大小:105.71KB
下载 相关 举报
逗比丢数据结构.docx_第1页
第1页 / 共30页
逗比丢数据结构.docx_第2页
第2页 / 共30页
逗比丢数据结构.docx_第3页
第3页 / 共30页
逗比丢数据结构.docx_第4页
第4页 / 共30页
逗比丢数据结构.docx_第5页
第5页 / 共30页
逗比丢数据结构.docx_第6页
第6页 / 共30页
逗比丢数据结构.docx_第7页
第7页 / 共30页
逗比丢数据结构.docx_第8页
第8页 / 共30页
逗比丢数据结构.docx_第9页
第9页 / 共30页
逗比丢数据结构.docx_第10页
第10页 / 共30页
逗比丢数据结构.docx_第11页
第11页 / 共30页
逗比丢数据结构.docx_第12页
第12页 / 共30页
逗比丢数据结构.docx_第13页
第13页 / 共30页
逗比丢数据结构.docx_第14页
第14页 / 共30页
逗比丢数据结构.docx_第15页
第15页 / 共30页
逗比丢数据结构.docx_第16页
第16页 / 共30页
逗比丢数据结构.docx_第17页
第17页 / 共30页
逗比丢数据结构.docx_第18页
第18页 / 共30页
逗比丢数据结构.docx_第19页
第19页 / 共30页
逗比丢数据结构.docx_第20页
第20页 / 共30页
亲,该文档总共30页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

逗比丢数据结构.docx

《逗比丢数据结构.docx》由会员分享,可在线阅读,更多相关《逗比丢数据结构.docx(30页珍藏版)》请在冰点文库上搜索。

逗比丢数据结构.docx

逗比丢数据结构

 

《数据结构与测绘软件开发》实验报告

 

 

姓名:

豆京京

班级:

测绘工程13-03班

学号:

07132860

指导教师:

王永波

 

中国矿业大学环境与测绘学院

2015-5-20

实验一:

线性表类的设计与实现

一、实验目的

通过上机实践,强化课堂有关线性表的教学内容,通过动手编程实现线性表类的设计,并通过实例验证类的实例化效果及其相关应用,达到对课堂所学内容的深刻掌握。

二、实验内容:

线性表类的设计与实现,包括线性表的实际应用。

(1)顺序存储的删除

(2)顺序存储的插入

(3)顺序存储的查找

(4)链式存储的删除

(5)链式存储的插入

(6)链式存储的查找

三、实现代码

实现代码:

#include"stdafx.h"

#include

usingnamespacestd;

//类声明

//顺序存储线性表

classCSeqList

{public:

//构造、析构函数

CSeqList();

CSeqList(intsize);

~CSeqList();

public:

//复制构造函数、赋值运算

CSeqList(constCSeqList&sl);

CSeqList&operator=(constCSeqList&sl);

public:

//友元函数

friendostream&operator<<(ostream&os,CSeqList&sl);

public:

//成员函数

intLength()const;

intRemove(intz);//删除

intFind(intx);//查找

intLocate(inti)const;//定位

intInsert(int&x,inti);//插入

intRemove(int&x);//删除

intNext(int&x);//后继

intPrior(int&x);//前驱

intIsEmpty();

intIsFull();

intGet(inti);//提取

voidinnt(intcursize);

public:

//成员变量

int*_data;

int_curSize;

int_maxSize;

};

//类实现

CSeqList:

:

CSeqList()

{

}

CSeqList:

:

CSeqList(intsize)

:

_maxSize(size)

_curSize(0)

{_data=newint[size];

innt(10);

}

CSeqList:

:

~CSeqList()

{if(_maxSize>0)

delete[]_data;

}

voidCSeqList:

:

innt(intcursize)

{for(inti=0;i

_data[i]=i*100;

_curSize=cursize;

}

intCSeqList:

:

Length()const

{for(inti=0;i<_curSize;i++)

cout<<_data[i]<<"";

cout<

return_curSize;

}

intCSeqList:

:

Insert(int&x,inti)//插入

{//元素移动

for(intj=_curSize-1;j>=i;j--)

{_data[j+1]=_data[j];

}

_data[i]=x;

_curSize++;

for(intk=0;k<_curSize;k++)

cout<<_data[k]<<"";

cout<

return0;

}

intCSeqList:

:

Remove(intz)//删除

{for(intj=z;j<=_curSize;j++)

_data[j]=_data[j+1];

_curSize--;

for(intg=0;g<_curSize;g++)

cout<<_data[g]<<"";

cout<

return0;

}

intCSeqList:

:

Find(intx)//查找

{for(intj=0;j<_maxSize;j++)

if(_data[j]==x)

{cout<

return0;

}

cout<<;

return-1;

}

ostream&operator<<(ostream&os,CSeqList&sl)

{

for(inti=0;i

{

cout<

cout<

}

returnos;

}

//类声明

template

classSeqList{

//顺序表存储数组

Type*data;

//最大允许长度

intMaxSize;

//当前最后元素下标

intlast;

public:

SeqList(intMaxSize=defaultSize);

~SeqList(){delete[]data;}

public:

//友元函数

intfriendoperator<<(ostream&os,SeqList&sl);

public:

intLength()const

{returnlast+1;

}

//查找

intFind(Type&x)const;

intLocate(inti)const;//定位

intInsert(inti,Type&x);//插入

intRemove(inti);//删除

intNext(Type&x);//后继

intPrior(Type&x);//前驱

intIsEmpty(){returnlast==-1;}

intIsFull()

{returnlast==MaxSize-1;

}

TypeGet(inti){//提取

returni<0||i>last?

NULL:

data[i];

}

};

//类实现

//类声明

//链式存储线性表

structSLNode{

intdata;//数据域

structSLNode*prior;//指针域

structSLNode*next;//指针域

};

classCLinkedList{

public:

//构造、析构函数

CLinkedList(void);

~CLinkedList(void);

public:

//复制构造函数、赋值运算

CLinkedList(constCLinkedList&sl);

CLinkedList&operator=(constCLinkedList&sl);

public:

//友元函数

intfriendoperator<<(ostream&os,CLinkedList&ll);

public:

//成员函数

intlength()const;

intsearch(int&x)const;//查找

intinsert(int&x,inti);//插入

intremove(int&x);//删除

voidsort();//排序

intisEmpty();

voidsetNull();//置空

intget(inti);//提取

voidoutput();

private:

//成员变量

SLNode*head;

int_size;

};

CLinkedList:

:

CLinkedList()

:

head(NULL)

_size(0)

{

}

CLinkedList:

:

~CLinkedList()

{

}

intCLinkedList:

:

length()const

{return_size;

}

intCLinkedList:

:

search(int&x)const

{if(NULL==head)

return-1;

intresult=1;

SLNode*cur;

cur=head;

while(cur!

=NULL)

{if(cur->data==x)

returnresult;

else

{cur=cur->next;

result++;

}

}

return-1;

}

intCLinkedList:

:

insert(int&x,inti)

{if(i<=0)//越界

return-1;

if(_size==0)

{//插入结点操作

SLNode*temLN=newSLNode();

temLN->data=x;

temLN->next=NULL;

head=temLN;

_size++;

return1;

}

elseif(i>_size)//在链表尾部插入

{SLNode*cur=head;

while(cur->next!

=NULL)

cur=cur->next;

//插入结点操作

SLNode*temLN=newSLNode();

temLN->data=x;

temLN->next=NULL;

cur->next=temLN;

_size++;

return1;

}

SLNode*cur=head;

for(intk=1;k

{cur=cur->next;

}

//插入结点操作

SLNode*temLN=newSLNode();

temLN->data=x;

temLN->next=cur->next;

cur->next=temLN;

_size++;

return1;

}

intCLinkedList:

:

remove(int&x)

{SLNode*cur=head;

if(cur->data==x)//删除表头第一个元素

{head=cur->next;

return1;

}

//删除除表头之外的其他位置元素

while(cur->next!

=NULL)

{if(cur->next->data==x)//执行删除操作

{cur->next=cur->next->next;

return1;

}

cur=cur->next;

}

return1;

}

intCLinkedList:

:

isEmpty()

{if(_size==0||head==NULL)

return1;

return0;

}

voidCLinkedList:

:

setNull()

{_size=0;

head=NULL;

}

intCLinkedList:

:

get(inti)

{if(i<=0||i>_size)

{cout<<"下表越界!

"<

return0;

}

SLNode*cur=head;

for(intk=1;k

{cur=cur->next;

}

returncur->data;

}

voidCLinkedList:

:

output()

{SLNode*cur;

cur=head;

while(cur!

=NULL)

{cout<data<<"";

cur=cur->next;

}

cout<

}

intmain(intargc,char*argv[])

{

/*CSeqListsl(100);

intlen=sl.Length();

cout<

cout<

inty=1000;

intp=2330;

sl.Insert(y,5);

sl.Remove(6);

sl.Find(p);

*/

CLinkedListmyList;

for(inti=1;i<=12;++i)

{myList.insert(i,i);

}

cout<<"输出遍历结果:

"<

myList.output();

cout<<"删除第个元素:

"<

intx=9;

myList.remove(x);

//遍历

myList.output();

cout<<"在第个元素位置处插入一个:

"<

x=20;

myList.insert(x,5);

//遍历

myList.output();

return0;

}

四、算法测试数据及其运行结果

五、实验小结(问题及心得)

(1)学会了线性表的基本应用,掌握了线性表的基本概念;

(2)掌握了VC++软件的应用,初步掌握了编写程序的基本步骤;

(3)掌握了线性表的实际用途。

 

实验二:

线性表类的设计与实现

一、实验目的

通过上机实践,强化课堂有关线性表的教学内容,通过动手编程实现线性表类的设计,并通过实例验证类的实例化效果及其相关应用,达到对课堂所学内容的深刻掌握。

二、实验内容

利用C++语言编程实现二叉树的构建及其先序、中序、后序遍历算法.

(1)二叉树的构建

(2)二叉树的先序遍历算法及其实现

(3)二叉树的中序遍历算法及其实现

(3)二叉树的后续遍历算法及其实现

三、实现代码

实现代码:

#include

#include

usingnamespacestd;

typedefstructBTNode

{intdata;

BTNode*lChild;

BTNode*rChild;

}SBTNode;

classCBinTree

{public:

CBinTree();

~CBinTree();

voidcreateBSTree(vector&xArray);

voidinsertNode(SBTNode*&temNode,BTNode*&root);

voidInOrder(BTNode*&root);//中序遍历

voidPreOrder(BTNode*&root);//先序遍历

voidPostOrder(BTNode*&root);//后序遍历

public:

BTNode*root;

vectorxArray;

};

CBinTree:

:

CBinTree()

:

root(NULL)

{

}

CBinTree:

:

~CBinTree()

{xArray.clear();

}

voidCBinTree:

:

createBSTree(vector&xArray)

{for(vector:

:

iteratoriter=xArray.begin();

iter!

=xArray.end();++iter)

{BTNode*temNode=newBTNode;

temNode->data=*iter;

temNode->lChild=NULL;

temNode->rChild=NULL;

insertNode(temNode,root);

}

}

voidCBinTree:

:

insertNode(SBTNode*&temNode,BTNode*&root)

{if(root==NULL)

root=temNode;

else

{if(temNode->data>root->data)

insertNode(temNode,root->rChild);

else

insertNode(temNode,root->lChild);

}

}

voidCBinTree:

:

InOrder(BTNode*&root)

{if(root==NULL)

return;

InOrder(root->lChild);

cout<data<<"";

InOrder(root->rChild);

}

voidCBinTree:

:

PreOrder(BTNode*&root)

{if(root==NULL)

return;

cout<data<<"";

PreOrder(root->lChild);

PreOrder(root->rChild);

}

voidCBinTree:

:

PostOrder(BTNode*&root)

{if(root==NULL)

return;

PostOrder(root->lChild);

PostOrder(root->rChild);

cout<data<<"";

}

int_tmain(intargc,_TCHAR*argv[])

{vectorxArray;

xArray.push_back(11);

xArray.push_back(19);

xArray.push_back(3);

xArray.push_back(8);

xArray.push_back(13);

xArray.push_back

(2);

xArray.push_back(7);

CBinTreeBT;

BT.createBSTree(xArray);

BT.InOrder(BT.root);

cout<

BT.PreOrder(BT.root);

cout<

BT.PostOrder(BT.root);

cout<

return0;}

四、算法测试数据及其运行结果

五、实验小结(问题及心得)

(1)学会了二叉树的基本应用,掌握了二叉树的基本概念;

(2)掌握了二叉树的实际用途。

 

实验三:

线性表类的设计与实现

一、实验目的

通过上机实践,强化课堂有关线性表的教学内容,通过动手编程实现线性表类的设计,并通过实例验证类的实例化效果及其相关应用,达到对课堂所学内容的深刻掌握。

二、实验内容

1)图的创建

2)基于深度优先的图的遍历算法的设计与实现

3)基于广度优先的图的遍历算法的设计与实现

4)基于Prim算法的最小生成树的构建

5)基于Kruskal算法的最小生成树的构建

三、实现代码

实现代码:

#include"stdafx.h"

#include

#include

#include

#include

#include

usingnamespacestd;

//用于存储图的节点及其相邻节点的结构体变量类型

structSGNode{intkey;//结点自身标识

mapneighNodes;//与当前结点相邻的结点集合,及其与相邻结点之间路径的权值

};//用于存储边的结构体变量类型

structSGEdge{intstart;

intend;

};//用于存储边及其权重的map容器(注意:

会按照权重自小而大自动排序)

typedefmapEdgeSet;

classCMyGraph

{public:

CMyGraph(void);

~CMyGraph(void);

public:

//其他函数,供实例调用

voidDFS();

voidDFS(inti,vector&mystack,bool*visited);

voidBFS();

voidBFS(inti,deque&mydeque,bool*visited);

voidPrim();

voidKruskal();

protected:

//属性变量

private:

//成员变量

vectorNodeSet;

};CMyGraph:

:

CMyGraph(void)

{floatg[7][7]=

{

{0,12,1,0,0,0,4},

{12,0,0,11,0,10,0},

{1,0,0,9,2,0,0},

{0,11,9,0,0,8,0},

{0,0,2,0,0,5,3},

{0,10,0,0,0,7,6},

{0,0,0,8,5,7,0},

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

{SGNodesg;

sg.key=i;

NodeSet.push_back(sg);

}for(intm=0;m<8;m++)

{for(intn=0;n<8;n++)

{if(g[m][n]!

=0)

{NodeSet[m].neighNodes.insert(map:

:

value_type(n,g[m][n]));

}

}

}vector:

:

iteratoriter;

for(iter=NodeSe

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

当前位置:首页 > 工作范文 > 行政公文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2