Seqlist.h
#ifndef __SEQLIST_H__
#define __SEQLIST_H__
#include <iostream>
const int INIT_SIZE = 4;
class Seqlist
{
public:
Seqlist(); //普通构造函数
Seqlist(const Seqlist& seqlist); //拷贝构造函数
Seqlist& operator = (const Seqlist& seqlist); //重载"="操作符
~Seqlist(); //析构函数
void Clean(); //清空所有元素
int Size(); //返回顺序表的长度,即表中元素个数
double Get(int t); //取得顺序表中t处元素
double Prior(int t); //取t的前驱元素
double Next(int t); //取t的后继元素
int Find(double x); //找到元素x在顺序表中第一次出现的位置
void Add(int t, double x); //在顺序表中位置t处加入元素x
void Add(double x); //在顺序表的末尾加入元素x
void Delete(int t); //删除顺序表中位置t处的元素
void Delete(); //删除顺序表中末尾的元素
bool IsEmpty(); //如果顺序表为空表返回true,否则返回false
void Traverse(); //遍历输出所有元素
void Sort(); //对所有元素进行从小到大排序
void Update(int t, double x); //修改顺序表中位置t处的元素为x
void Grow(); //数组扩容
private:
int m_size = 0;
int m_length = INIT_SIZE;
double *m_data = new double[m_length];
};
#endif
Seqlist.cpp
//*****头文件*****//
#include "Seqlist.h"
#include <iostream>
//*****命名空间*****//
using namespace std;
//*****函数定义*****//
//构造函数
inline
Seqlist::Seqlist()
{
NULL;
}
//拷贝构造函数
inline
Seqlist::Seqlist(const Seqlist& seqlist)
{
m_size = seqlist.m_size;
int length = sizeof(seqlist.m_data) / sizeof(seqlist.m_data[0]);
m_data = new double[length];
for (int i = 0; i < seqlist.m_size; i++)
{
m_data[i] = seqlist.m_data[i];
}
}
//重载"="操作符
inline
Seqlist& Seqlist::operator = (const Seqlist& seqlist)
{
//自我赋值检测
if (this == &seqlist)
{
return *this;
}
//清空原有空间;
delete[] m_data;
//分配空间并赋值
m_size = seqlist.m_size;
int length = sizeof(seqlist.m_data) / sizeof(seqlist.m_data[0]);
m_data = new double[length];
for (int i = 0; i < m_size; i++)
{
m_data[i] = seqlist.m_data[i];
}
return *this;
}
//析构函数
inline
Seqlist::~Seqlist()
{
delete[] m_data;
}
//清空所有元素
inline
void Seqlist::Clean()
{
this->m_size = 0;
}
//返回顺序表的长度,即表中元素个数
inline
int Seqlist::Size()
{
return this->m_size;
}
//取得顺序表中t处元素
inline
double Seqlist::Get(int t)
{
if (t < 0 || t >= this->m_size)
{
cout << "获取位置" << t << "处的元素失败!!!" << endl << "ERROR:数组越界!!!" << endl;
return 0;
}
return this->m_data[t];
}
//取t的前驱元素
inline
double Seqlist::Prior(int t)
{
if (t < 1 || t >= this->m_size)
{
cout << "获取位置" << t << "的前驱元素失败!!!" << endl << "ERROR:数组越界!!!" << endl;
return 0;
}
return this->m_data[t - 1];
}
//取t的后继元素
inline
double Seqlist::Next(int t)
{
if (t < 0 || t >= this->m_size - 1)
{
cout << "获取位置" << t << "的后继元素失败!!!" << endl << "ERROR:数组越界!!!" << endl;
return 0;
}
return this->m_data[t + 1];
}
//找到元素x在顺序表中第一次出现的位置
inline
int Seqlist::Find(double x)
{
const double EPSINON = 0.000001;
for (int i = 0; i < this->m_size; i++)
{
if ((x - this->m_data[i]) >= -EPSINON && (x - this->m_data[i]) <= EPSINON)
{
return i;
}
}
cout << "元素" << x << "在该顺序表中未出现过" << endl;
return -1;
}
//在顺序表中位置t处加入元素x
inline
void Seqlist::Add(int t, double x)
{
this->Grow();
for (int i = this->m_size - 1; i >= t; i--)
{
this->m_data[i + 1] = this->m_data[i];
}
this->m_data[t] = x;
this->m_size++;
}
//数组扩容
inline
void Seqlist::Grow()
{
//判断数组是否已满
if (m_size >= m_length)
{
//如果数组已满,给其分配一块更大的空间
double *newArr = new double[m_length + (m_length>>1)];
//将数据拷贝进新的空间
for (int i = 0; i < m_size; i++)
{
newArr[i] = m_data[i];
}
释放原内存
delete[] m_data;
//将新空间地址赋给m_data指针
m_data = newArr;
m_length = m_length + (m_length >> 1);
}
}
//在顺序表的末尾加入元素x
inline
void Seqlist::Add(double x)
{
this->Add(this->m_size, x);
}
//删除顺序表中位置t处的元素
inline
void Seqlist::Delete(int t)
{
if (this->IsEmpty())
{
cout << "当前是空表" << endl;
}
else
{
for (int i = t + 1; i < this->m_size; i++)
{
this->m_data[i - 1] = this->m_data[i];
}
this->m_size--;
}
}
//删除顺序表中末尾的元素
inline
void Seqlist::Delete()
{
this->Delete(this->m_size - 1);
}
//如果顺序表为空表返回true,否则返回false
inline
bool Seqlist::IsEmpty()
{
if (this->m_size == 0)
{
return true;
}
else
{
return false;
}
}
//遍历输出所有元素
inline
void Seqlist::Traverse()
{
cout << "[";
for (int i = 0; i < this->m_size; i++)
{
cout << this->m_data[i];
if (i != this->m_size - 1)
{
cout << ", ";
}
}
cout << "]" << endl;
}
//修改顺序表中位置t处的元素为x
inline
void Seqlist::Update(int t, double x)
{
if (t < 0 || t >= this->m_size)
{
cout << "修改位置" << t << "处的元素失败!!!" << endl << "ERROR:数组越界!!!" << endl;
return;
}
this->m_data[t] = x;
}
//对所有元素进行从小到大排序
inline
void Seqlist::Sort()
{
if (this->m_size <= 1)
{
return;
}
for (int i = 1; i < this->m_size; i++)
{
int tmp = this->m_data[i];
int j;
for (j = i - 1; j >= 0; j--)
{
if (tmp < this->m_data[j])
{
m_data[j + 1] = m_data[j];
}
else
{
break;
}
}
m_data[j+1] = tmp;
}
}