当前位置:文档之家› 《数据结构》实验3实验报告

《数据结构》实验3实验报告

南京工程学院实验报告
1.熟悉上机环境,进一步掌握语言的结构特点。

2.掌握线性表的顺序存储结构的定义及实现。

3.掌握线性表的链式存储结构——单链表的定义及实现。

4.掌握线性表在顺序存储结构即顺序表中的各种基本操作。

5.掌握线性表在链式存储结构——单链表中的各种基本操作。

二、实验内容
1.顺序线性表的建立、插入及删除。

2.链式线性表的建立、插入及删除。

三、实验步骤
1.建立含n个数据元素的顺序表并输出该表中各元素的值及顺序表的长度。

2.利用前面的实验先建立一个顺序表L={21,23,14,5,56,17,31},然后在第i个位置插入元素(学号)。

3.建立一个带头结点的单链表,结点的值域为整型数据。

要求将用户输入的数据按尾插入法来建立相应单链表。

四、程序主要语句及作用
程序1的主要代码(附简要注释)
#include <string.h>
#include "stdio.h"
#include <malloc.h>
#include <conio.h>
typedef struct BSTNODE
{
int data;
struct BSTNODE *lchild;
struct BSTNODE *rchild;
}BSTNODE;
BSTNODE* initBST(int n, BSTNODE *p)
{
if(p==NULL)
{
p=(BSTNODE*)malloc(sizeof(BSTNODE));
p->lchild=NULL;
p->rchild=NULL;
p->data=n;
}
else if(n>p->data)
p->rchild=initBST(n,p->rchild);
else
p->lchild=initBST(n,p->lchild);
return p;
}
void inorder(BSTNODE *BT){
if(BT!=NULL){
inorder(BT->lchild);
printf("%d",BT->data);
inorder(BT->rchild);
}
}
BSTNODE *search_btree(BSTNODE *root,int key) { if(!root)
{printf("Emptu btree\n"); return root;}
while(root->data!=key)
{
if(key<root->data)
root=root->lchild;
else
root=root->rchild;
if(root==0)
{ printf("Search Failure\n");
break;
}
}/*while(root->info!=key*/
if(root!=0)
printf("Successful search\n key%d\n",root->data); }/* *search_btree(root,key) */
int main()
{
BSTNODE *p=NULL;
int i,n,sd;
int a[100];
printf("enter the number of nodes:");
scanf("%d",&n);
printf("enter the number of the tree:");
for(i=0;i<n;i++)
{
scanf("%d",&a[1]);
p=initBST(a[1],p);
}
inorder(p);
printf("\n please input search data:");
scanf("%d",&sd);
search_btree(p,sd);
getch();
return 1;
}
程序2的主要代码(附简要注释)
#include<stdio.h>
#include<stdlib.h>
#define OVERFLOW 0
#define OK 1
typedef int Status;
typedef char ElemType;
typedef struct BiTree
{
ElemType data;
struct BiTree *Lchild;
struct BiTree *Rchild;
}BiTree,*Binary_Tree;
//创建一个二叉树
Binary_Tree CreateBiTree(Binary_Tree T) {
char ch;
T=(Binary_Tree)malloc(sizeof(BiTree));
if(!T)
exit(OVERFLOW);
scanf("%c",&ch);
if(ch=='0')
T=NULL;
else
{
T->data=ch;
T->Lchild=CreateBiTree(T->Lchild);
T->Rchild=CreateBiTree(T->Rchild); }
return T;
}
//先遍历二叉树
Status PreShowBiTree(Binary_Tree T)
{
if(T!=NULL)
{
printf("%c ",T->data);
PreShowBiTree(T->Lchild);
PreShowBiTree(T->Rchild);
}
return OK;
}
//中遍历二叉树
Status MidShowBiTree(Binary_Tree T) {
if(T!=NULL)
{
printf("%c ",T->data);
MidShowBiTree(T->Lchild);
MidShowBiTree(T->Rchild);
}
return OK;
}
//后遍历二叉树
Status BehShowBiTree(Binary_Tree T)
{
if(T!=NULL)
{
printf("%c ",T->data);
BehShowBiTree(T->Lchild);
BehShowBiTree(T->Rchild);
}
return OK;
}
int main()
{
BiTree *T;
printf("请在此处创建一个二叉树:\n");
T=CreateBiTree(T);
printf("先序遍历:");
PreShowBiTree(T);
printf("\n中序遍历:");
MidShowBiTree(T);
printf("\n后序遍历:");
BehShowBiTree(T);
printf("\n");
getch();
}
五、程序运行结果截图
程序1
程序2
程序3
六、收获,体会及问题(写得越详细、越个性化、越真实越好,否则我不知道你做这个实验的心路历程,也就无法充分地判断你是否是独立完成的这个实验、你是否在做这个实验时进行了认真仔细地思考、通过这个实验你是否在实践能力上得到了提高)。

相关主题