C++顺序表的实现

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;
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用c++实现顺序表:多文件编程,层次清晰,函数有注释 SeqList();//构造函数,存储的元素个数设为0 bool setLength(size_t length);//设置已经存储的元素个数 bool addElement(ElemType element);//把某个元素添加到顺序表末尾 bool addElement(ElemType element , size_t n);//插入一个元素,使其成为第n个元素,其余元素后移 bool delElement();//删除所有的元素 bool delElement(size_t n);//删除第n个元素 bool delElement(string elementDetailType,string elementDetail);//通过某个元素细节找到元素,把这个元素删除 bool replaceElement(ElemType element , size_t n);//使用一个元素,替换掉第n个元素 bool swapElement(size_t n1 , size_t n2);//把第n1个元素和第n2个元素交换 ElemType* getElement();//得到数组头的指针 ElemType* getElement(size_t n);//得到第n个元素的指针 size_t getLength();//得到存储的元素个数 size_t getMaxSize();//得到顺序表容量 bool showElementDetail();//输出所有的元素细节 bool showElementDetail(size_t n);//输出第n个元素的细节 bool showElementDetail(string elementDetailType,string elementDetail);//通过某个元素细节找到元素,输出元素所有细节 size_t findElement(string elementDetailType,string elementDetail);//通过某个元素细节找到元素位置 static int inputAInt(int min = 0,int max = 9,int defaultValue = -1);//从键盘读取,限制为一个min到max间的整数,非法情况返回defaultValue void startControlLoop();//打开控制界面 ~SeqList();//析构函数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值