学习数据结构day2-线性表及代码实现

线性表的定义

  • 线性表是具有相同特性的数据元素的一个有限序列。
  • 线性表中的数据呈线性关系。
  • 顺序表是线性表的线性存储结构。
  • 特性
    • 有穷性:线性表中的元素是有限的。
    • 一致性:线性表中的元素类型一致。
    • 序列性:线性表中的元素是有序的,是[[什么是数据结构#3.存储结构|线性关系]]。
  • 其在内存中的存储形式在这里插入图片描述

代码实现: 头文件"LineList.h"

#pragma once
#include<iostream>
#include<string>
using namespace std;
//宏定义区
#define MAXSIZE 100
//数据表类型
typedef int ElemType;

class LineList
{
private:
	//这是数据区,设为私有权限,仅开放成员函数能调用
	ElemType* data;		//数据,推荐将数组下标为零的地方设为空值。这样做便于某些函数的操作
	int len;		//长度
	int size;		//当前最大长度

public:
	//这是成员函数区,设为公共权限
	LineList();		//构造函数初始化线性表
	~LineList();
	//基本扩容函数
	bool getBiggerSpace();

	//基本函数
	int getLength();		//获得线性表长度
	bool getAllData();		//打印全部数据
	bool ListIsEmpty();		//判断线性表是否为空
	bool DestroyList();		//销毁线性表
	ElemType getData(int palce);		//获得线性表特定位置数据
	int searchData(int num);		//查找线性表中特定数值的元素并返回下标
	bool addData(int palce, ElemType value);		//在线性表中位置palce插入一个元素		//设计时可以使用函数的重载
	ElemType deleteData(int palce);		//删除线性表中位置palce的元素

	//额外函数
	int addData(ElemType, bool _switch = false);		//头插法和尾插法,尾插法开关时bool值,默认调用头插法,该方法和基本方法重载,返回值为添加元素位置的下标

	//算法
	bool deleteSomeoneData(int value, bool _switch = false);		//删除该表中数据大小为x的所以数据元素,其中有两种实现方式
	
};
  • 实现文件"LineLIst.cpp"
#include "LineList.h"

LineList::LineList() :len(0),size(MAXSIZE)		//构造函数
{
	this->data = new ElemType[MAXSIZE];
}

LineList::~LineList()		//析构函数
{
	delete[] this->data;
	this->data = NULL;
}

bool LineList::getBiggerSpace()			//基本扩容函数的实现
{
	ElemType* _data = new ElemType[size + MAXSIZE];
	for (int i = 0; i < this->len; i++)
	{
		_data[i] = this->data[i];
	}
	delete[] this->data;
	this->data = _data;
	return true;
}

int LineList::getLength()		//得到线性表长度的实现
{
	return this->len;
}

bool LineList::getAllData()		//得到全部数据的实现
{
	if (this->ListIsEmpty())
	{
		cout << "该线性表为空" << endl;
		return false;
	}
	for (int i = 0; i < len; i++)		//在此可尝试for(auto i: Type[] num)循环
	{
		cout << this->data[i] << " " << endl;
	}
	return true;
}

bool LineList::ListIsEmpty()		//判断线性表是否为空的实现
{
	if (this->len == 0)
		return true;
	return false;
}

bool LineList::DestroyList()		//销毁线性表的实现
{
	delete[] this->data;
	this->data = NULL;
	this->len = 0;
	return true;
}

ElemType LineList::getData(int palce)		//返回特定位置数据的实现
{
	if (palce > len + 1)
	{
		return (ElemType)-1;
	}
	return this->data[palce - 1];
}

int LineList::searchData(int num)		//查找数据的实现
{
	for (int i = 0; i < this->len; i++)		//在此可以优化一下,以减小时间复杂度.
	{
		if (num == this->data[i])
			return i;
	}
	return -1;
}

bool LineList::addData(int palce, ElemType value)		//在特定位置插入数据的实现
{
	if (this->len >= this->size)
		this->getBiggerSpace();
	if (palce < 1 || palce > this->size)
		return false;
	for (int i = len-1; i >= palce - 1; i--)
	{
		data[i + 1] = data[i];
	}
	data[palce - 1] = value;
	this->len++;
	return true;
}

ElemType LineList::deleteData(int palce)		//删除数据的实现
{
	if (this->ListIsEmpty())
		return (ElemType)-1;
	if (palce < 1 || palce > this->size)
		return false;
	ElemType value = this->data[palce - 1];
	for (int i = palce - 1; i < this->len - 1;)
	{
		this->data[i] = this->data[i + 1];
	}
	this->len--;
	return value;
}

int LineList::addData(ElemType value, bool _switch)		//头插法和尾插法的实现
{
	if (this->len >= this->size)
		this->getBiggerSpace();
	if (_switch == false)
	{
		this->data[len] = value;
		len++;
		return len - 1;
	}
	else
	{
		this->addData(1,value);
	}
}

bool LineList::deleteSomeoneData(int value, bool _switch)		//删除特定大小的全部元素的实现
{
	ElemType* _data = this->data;
	int num = 0;
	if (_switch == false)		//当开关关闭时,自动调用第一种算法
	{
		for (int i = 0; i < this->len; i++)
		{
			if (this->data[i] != value)
			{
				_data[num] = this->data[i];
				num++;
			}
		}
		this->len = num;
	}
	else
	{
		for (int i = 0; i < this->len; i++)
		{
			if (this->data[i] == value)
				num++;
			this->data[i] = this->data[i + num];
		}
		this->len -= num;
	}
	return true;
}
#include "LineList.h"

LineList::LineList() :len(0),size(MAXSIZE)		//构造函数
{
	this->data = new ElemType[MAXSIZE];
}

LineList::~LineList()		//析构函数
{
	delete[] this->data;
	this->data = NULL;
}

bool LineList::getBiggerSpace()			//基本扩容函数的实现
{
	ElemType* _data = new ElemType[size + MAXSIZE];
	for (int i = 0; i < this->len; i++)
	{
		_data[i] = this->data[i];
	}
	delete[] this->data;
	this->data = _data;
	return true;
}

int LineList::getLength()		//得到线性表长度的实现
{
	return this->len;
}

bool LineList::getAllData()		//得到全部数据的实现
{
	if (this->ListIsEmpty())
	{
		cout << "该线性表为空" << endl;
		return false;
	}
	for (int i = 0; i < len; i++)		//在此可尝试for(auto i: Type[] num)循环
	{
		cout << this->data[i] << " " << endl;
	}
	return true;
}

bool LineList::ListIsEmpty()		//判断线性表是否为空的实现
{
	if (this->len == 0)
		return true;
	return false;
}

bool LineList::DestroyList()		//销毁线性表的实现
{
	delete[] this->data;
	this->data = NULL;
	this->len = 0;
	return true;
}

ElemType LineList::getData(int palce)		//返回特定位置数据的实现
{
	if (palce > len + 1)
	{
		return (ElemType)-1;
	}
	return this->data[palce - 1];
}

int LineList::searchData(int num)		//查找数据的实现
{
	for (int i = 0; i < this->len; i++)		//在此可以优化一下,以减小时间复杂度.
	{
		if (num == this->data[i])
			return i;
	}
	return -1;
}

bool LineList::addData(int palce, ElemType value)		//在特定位置插入数据的实现
{
	if (this->len >= this->size)
		this->getBiggerSpace();
	if (palce < 1 || palce > this->size)
		return false;
	for (int i = len-1; i >= palce - 1; i--)
	{
		data[i + 1] = data[i];
	}
	data[palce - 1] = value;
	this->len++;
	return true;
}

ElemType LineList::deleteData(int palce)		//删除数据的实现
{
	if (this->ListIsEmpty())
		return (ElemType)-1;
	if (palce < 1 || palce > this->size)
		return false;
	ElemType value = this->data[palce - 1];
	for (int i = palce - 1; i < this->len - 1;)
	{
		this->data[i] = this->data[i + 1];
	}
	this->len--;
	return value;
}

int LineList::addData(ElemType value, bool _switch)		//头插法和尾插法的实现
{
	if (this->len >= this->size)
		this->getBiggerSpace();
	if (_switch == false)
	{
		this->data[len] = value;
		len++;
		return len - 1;
	}
	else
	{
		this->addData(1,value);
	}
}

bool LineList::deleteSomeoneData(int value, bool _switch)		//删除特定大小的全部元素的实现
{
	ElemType* _data = this->data;
	int num = 0;
	if (_switch == false)		//当开关关闭时,自动调用第一种算法
	{
		for (int i = 0; i < this->len; i++)
		{
			if (this->data[i] != value)
			{
				_data[num] = this->data[i];
				num++;
			}
		}
		this->len = num;
	}
	else
	{
		for (int i = 0; i < this->len; i++)
		{
			if (this->data[i] == value)
				num++;
			this->data[i] = this->data[i + num];
		}
		this->len -= num;
	}
	return true;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值