數據結構學習: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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章