实验一线性表应用.docx

上传人:b****7 文档编号:23782893 上传时间:2023-05-20 格式:DOCX 页数:25 大小:90.04KB
下载 相关 举报
实验一线性表应用.docx_第1页
第1页 / 共25页
实验一线性表应用.docx_第2页
第2页 / 共25页
实验一线性表应用.docx_第3页
第3页 / 共25页
实验一线性表应用.docx_第4页
第4页 / 共25页
实验一线性表应用.docx_第5页
第5页 / 共25页
点击查看更多>>
下载资源
资源描述

实验一线性表应用.docx

《实验一线性表应用.docx》由会员分享,可在线阅读,更多相关《实验一线性表应用.docx(25页珍藏版)》请在冰豆网上搜索。

实验一线性表应用.docx

实验一线性表应用

实验报告

学院(系)名称:

计算机与通信工程学院

**

学号

********

专业

计算机科学与技术

班级

2015级*班

实验项目

实验一:

线性表应用

课程名称

数据结构与算法

课程代码

0661013

实验时间

2017年3月9日第一节

实验地点

7-219

考核标准

实验过程

25分

程序运行

20分

回答问题

15分

实验报告

30分

特色

功能

5分

考勤违纪情况

5分

成绩

成绩栏

其它批改意见:

 

教师签字:

考核容

评价在实验课堂中的表现,包括实验态度、编写程序过程等容等。

□功能完善,

□功能不全

□有小错

□无法运行

○正确

○基本正确

○有提示

○无法回答

○完整

○较完整

○一般

○容极少

○无报告

○有

○无

○有

○无

一、实验目的

实验目的:

理解线性表的逻辑特点;掌握顺序表、链表存储结构,以及线性表的基本操作,如插入、

删除、查找,以及线性表合并等操作在顺序存储结构和链式存储结构上的实现算法,并能够在实际

问题背景下的灵活运用线性表来解决问题,实现相应算法。

二、实验题目与要求

1.一元稀疏多项式简单的计算器

1)问题描述:

用线性表表示一元稀疏多项式,设计一个一元多项式运算器

2)要求:

(1)采用单链表存储结构一元稀疏多项式

(2)输入并建立多项式

(3)输出多项式

(4)实现多项式加、减运算

3)分析算法时间复杂度

2.约瑟夫环问题

1)问题描述:

有编号为1,2…n的n个人按顺时针方向围坐一圈,每人持有一个正整数密码。

开始给定一个正整数m,从第一个人按顺时针方向自1开始报数,报到m者出列,不再参加报数,这时将出列者的密码作为m,从出列者顺时针方向的下一人开始重新自1开始报数。

如此下去,直到所有人都出列。

试设计算法,输出出列者的序列。

2)要求:

采用顺序和链式两种存储结构实现

3)分析算法时间复杂度

3.单链表基本操作练习

1)问题描述:

在主程序中提供下列菜单:

1…建立链表

2…连接链表

3…输出链表

0…结束

2)实验要求:

算法中包含下列过程,分别完成相应的功能:

CreateLinklist():

从键盘输入数据,创建单链表

ContLinklist():

将前面建立的两个单链表首尾相连

OutputLinklist():

输出显示单链表

3)分析算法时间复杂度

4.单链表基本操作练习

1)问题描述:

已知单链表L(带头节点)是一个递增有序表,试编写算法,删除表中值大于min且小于max的节点(若表中有这样的节点),同时释放被删节点的空间。

2)实验要求:

min和max是两个给定参数。

3)分析算法时间复杂

三、实验过程与实验结果

应包括如下主要容:

Ø数据结构定义

Ø链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针次序实现的。

链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。

每个结点包括两个部分:

一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。

 

Ø算法设计思路简介

Ø通过建立包含数据和指针的节点来保存数据,然后将一系列节点通过指针插入到链表中去,实现建立链表,插入、删除节点,进而组合各种基本功能,编写符合题目要求的算法。

Ø算法描述:

可以用自然语言、伪代码或流程图等方式

Ø1、

Ø

Ø

(1)将多项式各项的系数和指数分别存在A、B两个链表的中。

Ø

(2)用指针Pa、Pb分别指向连个链表的首元素。

Ø(3)遍历两个链表,比较各元素的指数,若相同则相加减,将结果插入新表中,若不相等则将指数较小的插入新表中,继续向后遍历,直到其中一个链表到达表尾。

Ø(4)将另一表中的剩余元素按指数大小顺序全部插入到新表中。

Ø(5)新表中的元素按规定格式输出既为相加或相减后的多项式。

Ø3、

Ø

Ø

(1)通过构造函数创建单链表

Ø

(2)通过循环变量temp找到第一个链表的尾部设为p。

Ø(3)使p的next指向第二个链表的第一个元素

Ø(4)将连接后的两链表输出

Ø4、

Ø

(1)用两个变量minZ,maxZ分别存储输入的最小值和最大值

Ø

(2)遍历整个单链表,将小于minZ和大于maxZ的节点删除

Ø(3)输出操作后的单链表

 

Ø算法的实现和测试结果:

