实验1

  • 格式:doc
  • 大小:228.50 KB
  • 文档页数:18

下载文档原格式

  / 18
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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

实验项目1:线性表存储及运算

学号姓名课程号实验地点指导教师时间

评语:

按时完成实验;实验内容和过程记录完整;回答问题完整、正确;实验报告的撰写认真、格式符合要求;无抄袭的行为。

成绩教师签字

线性表链式存储(双向链表)插入、删除运算

1、预习要求:线性表的插入、删除相关概念及运算,完成线性表元素的插入、删除。

2、实验目的:

(1)了解线性表的插入、删除相关概念;

(2)理解线性表的插入、删除过程和结构定义;

(3)掌握算法转换为程序的过程中的变化。

3、实验内容及要求:

(1)分别建立包含10个数据元素的链式存储线性表;

(2)从键盘输入一个数据元素,插入到线性表中第k(包含0号位置)个位置;

(3)从键盘输入一个数据元素关键字或位置k(包含1号位置),从线性表中删除相应数据元素;

(4)给出程序及插入、删除前和插入、删除后线性表结果。

4、实验设备(环境)及要求

硬件:支持 Intel Pentium Ⅱ及其以上 CPU ,内存 128MB 以上、硬盘 1GB 以上容量的微机。

软件:配有 Windows98/2000/XP 操作系统,安装 Visual C++ 。

5、实验时间:6学时

6、该文档的文件名不要修改,存入<学号> <姓名> 命名的文件夹中

7、该表中的数据只需填空,已有内容不要修改

实验结果(运行结果界面及源程序,运行结果界面放在前面):

实验程序如下:

#define EXPRESS EType

#define HeadEType int

#include

#include

#include

using namespace std;

struct EXPRESS

{

char name[5];

char number[5];

char place[5];

int weight;

};

struct DoubleNode

{

EType data;

DoubleNode *plink;

DoubleNode *nlink;

};//节点类型定义

struct HeadNode

{

HeadEType Hdata;

DoubleNode *first;

} ;

typedef HeadNode *DoubleChainList; //表头节点结构类型定义

void CreatDoubleChainList(DoubleChainList &L)

{

L=new HeadNode;

L->first=NULL;

};//构造一个空链表

void OutputDoubleChainList(DoubleChainList &L) {//输出链表中的元素

DoubleNode*current=L->first;

while(current)

current=current->nlink;

current=L->first;

cout<<" ";//4个空格

while(current)

{

cout<<<" ";//3个空格

current=current->nlink;

}

cout<

current=L->first;

cout<<" ";//8个空格

while(current)

{

cout<data.number<<" ";//3个空格

current=current->nlink;

}

cout<

current=L->first;

cout<<" ";//8个空格

while(current)

{

cout<data.place<<" ";//3个空格

current=current->nlink;

}

cout<

current=L->first;

cout<<" ";//8个空格

while(current)

{

cout<data.weight<<" ";//3个空格

current=current->nlink;

}

cout<

}

int LengthDoubleChainList(DoubleChainList &L)

{//返回链表中的节点数

DoubleNode*current=L->first;

int len=0;

while(current)

{

len++;

current=current->nlink;

}

return len;

}//确定链表的长度

void DestroyDoubleChainList(DoubleChainList &L)

{

DoubleNode *current;

current=L->first;

while(L->first)

{

current=current->nlink;

delete L->first;

L->first=current;

if(current)

current->plink=NULL;

}

}//删除链表中的数据节点,并释放空间

bool GetElemDoubleChainList(DoubleChainList &L,int k,EType&result)

{//L中第K个元素取至x中,如不存在返回false,找到返回true,result 带回if(k<1) return false;

DoubleNode *current=L->first;

int index=1;

while(index

{