线性表:线性表分为顺序存储和链式存储
顺序表:
优点:随机存取
缺点:储存空间有限、不易扩展、不便于插入、删除操作
代码:
/**************************************
Copyright 陈四海
Author: created by 陈四海
Date: 2020-12-18
Description: 顺序表
Version: 1.0
**************************************/
#include<iostream>
using namespace std;
#define ListSize 40 //线性表长度
class List
{
public:
List() {
cout << "输入顺序表长度length:";
cin >> this->length;
if (this->length > ListSize || this->length < 0) {
cout << "请输入不大于40的长度!" << endl;
List();
}
else
{
cout << "请依次输入n个姓名(回车确认):";
for (int i = 1; i <= this->length; i++)
{
cin >> this->data[i];
}
cout << "空链表创建成功!" << endl;
}
}
bool getList(); //遍历线性表
bool getElem(const int& i); //按位查找
bool locateElem(const string& name);//按姓名查找
bool listInsert(const string& name);//线性表插入
bool listDelete(const int& i); //线性表删除
private:
string data[ListSize];
int length;
void setListSize(int length); //修改线性表元素个数
};
/**************************************
Fuction: setListSize
Description: 修改线性表元素个数
Input: SeqList &L, int i,int e
Output:
Return: void
Others:
**************************************/
void List::setListSize(int length) {
this->length = length;
}
/**************************************
Fuction: getList
Description: 遍历输出元素值
Input:
Output:
Return: bool
Others:
**************************************/
bool List::getList() {
if (this->length == 0)
{
cout << "线性表未定义!" << endl;
return false;
}
else if (!this->data)
{
for (int i = 0; i < this->length; i++)
{
cout << this->data[i] << endl;
}
return true;
}
else
return false;
}
/**************************************
Fuction: getElem
Description: 按位置查找
Input: const int &i
Output:
Return: bool
Others:
**************************************/
bool List::getElem(const int& i) {
if (this->length <= i || i < 0)
{
cout << "位置无效" << endl;
return false;
}
else if (this->length == 0)
{
cout << "线性表未定义!" << endl;
return false;
}
else
{
cout << "----------------------------" << endl;
cout << "学号\t 姓名\t \n" << endl;
cout << i << "\t" << this->data[i - 1] << endl;
cout << "----------------------------" << endl;
return true;
}
}
/**************************************
Fuction: locateElem
Description: 按姓名查找
Input: const string& name
Output:
Return: bool
Others:
**************************************/
bool List::locateElem(const string& name) {
if (this->length == 0)
{
cout << "线性表未定义!" << endl;
return false;
}
else
{
for (int i = this->length; i > 0; i--)
{
if (this->data[i] == name)
{
cout << "----------------------------" << endl;
cout << "学号\t 姓名\t \n" << endl;
cout << i + 1 << "\t" << this->data[i] << endl;
cout << "----------------------------" << endl;
return true;
}
}
}
}
/**************************************
Fuction: listInsert
Description: 线性表插入
Input: const string &name
Output:
Return: bool
Others:
**************************************/
bool List::listInsert(const string& name) {
if (this->length == ListSize) {
cout << "线性表已满!" << endl;
return false;
}
else {
this->data[this->length] = name;
setListSize(this->length + 1);
return true;
}
}
/**************************************
Fuction: listDelete
Description: 删除元素位置并返回该位置的元素值
Input: const int& i
Output:
Return: bool
Others:
**************************************/
bool List::listDelete(const int& i) {
if (this->length == 0) {
cout << "线性表未定义!" << endl;
return false;
}
else if (this->length <= i || i < 0)
{
cout << "位置无效" << endl;
return false;
}
else
{
string name = this->data[i - 1];
this->data[i - 1] = this->data[this->length - 1];
this->data[this->length - 1] = name;
setListSize(this->length - 1);
cout << "删除成功!" << endl;
return true;
}
}
int main() {
cout << "----------顺序表初始化---------" << endl;
List L;
int key;
while (true)
{
cout <<
"顺序表操作:\n"
"1-遍历输出顺序表\n"
"2-按位置查找元素\n"
"3-按姓名查找\n"
"4-插入元素\n"
"5-删除位置元素\n"
"0-退出" << endl;
cin >> key;
switch (key) {
case 0:return 0;
case 1:L.getList(); break;
case 2:
{
int i;
cout << "请输入查找位置:"; cin >> i;
L.getElem(i); break;
}
case 3:
{
string name;
cout << "请输入查找姓名:"; cin >> name;
L.locateElem(name);
}
case 4:
{
string name;
cout << "请输入查找姓名:"; cin >> name;
L.listInsert(name);
break;
}
case 5:
{
int i;
cout << "请输入删除位置:"; cin >> i;
L.listDelete(i); break;
break;
}
}
}