线性表之顺序表C++实现

线性表之顺序表

一、头文件:SeqList.h

//顺序线性表的头文件
#include<iostream>

const int MaxSize = 100;
//定义顺序表SeqList的模板类
template<class DataType>
class SeqList{
public:
  //顺序表无参构造器(创建一个空的顺序表)
  SeqList(){ length = 0 }
  //顺序表有参构造器(创建一个长度为n的顺序表)
  SeqList(DataType array[], int n);
  //顺序表析构函数
  ~SeqList(){}
  //求顺序表的长度
  int GetLength(){ return length; }
  //顺序表按位查找,返回i位置的元素
  DataType GetElement(int i);
  //顺序表按值查找,返回该元素所在的位置
  int GetLocal(DataType x);
  //顺序表在指定的位置插入指定的元素
  void Insert(int i, DataType x);
  //顺序表删除元素,返回删除的元素
  DataType Delete(int i);
  //输出顺序表中的元素
  void PrintSeqList();
private:
  //一维数组,存放数据元素
  DataType data[MaxSize];
  //顺序表的长度
  int length;
};

//实现顺序表有参构造器
template<class DataType>
SeqList<DataType>::SeqList(DataType array[], int n)
{
  if (n > MaxSize)
  {
    throw "传入的顺序表长度过长";
  }
  //给顺序表的存储元素的数组赋值
  for (int i = 0; i < n; i++)
  {
    data[i] = array[i];
  }
  //给顺序表的长度赋值
  length = n;
}

//实现顺序表按位查找
template<class DataType>
DataType SeqList<DataType>::GetElement(int i)
{
  //判断是定的位置是否合理
  if (i < 1 || i >length)
  {
    throw "位置有误";
  }
  else
  {
    //返回指定位置的元素
    return data[i - 1];
  }
}

//实现顺序表按值查找,返回该元素所在的位置
template<class DataType>
int SeqList<DataType>::GetLocal(DataType x)
{
  //遍历顺序表的元素
  for (int i = 0; i < length; i++)
  {
    //判断指定的元素是否在顺序表中
    if (data[i] == x)
    {
      //返回指定元素在顺序表中的位置
      return (i + 1);
    }
  }
  //如果指定的元素不在顺序表中,则返回位置为0
  return 0;
}

//实现顺序表插入元素
template<class DataType>
void SeqList<DataType>::Insert(int index, DataType x)
{
  //判断插入的位置是否合理
  if (length >= MaxSize)
  {
    throw "顺序表已存放满";
  }
  if (index<1 || index>length + 1)
  {
    throw "插入元素的位置有误";
  }
  //如何插入的位置合理,则把顺序表中从最后位置到指定插位置的元素整体向后移动一个位置
  for (int j = length; j >= index; j--)
  {
    data[j] = data[j - 1];
  }
  //给插入的位置放入指定的元素
  data[index - 1] = x;
  length++;
}

//实现顺序表删除指定位置的元素
template<class DataType>
DataType SeqList<DataType>::Delete(int index)
{
  //声明要取出的元素
  DataType x;
  //判断要删除的位置是否合理
  if (index<1 || index>length)
  {
    throw "删除的位置有误";
  }
  else
  {
    //取出指定位置的元素
    x = data[index-1];
    //将指定位置后的元素全部都向前移动一个位置
    for (int i = index; i < length; i++)
    {
      data[i - 1] = data[i];
    }
    //删除顺序表中的元素后,其长度减1
    length--;
  }
  return x;
}

//顺序输出顺序表中的元素
template<class DataType>
void SeqList<DataType>::PrintSeqList()
{
  if (length < 1)
  {
    throw "顺序表中没有元素";
  }
  else
  {
    //顺序输出顺序表元素
    for (int i = 0; i < length; i++)
    {
       cout << data[i] << " ";
    }
    cout << endl;
  }
}

 二、测试线性表之顺序表:TestSeqList.cpp

#include<iostream>
#include"SeqList.h"
using namespace std;
void show()
{
  cout << "---------------------------------------" << endl;
}
int main()
{
  int array[10] = {1,3,4,2,5,6,8,7,9,10};
  SeqList<int> seqList = SeqList<int>(array,10);
  cout << "顺序表为:" << endl;
  seqList.PrintSeqList();
  show();
  cout << "顺序表的长度为:" << seqList.GetLength()<< endl;
  cout << "第三个位置的元素是:" << seqList.GetElement(3) << endl;
  cout << "元素3的位置是:" << seqList.GetLocal(3) << endl;
  show();
  cout << "在第5个位置插入元素22" << endl;
  seqList.Insert(5, 22);
  cout << "顺序表为:" << endl;
  seqList.PrintSeqList();
  cout << "顺序表的长度为:" << seqList.GetLength() << endl;
  show();
  cout << "删除第5个位置的元素" << endl;
  seqList.Delete(5);
  cout << "顺序表为:" << endl;
  seqList.PrintSeqList();
  cout << "顺序表的长度为:" << seqList.GetLength() << endl;
  show();
  return 0;
}

猜你喜欢

转载自www.linuxidc.com/Linux/2017-04/142466.htm
今日推荐