前言
该部分知识参考于《数据结构(C语言版 第2版)》24~28页
🌈每一个清晨,都是世界对你说的最温柔的早安:ૢ(≧▽≦)و✨
注意
这里的ElemType是以Book类型的数据作为举例,如果需要更改可以自行改变!
1、宏定义
#include<iostream>
#include <cstdlib>
using namespace std;//控制最大值
#define MAXSiZE 1000
//声明Status用于记录返回结果
typedef int Status;
#define OK 1
#define ERROR 0
2、对存储的数据内容进行定义
//定义书的基本数据项
typedef struct
{char no[20];char name[50];float peice;
}Book;
//定义顺序表的数据元素
typedef struct
{Book* elem;int length;
}SqList;//声明顺序表
SqList L;
3、初始化顺序表
//定义书的基本数据项
typedef struct
{char no[20];char name[50];float peice;
}Book;
//定义顺序表的数据元素
typedef struct
{Book* elem;int length;
}SqList;//声明顺序表
SqList L;
4、销毁顺序表
//销毁
void DestoryList(SqList& L)
{if (L.elem)delete[]L.elem; //回收指针空间
}
5、清空顺序表
//清空
void Clear(SqList& L)
{L.length = 0; //将线性表的表长重置
}
6、求线性表的长度
//求线性表的长度
int GetLength(SqList L)
{return L.length;
}
7、判空
//判空
int IsEmpty(SqList L)
{if (L.length == 0)return 1;return 0;
}
8、取值
取值操作根据指定的位置下标来进行,查找到所在位置下标的数据后传给e,然后将e通过引用传回主函数部分
//取值
Status GetElem(SqList L, int i, Book& e)
{if (i < 1 || i > L.length)return ERROR;e = L.elem[i - 1];return OK;
}
9、查找
查找是根据指定的元素e,查找顺序表中第1个值与e相等的元素。若查找成功,则返回该元素在表中的位置序;若查找失败。则返回0。
//查找
int LocateElem(SqList L, Book e)
{for (int i = 0; i < L.length; i++){if (strcmp(L.elem[i].no, e.no) == 0)return i + 1;}return OK;
}
10、插入
//插入
Status ListInsert(SqList& L, int i, Book e)
{if (i < 1 || i > L.length + 1)//插入位置可以是最后一个元素的后面return ERROR;if (L.length == MAXSiZE)return ERROR;for (int j = L.length - 1; j >= i - 1; j--){L.elem[j + 1] = L.elem[j];}L.elem[i - 1] = e;L.length++;return OK;
}
11、删除
//删除
Status ListDelete(SqList& L, int i, Book& e)
{if (i<1 || i>L.length)//判断位置是否合法{return ERROR;}e = L.elem[i - 1]; //将删除的值赋予e,达到一个返回的作用for (int j = i; j <= L.length - 1; j++){L.elem[j - 1] = L.elem[j];}L.length--;return OK;
}
12、总代码
#include<iostream>
#include <cstdlib>
using namespace std;//控制最大值
#define MAXSiZE 1000
//声明Status用于记录返回结果
typedef int Status;
#define OK 1
#define ERROR 0//定义书的基本数据项
typedef struct
{char no[20];char name[50];float peice;
}Book;
//定义顺序表的数据元素
typedef struct
{Book* elem;int length;
}SqList;//声明顺序表
SqList L;//创建
Status InitList(SqList &L)
{L.elem = new Book[MAXSiZE];if (!L.elem)exit(EXIT_FAILURE);L.length = 0;return OK;
}//销毁
void DestoryList(SqList& L)
{if (L.elem)delete[]L.elem; //回收指针空间
}//清空
void Clear(SqList& L)
{L.length = 0; //将线性表的表长重置
}//求线性表的长度
int GetLength(SqList L)
{return L.length;
}//判空
int IsEmpty(SqList L)
{if (L.length == 0)return 1;return 0;
}//取值
Status GetElem(SqList L, int i, Book& e)
{if (i < 1 || i > L.length)return ERROR;e = L.elem[i - 1];return OK;
}//查找
int LocateElem(SqList L, Book e)
{for (int i = 0; i < L.length; i++){if (strcmp(L.elem[i].no, e.no) == 0)return i + 1;}return OK;
}//插入
Status ListInsert(SqList& L, int i, Book e)
{if (i < 1 || i > L.length + 1)//插入位置可以是最后一个元素的后面return ERROR;if (L.length == MAXSiZE)return ERROR;for (int j = L.length - 1; j >= i - 1; j--){L.elem[j + 1] = L.elem[j];}L.elem[i - 1] = e;L.length++;return OK;
}//删除
Status ListDelete(SqList& L, int i, Book& e)
{if (i<1 || i>L.length)//判断位置是否合法{return ERROR;}e = L.elem[i - 1]; //将删除的值赋予e,达到一个返回的作用for (int j = i; j <= L.length - 1; j++){L.elem[j - 1] = L.elem[j];}L.length--;return OK;
}int main()
{InitList(L);Book book1 = { "001","数据结构",59 };ListInsert(L, 1, book1);Book book2 = { "002","计算机网络",23.6 };ListInsert(L, 1, book2);int length = GetLength(L);Book book3;GetElem(L, 2, book3);cout << book3.name<<" "<< book3.peice << endl;cout << length << endl;
}
测试结果
结语
这里的代码对于初学者读起来有些困难,因为这里面穿插使用着C和C++语言,如果有不懂的可以在后台私信,如有不足也希望可以指正
对于顺序表不仅仅只有一种固定方法来写,大体思路学会后可以自己尝试出自己的风格,奋斗!