您的位置:首页 > 理论基础 > 数据结构算法

数据结构严蔚敏 线性表基本操作C++实现

2019-03-05 21:15 537 查看

【数据结构(严蔚敏)】 线性表基本操作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[], int Lenth)  //以数组初始化线性表
{
this->lenth = Lenth;
this->listsize = Lenth * 2;			//表长设置为初始长度两倍
this->elem = new ElemType[listsize];
for (int i = 0; i < Lenth; i++)//将数组中的元素一次赋值到顺序表
{
this->elem[i] = values[i];
}
}

Linear_List::~Linear_List()
{
delete[]this->elem;
}

int Linear_List::ListLenth()	 //返回表长
{
return lenth;
}

void Linear_List::ListClear()	//将表置空
{
this->lenth = 0;
}

bool Linear_List::ListEmpty()	 //判断表是否为空
{
if (this->lenth == 0) return true;
return false;
}

void Linear_List::ListInsert(int i, ElemType e)  //算法2.4 插入元素
{
if (i<1 || i>this->lenth + 1)
{
cout << "illegal i" << endl;
exit(0);
}
if (this->lenth >= this->listsize)    //空间不够则进行扩充
{
int len = this->lenth + LISTINCREMENT;
ElemType* newbase = (ElemType *) realloc(this->elem,len * sizeof(ElemType));
if (!newbase) exit(OVERFLOW);
this->elem = newbase;
this->listsize += LISTINCREMENT;
}

ElemType* q;
q = &(this->elem[i - 1]);
ElemType *p;
for (p = &(this->elem[this->lenth - 1]); p >= q; --p) *(p + 1) = *p;	//元素右移
this->elem[i - 1] = e;	//插入元素
this->lenth++;			//表长加1
}

void Linear_List::ListDelete(int i, ElemType &e) //算法2.5  删除元素
{
if (i<1 || i>this->lenth )
{
cout << "illegal i" << endl;
exit(0);
}

e = this->elem[i - 1];    //将删除元素值赋给e
ElemType * p;
p = &(this->elem[this->lenth - 1]);  //p为表尾地址
ElemType * q;
for (q = &(this->elem[i - 1]);q<=p-1; ++q) *q = *(q+1);  //删除元素之后左移
this->lenth--;		//表长减1
}

ElemType Linear_List::GetElem(int i)  //返回i处元素值
{
if (i<1 || i>this->lenth )
{
cout << "illegal i" << endl;
system("pause");
exit(0);
}
return (this->elem[i - 1]);

}

int Linear_List::LocateElem( ElemType e, bool(*compare)(ElemType, ElemType))  //在顺序线性表中查找第一个值与e满足compare()元素的位序
{
int i = 1;
ElemType * p;
p = this->elem;
while(i <= this->lenth && !(*compare)(*p++, e))++i;
if (i <= this->lenth) return i;
else return 0;
}
void Linear_List::PriorElem(ElemType cur_e, ElemType &pre_e) //返回cur_e的前驱,若无,则失败
{
for (int i = 1; i < this->lenth;i++)
{
if (this->elem[i] == cur_e) { pre_e = this->elem[i - 1]; return; }
}
cout << "无前驱元素" << endl;
system("pause");
exit(0);

}

void Linear_List::NextElem(ElemType cur_e, ElemType &next_e) //返回cur_e的后继,若无,则失败
{
for (int i = 0; i < this->lenth-1; i++)
{
if (this->elem[i] == cur_e) { next_e = this->elem[i + 1];  return;
}
}
cout << "无后继元素" << endl;
system("pause");
exit(0);
}

void Linear_List::ListTraverse(void(*visit)(ElemType))		//对线性表每个元素调用visit()
{
for (int i = 0; i < this->lenth; i++)
{
visit(this->elem[i]);
}
}

测试代码

#include <string.h>
#include <iostream>
using namespace std;
#include <exception>

#include "LinearList.h"
bool compare(int a, int b)
{
if (a == b) return 1;
else return 0;
}
void visit(int a)
{
cout << a << endl;

}
int main()
{
Linear_List list(5);    //初始化新表,表长为5
for (int i = 0; i < 5; i++)list.ListInsert(1, i);  //往表中插入8个元素

cout << "遍历输出线性表" << endl;

list.ListTraverse(visit);  //遍历显示整个表

list.ListInsert(3, 100);     //在表中第五个位置插入元素100
cout << "插入元素后新表为:" << endl;
list.ListTraverse(visit);

cout << "返回表中元素100的位置:   "<<list.LocateElem(100, compare)<<endl;
int e;
list.NextElem(100, e);
cout << "元素100的后继元素为: " << e << endl;

for (int i = 0; i < 3; i++)list.ListDelete(1,e);   //删除表中8个元素
cout << "删除前3个元素后结果" << endl;
list.ListTraverse(visit);
system("pause");
return 0;

}

测试结果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: