用一组地址连续的存储单元依次存储线性表元素,线性表的这种机内表示称作线性表的顺序存储结构,简称顺序表。
特点:为表中相邻的元素Ai和Ai+1赋以相邻的存储位置LOC(Ai)和LOC(Ai+1)。换句话说,以元素在计算机内“物理位置相邻”来表示线性表中数据元素之间的逻辑关系。
上代码:#includeusing namespace std; const int SIZE = 100; typedef int Type; //构造一个线性表类 class List { public: List(Type size); ~List(); int Find(Type n); //查找元素,并返回下标 bool Full(); bool Empty(); void Out(); //打印表 bool Insert(int index, Type m); //指定位置插入元素 bool Delete(int index); //删除指定位置元素 void GetListLength(); protected: Type* p;//基地址 int Size; int Length; }; //构造一个空表 List::List(Type size) { this->Size = size; this->Length = 0; this->p = new Type[Size];//堆区申请内存 if (!p) { cout << "内存分配失败" << endl; exit(-1); } } //析构 List::~List() { delete[]p; } void List::GetListLength() { cout << "表的长度为" << Length << endl; } //查找元素是否存在(返回下标) int List::Find(Type n) { if (!Empty()) { for (int i = 0; i < Length; i++) { if (p[i] == n)//如果找到就返回元素下标+1 { return i + 1; } } } //元素不在表中返回-1; return -1; } bool List::Insert(int index, Type m) { //判断插入位置是否合理 if (index<1 || index>Length + 1||Full()) { return false; } else { Length++;//表长度+1 //从最后一个元素开始逐个往后移 for (int i = Length - 1; i >= index - 1; --i) { p[i] = p[i - 1]; } //插入 p[index - 1] = m; return true; } } bool List::Delete(int index) { //判断删除位置是否合理 if (index<1 || index>Length + 1) { return false; } else { //从当前元素开始后继元素逐个前移 for (int i = index-1; i



