数据结构(C++) - 记顺序表的实现

顺序表是用一组地址连续的存储单元依次存储线性表中的数据元素。
特点

  1. 表中元素的逻辑顺序与物理顺序相同(因此插入和删除操作需要移动大量的元素)
  2. 随机访问
  3. 存储密度高(每个节点只存储数据元素)
/*
ArrayList.h
*/
#pragma once

template <class DataType>
class ArrayList
{
public:
	ArrayList();
	ArrayList(int capacity);
	~ArrayList();
	// 获取当前顺序表长度
	int Length();
	// 按值查询(返回位序)
	int LocateElement(DataType x);
	// 按位插入元素
	bool Insert(DataType data, int i);
	// 顺序表末尾添加元素
	bool Add(DataType data);
	// 判断顺序表是否为空
	bool IsEmpty();
	// 按位删除
	bool DeleteByIndex(int i);

private:
	// 初始容量
	int capacity;
	// 注: 线性表中元素位序从1开始, 数组中元素下标从0开始
	// 顺序表长度
	int length;
	// 数组指针(动态分配数组)
	DataType* p;
};
/*
ArrayList.cpp
*/

#include "ArrayList.h"
#include <iostream>
using namespace std;

template<class DataType>
ArrayList<DataType>::ArrayList()
{
	this->capacity = 10;
	this->p = new DataType[capacity];
	this->length = 0;
}

/**
 * 输入: 顺序表初始化容量
 * 输出:
 */
template<class DataType>
ArrayList<DataType>::ArrayList(int capacity)
{
	this->capacity = capacity;
	this->p = new DataType[capacity];
	this->length = 0;
}

template<class DataType>
ArrayList<DataType>::~ArrayList()
{
	// 释放指针
	delete[] p;
}


/**
 * 操作:获取表长
 * 输入:
 * 输出: 当前顺序表长度
 */
template<class DataType>
int ArrayList<DataType>::Length()
{
	return this->length;
}

/**
 * 操作:按值查询
 * 输入: 待查询元素
 * 输出: 待查询元素在顺序表中的第一次出现的位序
 * 平均时间复杂度:O(n)
 */
template<class DataType>
int ArrayList<DataType>::LocateElement(DataType x)
{
	int i;
	for (i = 0; i < length; i++)
	{
		if (x == p[i])
		{
			return i;
		}
	}
	// 顺序表中不存在该元素
	return 0;
}

/**
 * 操作:按位插入
 * 输入: 待插入元素
 *		 待插入位置
 * 输出:
 * 平均时间复杂度:O(n)
 */
template<class DataType>
bool ArrayList<DataType>::Insert(DataType data, int i)
{
	if (i < 1 || i > length + 1) {
		cout << "参数错误" << endl;
		return false;
	}
	if (length >= capacity)
	{
		cout << "顺序表已满" << endl;
		return false;
	}

	// 待插入位置后面的元素依次后移一位
	for (int j = length; j >= i; j--)
	{
		p[j] = p[j - 1];
	}
	p[i - 1] = data;
	length++;
	return true;
}

/**
 * 操作:表尾添加元素
 * 输入: 待添加元素
 * 输出:
 */
template<class DataType>
bool ArrayList<DataType>::Add(DataType data)
{
	if (length >= capacity)
	{
		cout << "顺序表已满" << endl;
		return false;
	}
	p[length] = data;
	length++;
	return true;
}


/**
 * 操作:判断顺序表是否为空
 * 输入:
 * 输出:
 */
template<class DataType>
bool ArrayList<DataType>::IsEmpty()
{
	if (length == 0)
	{
		return true;
	}
	return false;
}


/**
 * 操作:按位删除
 * 输入: 待删除元素的位序
 * 输出:
 * 平均时间复杂度:O(n)
 */
template<class DataType>
bool ArrayList<DataType>::DeleteByIndex(int i)
{
	if (i < 1 || i < length)
	{
		cout << "参数错误" << endl;
		return false;
	}
	// 待删除位置后面的元素依次前移一位
	for (int j = i; j < length; j++)
	{
		p[j] = p[j + 1];
	}
	length--;
	return true;
}
  • 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、付费专栏及课程。

余额充值