环境:vscode
sequencelist.h
#ifndef SEQUENCELIST_H
#define SEQUENCELIST_H
#define MAXSIZE 20 // 最大存储容量
typedef int ElemType;
class SqList
{
public:
SqList(); //
SqList(ElemType elems[], int n); // 有参构造器
~SqList(); //
bool CreatList(); // 新建一个顺序表
bool UnionList(SqList L1, SqList L2);
int LocateElem(ElemType e); // 按元素查找:成功则返回元素的序号(从1开始),失败则返回0
int ListLength(); // 顺序表的长度
bool GetElem(int i, ElemType &e); // 查找第i个位置的元素
bool ListInsert(int i, ElemType e); // 在第i个位置插入元素
bool ListDelete(int i, ElemType &e); // 删除第i个位置的元素
bool ListEmpty(); // 判空
void clearList(); // 清空顺序表
void display(); // 显示当前的顺序表
private:
ElemType data[MAXSIZE]; // 下标从0开始,但是对线性表的操作中的下标从1开始:第1个元素其实就是下标为0的元素
int length;
};
#endif // SEQUENCELIST_H
sequencelist.cpp
#include "sequencelist.h"
#include <iostream>
using namespace std;
SqList::SqList()
{
length = 0;
}
SqList::SqList(ElemType elems[], int n)
{
if (n > MAXSIZE)
{
cout << "顺序表过长,只接收了前" << MAXSIZE << "个元素" << endl;
length = MAXSIZE;
}
else
{
length = n;
}
for (int index = 0; index < length; index++)
{
data[index] = elems[index];
}
}
SqList::~SqList()
{
}
bool SqList::CreatList()
{
cout << "插入多少个元素(0-20)?" << endl;
cin >> length;
if (length < 0 || length > MAXSIZE)
{
length = 0;
return false;
}
for (int i = 1; i <= length; i++)
{
data[i - 1] = i;
}
return true;
}
bool SqList::UnionList(SqList L1, SqList L2)
{
int i, j;
for (i = 0; i < L1.length; i++)
{
data[i] = L1.data[i];
}
for (j = 0; j < L2.length; j++)
{
if (L1.LocateElem(L2.data[j]) == 0)
{
if (i > MAXSIZE)
{
return false;
}
else
{
data[i] = L2.data[j];
i++;
}
}
}
length = i;
return true;
}
int SqList::LocateElem(ElemType e)
{
for (int index=0; index < length; index++)
{
if (data[index] == e)
{
return index;
}
}
return 0;
}
int SqList::ListLength()
{
return length;
}
bool SqList::GetElem(int i, ElemType &e)
{
if (length == 0 || i < 1 || i > length)
return false;
e = data[i - 1];
return true;
}
bool SqList::ListInsert(int i, ElemType e)
{
if (length == MAXSIZE || i < 1 || i > length + 1) // 线性表满,或者i的范围不在合理范围内时返回错误
return false;
if (i <= length)
{
for (int index = length - 1; index >= i - 1; index--)
{
// 插入位置的后续元素后移一位
data[index + 1] = data[index]; // 倒序挪动位置,避免覆盖问题
}
}
data[i - 1] = e;
length++;
return true;
}
bool SqList::ListDelete(int i, ElemType &e)
{
if (length == 0 || i < 1 || i > length) // 线性表空,或者i的范围不在合理范围内时返回错误
return false;
e = data[i - 1];
for (int k = i - 1; k < length - 1; k++)
{
// 插入位置的后续元素前移一位
data[k] = data[k + 1]; // 正序挪动位置,避免覆盖问题
}
length--;
return true;
}
bool SqList::ListEmpty()
{
if (length == 0)
return true;
return false;
}
void SqList::clearList()
{
length = 0;
}
void SqList::display()
{
if (length == 0)
{
cout << "[]" << endl;
return;
}
cout << "[";
for (int i = 0; i < length - 1; i++)
{
cout << data[i] << ", ";
}
cout << data[length - 1] << "]" << endl;
}
int main()
{
SqList list;
int num;
ElemType elem;
bool flag;
cout << " 1.顺序表的创建和显示" << endl;
if (!list.CreatList())
cout << "顺序表创建失败!" << endl;
else
cout << "顺序表创建成功! " << endl;
// 顺序表的显示
list.display();
cout << endl
<< endl;
cout << " 2.按元素查找" << endl;
num = list.LocateElem(3);
cout << "3是顺序表的第" << num+1 << "个元素" << endl
<< endl
<< endl;
cout << " 3.按位置查找" << endl;
list.GetElem(4, elem);
cout << "顺序表的第4个元素是:" << elem << endl
<< endl
<< endl;
cout << " 4.顺序表的插入" << endl;
if (list.ListInsert(2, 10))
cout << "插入成功!第2个位置插入10后: " << endl;
else
cout << "插入失败!" << endl;
list.display();
cout << endl
<< endl;
cout << " 5.删除元素" << endl;
list.ListDelete(5, elem);
cout << "删掉第5个元素:" << elem << endl;
cout << "该表的长度为:" << list.ListLength() << endl;
list.display();
cout << endl
<< endl;
cout << " 6.清空顺序表" << endl;
cout << "清空顺序表前-----";
if (!list.ListEmpty())
{
cout << "当前顺序表不是空表!" << endl;
list.clearList();
cout << "清空顺序表后-----";
if (list.ListEmpty())
cout << "当前顺序表是空表!" << endl;
}
cout << endl
<< endl;
cout << " 7.合并顺序表" << endl;
ElemType elems1[8] = {0, 1, 2, 3, 4, 5, 6, 7};
ElemType elems2[9] = {5, 6, 7, 8, 9, 10, 11, 1, 12};
SqList list1 = {elems1, 8};
SqList list2 = {elems2, 9};
SqList list3;
cout << "合并前的两个表为:" << endl;
list1.display();
list2.display();
flag = list3.UnionList(list1, list2);
if (!flag)
cout << "合并后,顺序表的长度超过最大范围" << endl;
cout << "该表的长度为: " << list3.ListLength() << endl;
list3.display();
return 0;
}
标签:顺序,return,实现,ElemType,C++,int,length,SqList,data
From: https://blog.csdn.net/yangguanghhh/article/details/136829473