6数据结构实验

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
【实验内容】 1、 要求将二叉排序树的建立、插入、删除、显示等算法合并在一个综合程序中,用户
可通过菜单选择方式运行各种操作算法。 2、 一直哈希表的表长为 m,哈希函数为 H(key)=key MOD p,用开放定址法(增量序
列采用线性探测再散列)解决冲突,试编写构造哈希表的程序。 二叉排排序树:
#include<stdio.h> #include<malloc.h> #define TRUE 1 #define FALSE 0 typedef int DataType; typedef int KeyType; struct BinTreeNode; typedef struct BinTreeNode * PBinTreeNode; struct BinTreeNode {
position->llink=p; else position->rlink=p; } void midOrder(PBinTreeNode ptree) { PBinTreeNode p; p=ptree; if(p==NULL)return; midOrder(p->llink); printf(" %d ",p->key); midOrder(p->rlink); }
BiTree bt; char x; x = getchar();
if(x == '#' ) {
bt = NULL; } else {
bt = new BiTNode; bt->data = x; bt->lchild = this->CreateBiTree(); bt->rchild = this->CreateBiTree();
} this->T = bt; return bt; };
void BITREE::InOrderTraverse(BiTree bt) {
if(bt != NULL) {
if(bt->lchild != NULL) {
this->InOrderTraverse(bt->lchild); } cout<<bt->data<<" "; if(bt->rchild != NULL) {
void preOrder(PBinTreeNode ptree) {
PBinTreeNode p;
p=ptree; if(p==NULL)return; printf(" %d ",p->key); preOrder(p->llink); preOrder(p->rlink); }
void postOrder(PBinTreeNode ptree) {
【实验内容】(问题描述,数据描述,算法描述,程序清单,测试数据) 二叉树采用二叉链表作存储结构,用编程实现二叉树的如下基本操作: 1. 按先序序列构造一棵二叉链表表示的二叉树 T; 2. 对这棵二叉树进行中序遍历,输出结点的遍历序列; 3. 给定的顶点和边的信息构造图的邻接矩阵存储; 4. 对改图进行深度优先搜索,输出搜索得到的结点序列。 【实验要求】 上交实验报告,要求同上。 关于树: #include <iostream> #include "BiTree.h" using namespace std; BiTree BITREE::CreateBiTree() {
void deleteNode(PBinTree pbtree,KeyType key) {
PBinTreeNode position,p,q,parent; p=*pbtree,parent=p; int tag=0; position=NULL; while(p!=NULL){ // printf("%d",p->key); if(p->key==key){
this->Destory(bt->lchild); }
if(bt->rchild != NULL) {
this->Destory(bt->rchild); } cout<<bt->data<<" "; delete bt; } else { return;
} } 关于图: #include "MGraph.h"
position=p; break; }else if(p->key>key){ parent=p; p=p->llink; tag=Βιβλιοθήκη Baidu1; }else { parent=p; p=p->rlink;
tag=1; } } position=p; if(position == NULL){ printf("\n 不含有此关键字!\n"); return; }else if(position==*pbtree){
①实验名称 ②实验目的 ③实验内容:问题描述:数据描述:算法描述:程序清单:测试数据 算法: #include <stdio.h> #include <stdlib.h> typedef struct LPeople { int num; struct LPeople *next; }peo;
void Joseph(int n,int m)
{ PBinTreeNode p,q; p=*ptree;q=p; while(p!=NULL) { q=p; if(p->key==key) { *position=p; return(TRUE); }else if(p->key>key)p=p->llink; else p=p->rlink; } *position=q; return(FALSE);
//用循环链表实现
{
int i,j;
peo *p,*q,*head;
head=p=q=(peo *)malloc(sizeof(peo));
p->num=0;p->next=head;
for(i=1;i<n;i++)
{
p=(peo *)malloc(sizeof(peo));
p->num=i;q->next=p;p->next=head;
this->InOrderTraverse(bt->rchild); } } else { return; } //-+a##*b##-c##d##/e##f## }; void BITREE::Destory(BiTree bt) { if(bt != NULL) { if(bt->lchild != NULL) {
free(position); } } int main() { PBinTree newBTree; insertNode(newBTree,5); insertNode(newBTree,3); insertNode(newBTree,7); insertNode(newBTree,2); insertNode(newBTree,4); insertNode(newBTree,6); insertNode(newBTree,8); insertNode(newBTree,1); printf("---------midOrder-----"); midOrder(*newBTree); printf("\n---------preOrder-----");
PBinTreeNode p; p=ptree; if(p==NULL)return; postOrder(p->llink); postOrder(p->rlink); printf(" %d ",p->key); }
void deleteTree(PBinTreeNode pbtree) {
PBinTreeNode p=pbtree; if(p==NULL)return; deleteTree(pbtree->llink); deleteTree(pbtree->rlink); free(p); }
哈希表:
#include<malloc.h> #define NULL 0 typedef char * InfoType; typedef int KeyType; typedef struct node {
printf("\n 是根节点!"); tag=0; } else
printf("\n 它的父节点是%d",parent->key); if(position->llink==NULL){
if(tag==-1)parent->llink=position->rlink; if(tag==1)parent->rlink=position->rlink; free(position); }else{ PBinTreeNode trchild=position->llink; while(trchild->rlink!=NULL){ trchild=trchild->rlink; } trchild->rlink=position->rlink; if(tag==-1)parent->llink=position->llink; if(tag==1)parent->rlink=position->llink; if(tag==0)*pbtree=position->llink;
}
q=p;p=p->next;
i=0;j=1;
while(i<n)
{
if(j%m==0)
{
q->next=p->next;
p=q->next;
printf("%4d",j%n);
i++;
}
else
{
q=p;p=p->next; } j++; } printf("\n"); } void main() { int n,m; /*人的个数*/ printf("Please Enter n amd m(n>m):\n"); scanf("%d%d",&n,&m); Joseph(n,m); }
【实验内容】 约瑟夫问题的实现:n 只猴子要选猴王,所有的猴子按 1,2,…,n 编号围坐一圈,从第 一号开始按 1,2…,m 报数,凡报到 m 号的猴子退出圈外,如此次循环报数,知道圈内 剩下一只猴子时,这个猴子就是猴王。编写一个程序实现上述过程,n 和 m 由键盘输入。 【实验要求】 1、 要求用顺序表和链表分别实现约瑟夫问题。 2、 独立完成,严禁抄袭。 3、 上的实验报告有如下部分组成:
preOrder(*newBTree); deleteNode(newBTree,5); printf("\n---------midOrder-----"); midOrder(*newBTree); printf("\n---------preOrder-----"); preOrder(*newBTree); printf("\n");
}
void insertNode(PBinTree ptree,KeyType key) {
PBinTreeNode p,position; if(searchNode(ptree,key,&position)==TRUE) return; p=(PBinTreeNode)malloc(sizeof(struct BinTreeNode)); if(p==NULL){printf("Error!\n");exit(1);} p->key=key; p->llink=p->rlink=NULL; if(position==NULL) *ptree=p; else if(key<position->key)
void main() {
MGRAPH G; G.CreateUDN(); cout<<endl<<endl
<<"------邻接矩阵------" <<endl; G.DisplayMatrix(); cout<<endl<<"深度优先遍历结果:"; G.Depth_FirstSearch(); cout<<endl<<endl; }
KeyType key; DataType other; /*元素的属性*/ PBinTreeNode llink,rlink; }; typedef struct BinTreeNode * BinTree; typedef BinTree * PBinTree;
int searchNode(PBinTree ptree,KeyType key,PBinTreeNode *position)
相关文档
最新文档