顺序表
顺序表是指用一组地址连续的存储单元依次存储线性表的数据元素,这种表示也称为线性表的顺序存储结构或顺序映像
顺序表的特点是逻辑上相邻的数据元素在物理次序上也是相邻的。
顺序表的定义
class student {//表内数据元素的类型
public:
int ID;
string name;
student() {}
student(int ID, string name) {
this->ID = ID;
this->name = name;
}
};
class student_array {//顺序表的定义
public:
student* elem;
int length;
};
顺序表的初始化
顺序表的初始化既为顺序表分配一个大小为MAXSIZE的空间
void initList(student_array& List) {//初始化
List.elem = new student[MAXSIZE];
List.length = 0;
cout<<"初始化成功"<<endl;
}
顺序表的取值
取值操作时根据序号i来获取顺序表中对应位置的元素值,因为采用数组的方式进行存储,所以可直接返回list.elem[i-1]来获取对应元素。
bool getElem(student_array &list, int i,student &elem){//取值
if(i < 1 || i > list.length || list.length == 0)return false;
elem = list.elem[i-1];
return true;
}
顺序表的查找
查找既根据元素值来查找其在表中的位置,步骤如下:
(1)从第一个元素起依次进行比较,若相等则返回i+1;
(2)若遍历完也未比较成功,则查找失败
int LocateElem(student_array &list,int ID){//查找
for(int i = 0; i < list.length; i++){
if(list.elem[i].ID == ID)return i + 1;//此处因为c++无法使用==判断两个对象是否相等,所以改成判断ID是否相等,输入数据时请注意保证ID的唯一性
}
return 0;
}
顺序表的插入
往表中插入一个元素,算法步骤如下:
(1)首先判断插入位置是否合法以及存储空间是否已满
(2)将第i-1个位置到第length-1位置之间的元素依次后移一个位置
(3)将第i-1位置的元素赋值为你想插入的元素
(4)将表的长度+1
int ListInsert(student_array &list, int i,student &elem){//插入
if( i <= 0 || i > list.length + 1 || list.length == MAXSIZE ) return 0;
//cout<<"开始插入"<<endl;
for ( int j = list.length - 1; j >= i - 1; j--){
list.elem[ j + 1 ] = list.elem[ j ];
}
//cout<<elem.name<<endl;
list.elem[ i - 1 ] = elem;
list.length++;
return 1;
}
顺序表的删除
将指定位置的元素删除,算法步骤如下:
(1)判断i值是否合法
(2)将第i到第length-1位置的元素前移
int ListDelete(student_array &list, int i){
if( i < 0 || i > list.length ) return 0;
for( int j = i - 1; j < list.length -1; j++){
list.elem[j] = list.elem[j+1];
}
list.length--;
return 1;
}
完整代码
#include<iostream>
#include<string.h>
using namespace std;
#define MAXSIZE 1000
class student {
public:
int ID;
string name;
student() {}
student(int ID, string name) {
this->ID = ID;
this->name = name;
}
};
class student_array {
public:
student* elem;
int length;
};
void initList(student_array& List) {//初始化
List.elem = new student[MAXSIZE];
List.length = 0;
cout<<"初始化成功"<<endl;
}
bool getElem(student_array &list, int i,student &elem){//取值
if(i < 1 || i > list.length || list.length == 0)return false;
elem = list.elem[i-1];
return true;
}
int LocateElem(student_array &list,int ID){//查找
for(int i = 0; i < list.length; i++){
if(list.elem[i].ID == ID)return i + 1;//此处因为c++无法使用==判断两个对象是否相等,所以改成判断ID是否相等,输入数据时请注意保证ID的唯一性
}
return 0;
}
int ListInsert(student_array &list, int i,student &elem){//插入
if( i <= 0 || i > list.length + 1 || list.length == MAXSIZE ) return 0;
//cout<<"开始插入"<<endl;
for ( int j = list.length - 1; j >= i - 1; j--){
list.elem[ j + 1 ] = list.elem[ j ];
}
//cout<<elem.name<<endl;
list.elem[ i - 1 ] = elem;
list.length++;
return 1;
}
int ListDelete(student_array &list, int i){
if( i < 0 || i > list.length ) return 0;
for( int j = i - 1; j < list.length -1; j++){
list.elem[j] = list.elem[j+1];
}
list.length--;
return 1;
}
void ListCout(student_array &list){
for( int i = 0; i < list.length; i++){
cout<<list.elem[i].ID<<" "<<list.elem[i].name<<endl;
}
}
void student_array_opearte() {
student_array List;
bool i = true;
int j = 0,ID,station;
string name;
cout<<"请先初始化顺序表以便进行后续操作"<<endl;
while (i) {
cout << "***请选择你想要进行的操作******" << endl;
cout << "***1:初始化顺序表*********" << endl;
cout << "***2:取值*****************" << endl;
cout << "***3:查找某一项的值**********" << endl;
cout << "***4:在某个位置插入值*" << endl;
cout << "***5:删除某一项************" << endl;
cout << "***6:输出顺序表************" << endl;
cout << "***7:退出************" << endl;
cin >> j;
switch (j)
{
case 1:
{
initList(List);
cout<<"请选择你接下来要进行的操作:"<<endl;
break;
}
case 2:
{
cout<<"请输入要取值元素的位置"<<endl;
cin>>station;
student s1{};
if(getElem(List,station,s1)){
cout<<"所取元素为:"<<s1.ID<<" "<<s1.name<<endl;
}
else{
cout<<"失败"<<endl;
}
cout<<"请选择你接下来要进行的操作:"<<endl;
break;
}
case 3:
{
cout<<"请输入需要查找的ID:"<<endl;
cin>>ID;
if(!LocateElem(List,ID)) cout<<"查找失败!"<<endl;
else cout<<"所查找元素所在位置为:"<<LocateElem(List,ID)<<endl;
cout<<"请选择你接下来要进行的操作:"<<endl;
break;
}
case 4:
{
cout<<"请输入需要插入元素的值:"<<endl;
cout<<"学号:"<<endl;
cin>>ID;
cout<<"姓名:"<<endl;
cin>>name;
cout<<"位置:"<<endl;
cin>>station;
student s{ID,name};
if(ListInsert(List,station,s)) cout<<"插入成功!"<<endl;
else cout<<"插入失败"<<endl;
cout<<"请选择你接下来要进行的操作:"<<endl;
break;
}
case 5:
{
cout<<"请输入需要删除的位置!"<<endl;
cin>>station;
if(ListDelete(List,station)) cout<<"删除成功!"<<endl;
else cout<<"删除失败!"<<endl;
cout<<"请选择你接下来要进行的操作:"<<endl;
break;
}
case 6:
{
ListCout(List);
cout<<"请选择你接下来要进行的操作:"<<endl;
break;
}
case 7:
{
i = false;
break;
}
}
}
}
int main() {
student_array_opearte();
return 0;
}
第一次写博客,如果有不足的地方还望各位大佬指正,感谢。