数据结构实验报告.docx
- 文档编号:26562103
- 上传时间:2023-06-20
- 格式:DOCX
- 页数:28
- 大小:75.87KB
数据结构实验报告.docx
《数据结构实验报告.docx》由会员分享,可在线阅读,更多相关《数据结构实验报告.docx(28页珍藏版)》请在冰豆网上搜索。
数据结构实验报告
数据结构实验报告
数据结构实验报告
学号:
08055140
班级:
计算机86
姓名:
邓凯
提交日期:
091216
第一次
一、上机实习题目
运用链表实现数据的排序,并检测:
(一)、12、21、34、56、23、36、87、13、987。
(二)、9、2、4、1、5、3、6、7、8、12、13、11、10。
(三)、234、162、289、999、435、90
三组数据。
二、相关知识或技术(对应DS部分)
C++的一些基本知识,以及链表的创建以及应用之类的知识。
三、算法及数据结构设计(算法设计)
voidsort(Lnode*L)//链表中元素按递增排序
{
Lnode*p,*q,*r,*s;
if(L->next!
=NULL)
{
p=L->next->next;
L->next->next=NULL;
}
while(p)
{
q=p;
p=p->next;
r=L;
s=L->next;
while(s&&s->data.shuju<=q->data.shuju)
{
r=s;
s=s->next;
}
r->next=q;
q->next=s;
}
四、上机环境和使用语言(计算机程序实现)
Microsoftvisualc++;使用c++语言
五、源程序(带注释或说明)、运行结果(数据或屏幕显示、结果分析、讨论T(n))
#include
usingnamespacestd;
structData
{
intshuju;
intinfo;
};
structLnode
{
Datadata;
Lnode*next;
};
Lnode*creat();
voidsort(Lnode*L);
voidprint(Lnode*head);
intmain(){
Lnode*L;
L=creat();
sort(L);
print(L);
return0;
}
Lnode*creat()//链表的创建
{
Lnode*L,*p,*q;
L=newLnode;
inti,n;
cout<<"请输入该链表的长度"< cin>>n; cout<<"输入数据项shuju的值"< q=L; for(i=0;i { p=newLnode; cin>>p->data.shuju; q->next=p; q=p; } q->next=NULL; returnL; } voidsort(Lnode*L)//链表中元素按递增排序 { Lnode*p,*q,*r,*s; if(L->next! =NULL) { p=L->next->next; L->next->next=NULL; } while(p) { q=p; p=p->next; r=L; s=L->next; while(s&&s->data.shuju<=q->data.shuju) { r=s; s=s->next; } r->next=q; q->next=s; } } voidprint(Lnode*L)//递增输出 { L=L->next; cout<<"递增输出数据的值"< while(L! =NULL) { cout< L=L->next; } } 输入12、21、34、56、23、36、87、13、987 运行结果: 输入: 9、2、4、1、5、3、6、7、8、12、13、11、10 运行结果: 输入: 234、162、289、999、435、90 运行结果: T(n)=n; 六、上机总结 本最近正在学线性表、栈、对列之类,我对这些知识真是陌生,尝试着写了,可总是调试不过,总会出现大堆的问题。 有时候没有错误,可也没有运行结果,已经到了最后的期限了,也只能和老师打个擦边球,运用以前还算熟悉的链表,也就是线性表做了做编程最为基础,最为常见的事情,排序。 不过,虽说如此但也是有所感悟,线性表大都会用到指针,而用指针则往往会出错,也就是说,学习数据结构,出错是在所难免的。 而且学习一定要学个清楚,在这里,只要有一丝含糊,就会出错,一定要认真对待。 七、参考资料 1.数据结构(c语言版)清华大学出版社 2.数据结构习题集清华大学出版社 3.C++面向对象程序设计西安交大出版社 第二次 一.上机实习题目 建立二叉树,并对其进行先序、中序、后序三种不同的遍历,并测试以下数据 (一)、ab##c### (二)、abc##de#g##f### (三)、abc#de##f##i##g#hj### 三组数据。 二、相关知识或技术(对应DS部分) C语言,C++的一些基本知识,树的建立、遍历以及应用之类的知识。 四、算法及数据结构设计(算法设计) 先序遍历 voidPreOrder(BinTreeroot)// { if(root! =NULL) { printf("%c",root->data); PreOrder(root->lchild); PreOrder(root->rchild); } } 中序遍历 voidInOrder(BinTreeroot)/ { if(root! =NULL) { PreOrder(root->lchild); printf("%c",root->data); PreOrder(root->rchild); } }/ 后序遍历 voidPostOrder(BinTreeroot)/ { if(root! =NULL) { PreOrder(root->lchild); PreOrder(root->rchild); }printf("%c",root->data); } 四、Microsoftvs6.0 五、源程序(带注释或说明)、运行结果(数据或屏幕显示、结果分析、讨论T(n)) #include #include typedefstructBNode { chardata; structBNode*lchild; structBNode*rchild; }BTNode; typedefBTNode*BinTree; voidCreateBinTree(BinTree*root)//以先序来建立二叉树 { charch; ch=getchar(); if(ch=='')//空格 *root=NULL;//建立空二叉树 else { *root=(BTNode*)malloc(sizeof(BTNode));//开辟空间,生成节点 (*root)->data=ch; CreateBinTree(&((*root)->lchild)); CreateBinTree(&((*root)->rchild)); } } voidPreOrder(BinTreeroot)//先序遍历 { if(root! =NULL) { printf("%c",root->data); PreOrder(root->lchild); PreOrder(root->rchild); } } voidInOrder(BinTreeroot)//中序遍历 { if(root! =NULL) { PreOrder(root->lchild); printf("%c",root->data); PreOrder(root->rchild); } } voidPostOrder(BinTreeroot)//后序遍历 { if(root! =NULL) { PreOrder(root->lchild); PreOrder(root->rchild); }printf("%c",root->data); } voidmain() { BinTreeroot; CreateBinTree(&root); printf("先序遍历: "); PreOrder(root); printf("\n"); printf("中序遍历: "); InOrder(root); printf("\n"); printf("后序遍历: "); PostOrder(root); printf("\n"); } 输入ab##c### 运行结果 输入: abc##de#g##f### 运行结果: 输入: abc#de##f##i##g#hj### 运行结果: 六、上机总结 课上的速度飞快,还没来得及看书,一章就过去了。 现在听课更还是像以前一样,云里雾里,都快成仙了,不过有些时候还是能听懂,但总也听不完全,因此做这样的实验真可谓是难啊,先得看上半天的书,然后再在网上看有没有类似的程序,忙得不可开交,好不容易找到个,编辑器通不过,只是一个错误却半天也找不到,哎,何苦呢,与其如此还不如自己写,浪费时间,却什么也没学到。 这个程序我又在网上看,看到底怎么个写法,写了半天最后还得请隔壁宿舍的同学帮忙调试,大半天弄不好。 不过,还算是搞出来了,这样简单的一个程序,却也难倒了我,哎,前路多艰,我将努力学习。 七、参考资料 1.数据结构(c语言版)清华大学出版社 2.数据结构习题集清华大学出版社 3.C++面向对象程序设计西安交大出版社 第三次 二、上机实习题目 对于十个输入的整形数据进行快速排序,并按照从小到大的顺序输出排序后结果并测试以下数据: (一)、0987654321 (二)、12,23,89,90,78,76,45,34,43,60 (三)、1,123,12,45,876,345,24,37,0,876 三组数据。 二、相关知识或技术(对应DS部分) C语言,C++的一些基本知识,快速排序的算法以及应用之类的知识。 五、算法及数据结构设计(算法设计) 快速排序中的一趟: intpartition(inta[],intlow,inthigh){ intpivotkey; pivotkey=a[low]; while(low { while(low --high; a[low]=a[high]; while(low ++low; a[high]=a[low]; } a[low]=pivotkey; returnlow; } 快速排序的递归形式: voidqsort(inta[],intlow,inthigh){ intpivotloc; if(low pivotloc=partition(a,low,high);//一趟排序结果的调用 qsort(a,low,pivotloc-1); qsort(a,pivotloc+1,high); } 四、Microsoftvs6.0 五、源程序(带注释或说明)、运行结果(数据或屏幕显示、结果分析、讨论T(n)) #include"stdio.h" #include"stdlib.h" #defineN10 #include"iostream.h" intpartition(inta[],intlow,inthigh){//快速排序中的一趟 intpivotkey; pivotkey=a[low]; while(low { while(low --high; a[low]=a[high]; while(low ++low; a[high]=a[low]; } a[low]=pivotkey; returnlow; } voidqsort(inta[],intlow,inthigh){//快速排序的递归形式 intpivotloc; if(low pivotloc=partition(a,low,high);//一趟排序结果的调用 qsort(a,low,pivotloc-1); qsort(a,pivotloc+1,high); } } voidinit(inta[]){//输入要拍的数据 inti; cout<<"请输入将要排序的十个数字"< for(i=0;i cin>>a[i]; } voidmain() { inta[N],i; init(a); qsort(a,0,N); printf("排序后的结果\n"); for(i=1;i<=N;i++){//输出排序后的数据 printf("%d",a[i-1]); if(i%10==0) printf("\n\n"); } printf("\n\n"); } 测试数据 1.输入: 0987654321 运行结果: 2.输入: 12,23,89,90,78,76,45,34,43,60 运行结果: 3.输入: 1,123,12,45,876,345,24,37,0,876 运行结果: 六、上机总结 课终于上完了,但这门课还没有结束,除了实验还有考试,迄今为止,还是很不懂,费了大半天的功夫,也只能做出这类的极其简单的程序,还是多半用的过去的知识,真是惭愧啊。 不过总还算是有些收获,至少这个程序所运用的快速排序还是学会了。 也只有慢慢来了,前路多艰,也不仅仅是数据结构这门课程,其实好多事情也正如数据结构这门课程,难,但是不得不学,对于计算机的同学,应该是不得不学会。 选择了计算机就无可避免的选择了数据结构,选择了人生也就选择了努力与奋斗,这算是这次实验的一点心得吧。 七、参考资料 1.数据结构(c语言版)清华大学出版社 2.数据结构习题集清华大学出版社 3.C++面向对象程序设计西安交大出版社 第四次 三、上机实习题目 实现树与二叉树的转化,并测试一组数据。 二、相关知识或技术(对应DS部分) C语言,C++的一些基本知识,树、二叉树的基本知识。 六、算法及数据结构设计(算法设计) voidChangeToBinary(Node*pNode) { if(pLChild==NULL) return; Node*pL=newNode(pLChild->val); pNode->pLeft=pL; pLChild->ChangeToBinary(pNode->pLeft); if(pLChild->pRSibling) { Node*pR=newNode(pLChild->pRSibling->val); pNode->pRight=pR; pLChild->pRSibling->ChangeToBinary(pNode->pRight); }四、Microsoftvs6.0 五、源程序(带注释或说明)、运行结果(数据或屏幕显示、结果分析、讨论T(n)) #include usingnamespacestd; structNode; structTreeNode; structNode { charval; Node*pLeft; Node*pRight; Node() { pLeft=NULL; pRight=NULL; } Node(charch) { val=ch; pLeft=NULL; pRight=NULL; } ~Node() { if(pLeft) pLeft->~Node(); if(pRight) pRight->~Node(); deletethis; } voidInsert(charch) { if(ch>=val) { if(pRight) pRight->Insert(ch); else { Node*add=newNode(ch); pRight=add; } } else { if(pLeft) pLeft->Insert(ch); else { Node*add=newNode(ch); pLeft=add; } } } voidShow() { cout< if(pLeft) pLeft->Show(); if(pRight) pRight->Show(); } }; structTreeNode { charval; TreeNode*pLChild; TreeNode*pRSibling; TreeNode() { pLChild=NULL; pRSibling=NULL; } TreeNode(charch) { val=ch; pLChild=NULL; pRSibling=NULL; } ~TreeNode() { if(pLChild) pLChild->~TreeNode(); if(pRSibling) pRSibling->~TreeNode(); deletethis; } voidInsert(charch) { if(ch>=val) { if(pRSibling) pRSibling->Insert(ch); else { TreeNode*pR=newTreeNode(ch); pRSibling=pR; } } else { if(pLChild) pLChild->Insert(ch); else { TreeNode*pL=newTreeNode(ch); pLChild=pL; } } } voidShow() { cout< if(pLChild) pLChild->Show(); if(pRSibling) pRSibling->Show(); } voidChangeToBinary(Node*pNode) { if(pLChild==NULL) return; Node*pL=newNode(pLChild->val); pNode->pLeft=pL; pLChild->ChangeToBinary(pNode->pLeft); if(pLChild->pRSibling) { Node*pR=newNode(pLChild->pRSibling->val); pNode->pRight=pR; pLChild->pRSibling->ChangeToBinary(pNode->pRight); } } }; classBinaryTree; classTree; classBinaryTree { public: Node*root; public: BinaryTree() { root=NULL; } ~BinaryTree() { if(root) root->~Node(); } voidInsert(charch) { if(! root) root=newNode(ch); else root->Insert(ch); } voidShow() { if(root) root->Show(); } }; classTree { public: TreeNode*root; public: Tree() { root=NULL; } ~Tree() { if(root) root->~TreeNode(); } voidInsert(charch) { if(! root) root=newTreeNode(ch); else root->Insert(ch); } voidShow() { if(root) root->Show(); } BinaryTree*ChangeToBinary() { if(root==NULL) returnNULL; BinaryTree*pBT=newBinaryTree(); pBT->root=newNode(root->val); root->ChangeToBinary(pBT->root); returnpBT; } }; intmain() { Treetree1; tree1.Insert('s'); tree1.Insert('e'); tree1.Insert('f'); tree1.Insert('c'); tree1.Insert('d'); //tree1.Insert(''); //tree1.Insert(''); //tree1.Insert(''); //tree1.Insert(''); cout<<"树输出: "< tree1.Show(); BinaryTree*bTree=tree1.ChangeToBinary(); cout<<"二叉树输出: "< bTree->Show(); return0; } 运行结果: 六、上机总结 四个小时的上机时间,居然没有编出一个像样的程序,最终也只得回去做,而且还得被扣分,其实我是写出来一个的,只是与他人的相比完全可以说是相形见绌,完全得靠c语言的知识。 一棵树的构造完全的在主程序里,繁琐之极,最主要的是没有一般性,只能就题而言,没办法,只得被扣分。 实话说今天才是第一次看这部分的内容,可谓是临时抱佛脚啊。 上课期间就完全同课本脱离了,期末了,也有复习,可还没有看到这里,哎,真可谓是“时运不济”啊。 不过,终于还是借助着辅导书做出来了。 由于忘记了题目,测试只得随便找了几个。 七、参考资料 1.数据结构(c语言版)清华大学出版社 2.数据结构习题集清华
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 实验 报告
![提示](https://static.bdocx.com/images/bang_tan.gif)