数据结构学习:C++实现线性表的顺序存储结构

提要:这是我新开的一个专栏(数据结构学习)

很多的数据结构的书籍都是以C为基础实现的,主要是由于C强大的指针功能,我本人看的也是C语言版本,但是我是用C++实现的。建议以后想要学习C++的小伙伴最好也能把数据结构的内容从头至尾自己都实现一遍,不管用什么语言,因为数据结构在以后找工作的时候是必问知识

这是本专栏的第一篇内容 介绍的是线性表的顺序存储结构以及C++实现方法,话不多说,下面就开始的线性表的学习:

首先是头文件部分:

/////////////////////////////////////////////
#pragma once

#include <iostream>

#define MAXSIZE 100

using namespace std;

template <class ElemType>

class CList
{
public:

	CList();
	virtual ~CList();                                   //构造和析构

	int getLen();                                       //获得线性表的空间长度
	int getSize();                                      //获得线性表的与元素个数
	bool insertElem(const int &i, const ElemType &e);   //在指定的位置插入元素
	void showValue();                                   //显示所有元素
	ElemType getElem(int i);                            //获取指定位置的元素
	void deletElem(int i);                              //删除指定位置处的元素
	void modify(int i,const ElemType &e);               //修改指定位置的元素


	ElemType *baseAddress;            //链表的基地址
	int m_length;                    //线性表分配的存储空间长度
	int m_size;                      //线性表的当前长度


};

其次是核心代码部分(函数的定义)

/////////////////////////////////////////
#include "list.h"

/***************************************
 *所有函数的定义
 ***************************************/

//构造函数

template <class ElemType>
CList<ElemType>::CList()
{
	m_size = 0;
	m_length = MAXSIZE;
	baseAddress = new ElemType[MAXSIZE];//动态开辟空间
}

//析构函数

template <class ElemType>
CList<ElemType>::~CList()
{
	delete[] baseAddress;               //删除动态数组
}

//获取线性表的目前存储空间,空间未增加时

template <class ElemType>
int CList<ElemType>::getLen()
{
	return m_length;
}

//获取线性表的实际元素数

template <class ElemType>
int CList<ElemType>::getSize()
{
	return m_size;
}

//在线性表l的第i个位置插入元素e

template <class ElemType>

bool CList<ElemType>::insertElem(const int &i, const ElemType &e){
	if (m_size == MAXSIZE) {
		cout << "线性表已满,不能插入元素" << endl;
		return false;
	}else if (i < 1 || i >= MAXSIZE) {
		cout << "请输入正确的插入位置" << endl;
		return false;
	}else {
		for (int index = m_size - 1; index >= i - 1; --index) {
			baseAddress[index + 1] = baseAddress[index];
		}
		baseAddress[i - 1] = e;
		m_size++;
		return true;
	}
}

//访问指定位置的元素

template<class ElemType>

ElemType CList<ElemType>::getElem(int i)
{
	if (i < 1 || i > m_size) {
		cout << "请输入正确的查找位置" << endl;
	}
	else {
		return baseAddress[i - 1];
	}
}

//删除指定位置的元素

template<class ElemType>
void CList<ElemType>::deletElem(int i)
{
	;
	if (!m_size) {
		cout << "线性表为空" << endl;
		return;
	}
	else if (i < 1 || i > m_size) {
		cout << "要删除的位置不在可行范围之内" << endl;
		return;
	}
	else {
		ElemType e = baseAddress[i - 1];
		for (int index = i; index != m_size; ++index) {
			baseAddress[index - 1] = baseAddress[index];
		}
		m_size--;
	}
}

//显示表中所有的元素

template<class ElemType>

void CList<ElemType>::showValue(){
	if (m_size == 0) {
		cout << "表中无元素" << endl;
		return;
	}else {
		for (int index = 0; index < m_size; ++index) {
			cout << baseAddress[index] << " ";
		}
		cout << endl;
	}
}

//修改指定位置的元素

template <class ElemType>

void CList<ElemType>::modify(int i, const ElemType &e) {
	if (i < 1 || i >= m_size) {
		cout << "请输入正确的位置" << endl;
		return;
	}
	baseAddress[i - 1] = e;
}

最后是测试部分(主函数)

#include "list.cpp"

using namespace std;

int main()
{
	CList<int> lst; //创建一个空表
	cout << "空表的中的元素个数" << lst.getSize() << endl;
	cout << "空表分配的内存空间" << lst.getLen() << endl;
	for (int i = 1,j=9; i < 10; ++i,--j) {
		lst.insertElem(i,j);
	}
	lst.showValue();
	cout << lst.getElem(1) << endl;
	lst.deletElem(1);
	cout << "作删除操作后的线性表" << endl;
	lst.showValue();
	lst.modify(1,9);
	lst.showValue();

	system("pause");
	return 0;
}
发布了33 篇原创文章 · 获赞 23 · 访问量 2289

猜你喜欢

转载自blog.csdn.net/weixin_42119041/article/details/101287425