包括算法运行时的输入、输出,实验中出现的问题及解决办法等

Ø出现问题

Ø无法返回操作后的单链表

Ø解决办法

Ø经老师直到后,在相关函数前面加*成功返回操作后的单链表

Ø1、

3、

 

4、

Ø算法时间复杂度分析

Ø1、O(1表长度+2表长度)---------------可视为O(n)

Ø3、O(1表长度+2表长度)---------------可视为O(n)

Ø4、O(n)

四、收获与体会

线性表分为顺序表和链表,其中顺序表已相当熟悉,主要练了新接触的链表,感觉现在才真正体会到指针的魅力之处。

同时也明白了顺序表和链表的优缺点,在老师的知道下,更增进了对C++的理解。

算法相对来说还是比较简单的,很容易理解,关键在如何把算法变成你想要的计算机程序,在实验课以及平时的训练中,自己对代码的理解、控制能力都有所提高。

五、源代码清单

采取分文件方式编写源代码

1、

节点文件

Node.h

classNode{//元素节点

public:

Node(intiCoef=0,intiExp=0);

voidprintNode();//打印函数

intcoef;//系数

intexp;//指数

Node*next;

};

Node.cpp

#include"Node.h"

#include

usingnamespacestd;

Node:

:

Node(intiCoef,intiExp){

coef=iCoef;

exp=iExp;

}

voidNode:

:

printNode(){

if(exp==0){

cout<

}

else{

cout<

}

}

链表文件

List1.h

#include"Node.h"

classList{

public:

List();//建立链表

~List();//销毁链表

voidClearList();//清空链表

boolListInsert(inti,Node*pNode);//从指定位置将元素插入链表

boolListInsertTail(Node*pNode);//从尾部将元素插入链表

List*ListAdd(List*pList1,List*pList2);//多项式相加函数

List*ListMinus(List*pList1,List*pList2);//多项式相减函数

voidListTraverse();//遍历输出函数

private:

intm_iLength;//链表长度

Node*m_pList;//头指针

};

List1.cpp

 

#include

#include"List1.h"

usingnamespacestd;

List:

:

List(){

m_pList=newNode;

m_pList->coef=0;

m_pList->exp=0;

m_pList->next=NULL;

m_iLength=0;

}

voidList:

:

ClearList(){

Node*CurrentNode=m_pList->next;

while(CurrentNode!

=NULL){

Node*temp=CurrentNode->next;

deleteCurrentNode;

CurrentNode=temp;

}

m_pList->next=NULL;

}

List:

:

~List(){

ClearList();

deletem_pList;

m_pList=NULL;

}

boolList:

:

ListInsert(inti,Node*pNode){

Node*NewNode=newNode();

if(i<1||i>m_iLength+1){

cout<<"插入位置不合法..."<

returnfalse;

}elseif(NewNode==NULL){

cout<<"申请存失败..."<

returnfalse;

}else{

Node*temp=m_pList;

for(intk=0;k

temp=temp->next;

}

NewNode->coef=pNode->coef;

NewNode->exp=pNode->exp;

NewNode->next=temp->next;

temp->next=NewNode;

m_iLength++;

returntrue;

}

}

boolList:

:

ListInsertTail(Node*pNode){

Node*NewNode=newNode();

if(NewNode==NULL){

cout<<"申请存失败..."<

returnfalse;

}else{

Node*temp=m_pList;

while(temp->next!

=NULL){

temp=temp->next;

}

NewNode->coef=pNode->coef;

NewNode->exp=pNode->exp;

temp->next=NewNode;

NewNode->next=NULL;

m_iLength++;

returntrue;

}

}

List*List:

:

ListAdd(List*pList1,List*pList2){

Node*Pa,*Pb;

intx;

List*pList3=newList();

Pa=pList1->m_pList->next;

Pb=pList2->m_pList->next;

while(Pa&&Pb){

if(Pa->exp==Pb->exp){

x=Pa->coef+Pb->coef;

if(x!

=0){

Node*n=newNode();

n->coef=x;

n->exp=Pa->exp;

pList3->ListInsertTail(n);

}

Pa=Pa->next;

Pb=Pb->next;

}

elseif(Pa->expexp){

Node*n=newNode();

n->coef=Pa->coef;

n->exp=Pa->exp;

pList3->ListInsertTail(n);

Pa=Pa->next;

}elseif(Pa->exp>Pb->exp){

Node*n=newNode();

n->coef=Pb->coef;

n->exp=Pb->exp;

pList3->ListInsertTail(n);

Pb=Pb->next;

}

}

while(Pa){

Node*n=newNode();

n->coef=Pa->coef;

n->exp=Pa->exp;

pList3->ListInsertTail(n);

Pa=Pa->next;

}

while(Pb){

Node*n=newNode();

n->coef=Pb->coef;

n->exp=Pb->exp;

pList3->ListInsertTail(n);

Pb=Pb->next;

}

returnpList3;

}

List*List:

:

