顺序表(类模板,包括插入、删除等基本操作)

//头文件sq_list.h

#define _SQ_LIST_H_
#ifndef DEFAULT_SIZE
#define DEFAULT_SIZE 1000//缺省元数个数
//顺序表模板
template <class ElemType>
class SqList
{
protected://数据成员
ElemType* elems;//元素存储空间
int maxSize;//顺序表最大元素个数;
int count;//元素个数
public:
SqList(int size = DEFAULT_SIZE);//构造函数模板
virtual~SqList();//析构函数模板
int Length()const;//求线性表长度
bool Empty()const;//判断线性表是否为空
void Clear();//将线性表清空
void Traverse(void(*vist)(const ElemType&))const;//遍历线性表
bool GetElem(int position, ElemType& e);//求指定位置的元素值
bool SetElem(int position, const ElemType& e);//设置指定位置的元素值
bool Delete(int position, ElemType& e);//删除元素
bool Delete(int position);
bool Insert(int position, const ElemType& e);//插入元素
SqList(const SqList<ElemType>& source);//复制构造函数模板
SqList<ElemType>& operator=(const SqList<ElemType>& source);//重载运算符
};
//顺序表类模板实现部分
template<class ElemType>
SqList<ElemType>::SqList(int size)//构造一个最大元素个数为size的空顺序表
{
maxSize = size;//最大元素个数
elems = new ElemType[maxSize];//分配储存空间
count = 0;//空线性表元素个数为0;
}
template<class ElemType>
SqList<ElemType>::~SqList()//销毁线性表
{
delete[]elems;//释放存储空间
}
template<class ElemType>
int SqList<ElemType>::Length()const {//返回线性表元素个数
return count;
}
template<class ElemType>
bool SqList<ElemType> ::Empty()const//判断线性表是否为空,为返回1,否为0
{
return count == 0;
}
template<class ElemType>
void SqList<ElemType> ::Clear()//清空线性表
{
count= 0;
}
template<class ElemType>
void SqList<ElemType> ::Traverse(void(*vist)(const ElemType&))const
//依次对线性表的每个元素调用函数(*visit)
{
for (int temPos = 1; temPos <= Length(); temPos++)//对线性表的每个元素调用函数(*visit)
{
(*vist)(elems[temPos - 1]);
}
}
template<class ElemType>
bool SqList<ElemType> ::GetElem(int position, ElemType& e)
//当线性表存在第position个元素是,用e返回其值,返回ture,否则返回false
{
if (position<1 || position>Length()) {
return false;//范围错误
}
else {//范围正确
e = elems[position - 1];
return true;
}
}
template<class ElemType>
bool SqList<ElemType> ::SetElem(int position, const ElemType& e)
//删除线性表第position个元素,并用e返回其值
//position合法时返回ture,否则false
{
if (position<1 || position>Length()) {
return false;
}
else {//position合法
elems[position - 1] = e;
return true;
}
}
template<class ElemType>
bool SqList<ElemType> ::Delete(int position, ElemType& e)//删除线性表的第position个元素
{//涉及到线性表position的每次都要判断position是否合法
if (position<1 || position>Length()) {
return false;
}
else {//position合法
ElemType temElem;
for (int temPos = position + 1; temPos <= Length(); temPos++)
//被删除元素之后的元素依次左移
{
GetElem(temPos, temElem);//用返回被删除元素的值
SetElem(temPos - 1, temElem);
}
count--;
return true;//删除成功
}
}
template<class ElemType>
bool SqList<ElemType> ::Delete(int position)//删除线性表的第position个元素
{//涉及到线性表position的每次都要判断position是否合法
if (position<1 || position>Length()) {
return false;
}
else {//position合法
ElemType temElem;
for (int temPos = position + 1; temPos <= Length(); temPos++)
//被删除元素之后的元素依次左移
{
GetElem(temPos, temElem);//用返回被删除元素的值
SetElem(temPos - 1, temElem);
}
count--;
return true;//删除成功
}
}
template<class ElemType>
bool SqList<ElemType> ::Insert(int position, const ElemType& e){
//在线性表的第position个元素前插入元素e
if (count == maxSize)//线性表已满返回false;
return false;
else if (position<1 || position>Length() + 1) //范围错误
return false;
else {//position合法
ElemType temElem;//临时元素
for (int temPos = Length(); temPos >= position; temPos--) {
GetElem(temPos, temElem);
SetElem(temPos+1, temElem);
}
count++;
SetElem(position, e);
return true;
}
}
template<class ElemType>
SqList<ElemType> ::SqList(const SqList<ElemType>& source) {
//线性表source构造新的线性表表--复制构造模板
maxSize = source.maxSize;
count = source.maxSize;
elems = new ElemType[maxSize];
for (int temPos = 1; temPos <= source.Length(); temPos++) {
//复制数据元素
elems[temPos - 1] = source.elems[temPos - 1];
}
}
template<class ElemType>
SqList<ElemType>& SqList<ElemType>:: operator=(const SqList<ElemType>& source)
//将线性表source赋值给当前线性表--重载赋值运算符
{
if (&source != this)//防止自赋值
{
maxSize = source.maxSize;
count = source.count;
delete[]elems;
elems = new ElemType[maxSize];
for (int temPos = 1; temPos <= source.Length(); temPos++)//
{
elems[temPos-1]= source.elems[temPos - 1];
}
}
return *this;
}
#endif

 

//源文件main.cpp

#include<iostream>
using namespace std;
#include "sq_list.h"
template<class ElemType>
void Show(const ElemType& e)
{//显示数据元素
cout << e << " ";
}
int main() {
char select = '0';
SqList<double>la, lb;
double e;
int position;
while (select != '7')
{
cout << endl << "1.生成线性表.";
cout << endl << "2.显示线性表.";
cout << endl << "3.检索元素.";
cout << endl << "4.设置元素值.";
cout << endl << "5.删除元素值.";
cout << endl << "6.插入元素.";
cout << endl << "7.退出.";
cout << endl << "选择功能(1-7):";
cin >> select;
switch (select)
{
case '1'://生成线性表
cout << endl << "输入e(e=0时退出)";
cin >> e;
while (e != 0)
{
la.Insert(la.Length() + 1, e);
cin >> e;
}
break;
case '2'://显示线性表
lb = la;
lb.Traverse(Show<double>);
break;
case '3'://检索元素
cout << endl << "输入元素位置值:";
cin >> position;
if (!la.GetElem(position, e))
cout << "没有此位置的元素" << endl;
else
cout << "元素:" << e << endl;
break;
case'4'://设置元素值
cout << endl << "输入要替换的位置:";
cin >> position;
cout << endl << "输入替换为的元素:";
cin >> e;
if (!la.SetElem(position, e))
cout << "位置范围错误." << endl;
else
cout << "设置成功." << endl;
break;
case '5'://删除元素值
cout << endl << "输入位置:";
cin >> position;
if (!la.Delete(position, e) && !la.Delete(position))
cout << "位置范围错." << endl;
else
cout << "被删除的元素值:" << e << endl;
break;
case'6'://插入元素
cout << endl << "请输入位置:";
cin >> position;
cout << endl << "请输入元素值:";
cin >> e;
if (!la.Insert(position, e))
cout << "位置范围错误." << endl;
else
cout << "成功插入" << e << endl;
break;
}
}
return 0;
}

 

posted @ 2021-10-23 20:42  Grit_L。  阅读(203)  评论(0编辑  收藏  举报