【数据结构(严蔚敏)】 线性表基本操作C++实现(顺序表)
基本代码
vs2017
#pragma once
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
typedef int ElemType;
# define LISTINCREMENT 2;
class Linear_List
{
public:
Linear_List(int listSize); //初始化空表
Linear_List(ElemType values[], int Lenth); //用数组初始化表
~Linear_List();
ElemType GetElem(int i); //返回第I个元素值
int ListLenth(); //返回表长
void ListClear(); //将表置空
bool ListEmpty(); //判断表是否为空
void ListInsert(int i, ElemType e); //在i处插入元素e
void ListDelete(int i, ElemType &e); //删除i处元素,用e返回其值
void PriorElem(ElemType cur_e,ElemType &pre_e); //返回cur_e的前驱,若无,则失败
void NextElem(ElemType cur_e, ElemType &next_e); //返回cur_e的后驱,若无,则失败
int LocateElem( ElemType e, bool(*compare)(ElemType, ElemType)); //在顺序线性表中查找第一个值与e满足compare()元素的位序
void ListTraverse(void(*visit)(ElemType)); //对线性表每个元素调用visit()
private:
void init(ElemType values[], int n);
protected:
ElemType * elem; //存储空间基址
int lenth; //当前长度
int listsize; //当前分配的存储容量
};
Linear_List::Linear_List(int listSize)
{
this->listsize = listSize;
this->lenth = 0;
this->elem = new ElemType[listsize];
}
Linear_List::Linear_List(ElemType values[]