ListMinus(List*pList1,List*pList2){

Node*Pa,*Pb;

intx;

List*pList3=newList();

Pa=pList1->m_pList->next;

Pb=pList2->m_pList->next;

while(Pa&&Pb){

if(Pa->exp==Pb->exp){

x=Pa->coef-Pb->coef;

if(x!

=0){

Node*n=newNode();

n->coef=x;

n->exp=Pa->exp;

pList3->ListInsertTail(n);

}

Pa=Pa->next;

Pb=Pb->next;

}

elseif(Pa->expexp){

Node*n=newNode();

n->coef=Pa->coef;

n->exp=Pa->exp;

pList3->ListInsertTail(n);

Pa=Pa->next;

}elseif(Pa->exp>Pb->exp){

Node*n=newNode();

n->coef=Pb->coef;

n->exp=Pb->exp;

pList3->ListInsertTail(n);

Pb=Pb->next;

}

}

while(Pa){

Node*n=newNode();

n->coef=Pa->coef;

n->exp=Pa->exp;

pList3->ListInsertTail(n);

Pa=Pa->next;

}

while(Pb){

Node*n=newNode();

n->coef=Pb->coef;

n->exp=Pb->exp;

pList3->ListInsertTail(n);

Pb=Pb->next;

}

returnpList3;

}

voidList:

:

ListTraverse(){

Node*temp=m_pList->next;

cout<

while(temp!

=NULL){

temp->printNode();

if(temp->next!

=NULL){

if(temp->next->coef>0){

cout<<"+";

}elseif(temp->next->coef==0){

continue;

}else{

cout<<"";

}

}

temp=temp->next;

}

cout<

}

主文件

Demo.cpp

#include"List1.h"

#include

usingnamespacestd;

intmain()

{

Node*n0=newNode(20,0);

Node*n1=newNode(4,2);

Node*n2=newNode(2,5);

Node*n3=newNode(7,9);

Node*n4=newNode(5,12);

Node*n5=newNode(-11,18);

List*pList1=newList();

pList1->ListInsert(1,n0);

pList1->ListInsert(2,n1);

pList1->ListInsert(3,n2);

pList1->ListInsert(4,n3);

pList1->ListInsert(5,n4);

pList1->ListInsertTail(n5);

pList1->ListTraverse();

Node*m1=newNode(12,3);

Node*m2=newNode(2,5);

Node*m3=newNode(9,8);

Node*m4=newNode(20,9);

Node*m5=newNode(11,18);

List*pList2=newList();

pList2->ListInsert(1,m1);

pList2->ListInsert(2,m2);

pList2->ListInsert(3,m3);

pList2->ListInsert(4,m4);

pList2->ListInsert(5,m5);

pList2->ListTraverse();

List*pList4=newList();

pList4=pList4->ListAdd(pList1,pList2);

pList4->ListTraverse();

pList4=pList4->ListMinus(pList1,pList2);

pList4->ListTraverse();

return0;

}

3、

节点文件

Node.h

classNode{

public:

intdata;

Node*next;

Node(intiData=0);

voidprintNode();

};

Node.cpp

#include"Node.h"

#include

usingnamespacestd;

Node:

:

Node(intiData){

data=iData;

}

voidNode:

:

printNode(){

cout<

}

链表文件

List.h

#include"Node.h"

classList{

public:

List();//建立链表

~List();//销毁链表

voidClearList();//清空链表

boolListInsertTail(Node*pNode);//从尾部插入元素

List*ContLinklist(List*pList1,List*pList2);//连接链表

voidOutputLinklist();//输出链表

private:

Node*m_pList;

intm_iLength;

};

List.cpp

#include"List.h"

#include

usingnamespacestd;

List:

:

List(){

m_pList=newNode();

m_pList->data=0;

m_pList->next=NULL;

m_iLength=0;

}

List*List:

:

ContLinklist(List*pList1,List*pList2){

List*pList3=pList1;

Node*temp=pList3->m_pList;

while(temp->next!

=NULL){

temp=temp->next;

}

temp->next=pList2->m_pList->next;

returnpList3;

}

voidList:

:

OutputLinklist(){

Node*temp=m_pList;

while(temp->next!

=NULL){

temp=temp->next;

temp->printNode();

}

}

boolList:

:

ListInsertTail(Node*pNode){

Node*NewNode=newNode();

if(NewNode==NULL){

cout<<"申请存失败..."<

returnfalse;

}else{

Node*temp=m_pList;

while(temp->next!

=NULL){

temp=temp->next;

}

NewNode->data=pNode->data;

temp->next=NewNode;

NewNode->next=NULL;

m_iLength++;

returntrue;

}

}

voidList:

:

ClearList(){

Node*CurrentNode=m_pList->next;

while(CurrentNode!

=NULL){

Node*temp=CurrentNode->next;

deleteCurrentNode;

CurrentNode=temp;

}

m_pList->next=NULL;

}

List:

:

~List(){

ClearList();

deletem_pList;

m_pList=NULL;

}

主文件

Demo.cpp

#include"List.h"

#include

usingnamespacestd;

intmain()

{

Node*n1=newNode(3);

Node*n2=newNode(7);

Node*n3=newNode(5);

Node*n4=newNode(6);

Node*n5=

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

当前位置:首页 > 求职职场 > 社交礼仪

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1