《数据结构》线性表-顺序表

线性表的顺序存储结构称为顺序表,其基本思想就是用一段地址连续的存储单元依次存储线性表的数据元素。

如何来实现

通常用一维数组来实现顺序表,也就是把线性表中相邻的元素储存在数组中相邻的位置,从而形成一一对应的关系。

需要注意的一点是:C++语言中数组下标是从0开始的,而线性表中元素的序号是从1开始的,也就是说线性表中的第i个元素存储在数组下标为i-1的位置上。

代码实现

#include <iostream>
using namespace std; 

const int MaxSize = 100; //100只是示例性的数据,根据实际问题具体定义
template <class DataType> //定义模板类SeqList
class SeqList
{
public:
SeqList( ); //无参构造函数,建立空的顺序表
SeqList(DataType a[ ], int n); //有参构造函数,建立长度为n的顺序表
~SeqList( ); //析构函数
int Length( ); //求线性表的长度
int Empety();
DataType Get(int i); //按位查找,查找第i个元素的值
int Locate(DataType x ); //按值查找,查找值为x的元素序号
void Insert(int i, DataType x); //插入操作,在第i个位置插入值为x的元素
DataType Delete(int i); //删除操作,删除第i个元素
void PrintList( ); //遍历操作,按序号依次输出各元素
private:
DataType data[MaxSize]; //存放数据元素的数组
int length; //线性表的长度
};

template<class DataType>
SeqList<DataType> :: ~SeqList()
{

}

template <class DataType>
SeqList<DataType> :: SeqList()
{
length = 0;
}

template <class DataType>
int SeqList<DataType> :: Empety()
{
if(length == 0)
return 1;
else
return 0; 
}

template <class DataType>
int SeqList<DataType> :: Length()
{
return length;
}

template <class DataType> 
SeqList<DataType> :: SeqList(DataType a[ ], int n)
{
if (n > MaxSize) 
throw "参数非法";
for (int i = 0; i < n; i++) 
data[i] = a[i];
length = n;
}

template <class DataType> 
void SeqList<DataType> :: PrintList( )
{
for (int i = 0; i < length; i++)
cout << data[i]; //依次输出线性表的元素值
}

template <class DataType> 
int SeqList<DataType> :: Locate(DataType x)
{
for (int i = 0; i < length; i++)
if (data[i] == x) return i+1; //返回其序号i+1
return 0; //退出循环,说明查找失败
}

template <class DataType> 
DataType SeqList<DataType> :: Get(int i)
{
if (i < 1 && i > length) 
throw "查找位置非法";
else 
return data[i - 1];
}

template <class DataType> 
DataType SeqList<DataType> :: Delete(int i)
{
if (length == 0) 
throw "下溢";
if (i < 1 || i > length) 
throw "位置";
int x = data[i - 1]; //取出位置i的元素
for (int j = i; j < length; j++)
data[j - 1] = data[j]; //此处j已经是元素所在的数组下标
length--;
return x;
}

template <class DataType> 
void SeqList<DataType> :: Insert(int i, DataType x)
{
if (length >= MaxSize) 
throw "上溢";
if (i < 1 || i > length + 1) 
throw "位置";
for (int j = length; j >= i; j--)
data[j] = data[j - 1]; //第j个元素存在数组下标为j-1处
data[i - 1] = x;
length++;
}

int main( )
{
int r[5] = {1, 2, 3, 4, 5}, i, x; 
SeqList<int> L{r, 5}; //建立具有5个元素的顺序表
cout << "当前线性表的数据为:";
L.PrintList( ); //输出当前线性表1 2 3 4 5
try 
{
L.Insert(2, 8); //在第2个位置插入值为8的元素
cout << endl << "执行插入操作后数据为:";
L.PrintList( ); //输出插入后的线性表1 8 2 3 4 5
cout << endl;
}catch(char* str){
cout << str << "插入操作错误!" << endl;
}

cout << "当前线性表的长度为:" << L.Length( ); //输出线性表的长度6
cout << endl;
cout << "请输入查找的元素值:";
cin >> x;
i = L.Locate(x);
if (0 == i) cout << "查找失败" << endl;
else cout << "元素" << x << "的位置为:" << i << endl; 
try
{
cout << "请输入查找第几个元素值:";
cin >> i;
cout << "第" << i << "个元素值是" << L.Get(i) << endl;
}catch(char* str){
cout << "线性表中没有该元素" << endl;
} 
try 
{
cout << "请输入要删除第几个元素:";
cin >> i;
x = L.Delete(i); //删除第i个元素
cout << "删除的元素是" << x <<",删除后数据为:";
L.PrintList( ); //输出删除后的线性表
}catch(char* str){
cout << "删除错误!" << endl;
} 

return 0;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值