实验报告1—线性表的顺序结构
2017-10-07 14:18
387 查看
实验报告1——线性表的顺序结构
[align=left] [/align][align=left]
[/align]
[align=left]
[/align]
顺序表的建立与操作实现
一、实验内容
顺序表的建立与操作实现建立n 个元素的顺序表(n 的大小和表里数据自己确定),实现相关的操作:输出,插 入,删除,查找等功能。编写完整程序实现,程序语言不限定,使用技术形式不定。
二、实验步骤
1、实验程序中用到的数据类型的定义
const int m=8; //线性表容量
private:
int n[m]; //存放元素的数组
int length; //线性表长度
2、相关操作的算法表达
(1)、template<class dt>
D<dt>::D(dt c[m],int i)
{ //初始化线性表
for(int j=0;j<i;j++)
{ n[j]=c[j];
length++;}
cout<<"初始化后的线性表的元素值依次为:";
for(int j=0;j<length;j++)
cout<<n[j]<<" ";
}
(2)、void D<dt>::sprint()
{
cout<<"线性表的元素值依次为:"; //遍历数组元素
for(int j=0;j<length;j++)
cout<<n[j]<<" ";
}
(3)、void D<dt>::insert(intt,int s)
{
if(t<0||t>m) cout<<"abnormal!"; //插入元素
for(int j=length;j<=t;j--)
n[j]=n[j-1];
n[t-1]=s;
length++;
cout<<"线性表的元素值依次为:";
for(int j=0;j<length;j++)
cout<<n[j]<<" ";
}
(4)、int D<dt>::get(int i)
{ if(i<0||i>m) cout<<" abnormal!"; //按位置寻值
cout<<n[j];
return n[j];
}
(5)int D<dt>::locate (inta)
{
int i; //按值寻位置
for(intj=0;j<length;j++)
{ i=j;
if(n[j]==a)
cout<<"locate:"<<i;
return i;}
if(n[length]!==a) cout<<" Do not exist!";
}
(6)、void D<dt>::modify(inti,int s)
{
if(i<0||i>m) cout<<" abnormal!"; //修改元素 n[i-1]=s;
}
(7)、void D<dt>::delect(inti) //删除元素
{
if(i<0||i>m) cout<<" abnormal!";
for(int j=i;j<length;j++)
n[j-1]=n[j];
length--;
cout<<"Finish!";
cout<<"删除后的线性表的元素值依次为:";
for(int j=0;j<length;j++)
cout<<n[j]<<" ";
}
3、完整程序
#include <iostream>
using namespace std;
const int m=8; //线性表长度
template<class dt>
class D
{
private:
dt n[m];
int length;
public:
D(); //无参构造
D(dt c[m],int i); //有参构造
~D();
void delect(int i); //删除
void modify(int i,dt s); //修改
dt get(int i); //按位置寻值
int locate(dt a); //按值寻位置
void insert(int t,dt s); //插入
void sprint(); //遍历
};
template<class dt>
void D<dt>::modify(int i,dt s)
{
if(i<0||i>m) cout<<" abnormal!"; //修改
n[i-1]=s;
}
template<class dt>
void D<dt>::delect(int i) //删除
{
if(i<0||i>m) cout<<" abnormal!";
for(int j=i;j<length;j++)
n[j-1]=n[j];
length--;
cout<<"Finish !";
cout<<"删除后的线性表的元素值依次为:";
for(int j=0;j<length;j++)
cout<<n[j]<<" ";
}
template<class dt>
dt D<dt>::get(int i)
{ //按位置寻值
if(i<0||i>m) cout<<" abnormal!";
cout<<n[j];
return n[j];
}
template<class dt>
int D<dt>::locate (dt a)
{
int i;
for(int j=0;j<length;j++) //按值寻位置
{
i=j;
if(n[j]==a)
cout<<"locate:"<<i;
return i;}
if(n[length]!==a) cout<<" Do not exist!";
}
template<class dt>
void D<dt>::insert(int t,dt s) //插入
{
if(t<0||t>m) cout<<"abnormal!";
for(int j=length;j<=t;j--)
n[j]=n[j-1];
n[t-1]=s;
length++;
cout<<"线性表的元素值依次为:";
for(int j=0;j<length;j++)
cout<<n[j]<<" ";
}
template<class dt>
D<dt>
4000
;::D(dt c[m],int i)
{ //初始化线性表
for(int j=0;j<i;j++)
{ n[j]=c[j];
length++;}
cout<<"初始化后的线性表的元素值依次为:";
for(int j=0;j<length;j++)
cout<<n[j]<<" ";
}
template<class dt>
void D<dt>::sprint()
{ //遍历
cout<<"线性表的元素值依次为:";
for(int j=0;j<length;j++)
cout<<n[j]<<" ";
}
template<class dt>
D<dt>::D()
{}
template<class dt>
D<dt>::~D()
{}
template<class dt>
void main()
{
dt a[m];
int i=0;
int n;
cout<<"输入8个数:";
while(i<m)
{cin>>a[i];
i++;
n=i;}
D<dt> d1();
D<dt> d2(a[m],n);
d2.sprint();
d2.insert(3,28);
d2.delete(7);
d2.get(6);
d2.locate(8);
}
4、总结、运行结果和分析
5、总体收获和不足,疑问等
收获:在写程序的过程中,线性表的顺序结构的相关代码已经渐渐熟悉,基本算法思想学习得到进一步的复习与巩固。在后来的调试过程中,又因各种各样的错误和不断解决问题而丰富了个人编程的经历和经验和提高个人解决问题的能力。对于已经忘记的知识,重新拿起书复习,使得知识得到复习与巩固。
不足:敲打代码的速度不够快,且一些问题尚未能够把得到解决。
疑惑:觉得代码已经没有问题,但还是未能调试成功。
相关文章推荐
- 线性结构 实验报告 20162305
- 【实验报告】数据结构实验二:线性表的实验
- C++数据结构实验报告:顺序表实现
- 20162317袁逸灏 第四周实验报告:实验一 线性结构
- 线性结构-实验报告
- 数据结构实验之图书馆借书线性表的顺序和链式存取结构的应用
- 线性表的相关操作 数据结构实验报告第二个
- 【线性表二:】大学数据结构实验报告中的两三个关于线性表的小算法
- [SDUT](2116)数据结构实验之链表一:顺序建立链表 ---链式存储(线性表)
- 数据结构实验一:基于线性表的学生信息管理(顺序结构)
- 数据结构实验一:基于线性表的学生信息管理(顺序结构)
- 线性结构-实验报告
- 数据结构实验之链表一:顺序建立链表
- 排序二叉树、查找、二分法查找、数据结构,实验报告
- 数据结构实验之查找六:顺序查找
- 实验一:线性表实验-顺序表的实现
- 数据结构之:线性表的顺序表示和实现
- OJ 1946: C语言实验——单个字符输入和输出(顺序结构)
- 实验二 线性表综合实验之顺序表
- 数据结构笔记(一)线性表的顺序表示和基本操作及其顺序表实现的集合运算(A-B)U(B-A)实例