1. 程式人生 > >一、(1)C++ 實現簡單的線性表(順序儲存結構)

一、(1)C++ 實現簡單的線性表(順序儲存結構)

作為新手,初學C++和資料結構,也想發發博文,分享點自己的學習所得,也請諸位看官幫忙指正,能提提意見是極好的了!

首先呢,我給各位看官講個笑話:一個統計學家在調查了大量的資料後發現,兩個互不串通的人同時帶炸彈上飛機的概率幾乎是零,於是他每次坐飛機都會隨身攜帶一個炸彈。哈哈,程式設計師的你,今天笑了嗎?

我的第一篇博文就分享下簡單的用C++實現的順序儲存結構的線性表吧!以下是鄙人自己寫的簡單程式碼:

#include <iostream>
using namespace std;

const int maxSize = 100;//定義線性表最大長度為100

template<typename T> class Sqlist
{
public:
	void InitList();//初始化一個空表
	bool ListEmpty();//判斷線性表是否為空
	void ClearList();//將線性表清空
	bool GetElem(int location, T &element);//返回線性表中第i個值
	int LocateElem(T element);//線性表中查詢與e相等的元素,返回第一個與e相等元素線上性表中的序號,否則,返回0
	bool ListInsert(int location, T element);//線上性表中的第i個位置插入新元素
	bool ListDelete(int location, T &element);//刪除線性表中第i個位置的元素,並返回其值
	int ListLength();//返回線性表的長度
private:
	T *data;//線性表中的資料
	int length;//線性表的當前長度
};

//開始實現各成員函式
template<typename T> void Sqlist<T>::InitList()
{
	data = new T[maxSize];//初始化一個空表
	length = 0;
}

template<typename T> bool Sqlist<T>::ListEmpty()
{
	if(length == 0)
		return true;
	return false;
}

template<typename T> void Sqlist<T>::ClearList()
{
	data = NULL;
	length = 0;
}

template<typename T> bool Sqlist<T>::GetElem(int location, T &element)
{
	if (location > length)
	{
		return false;//越界了
	}
	element = *(data + location - 1);
	return true;
}

template<typename T> int Sqlist<T>::LocateElem(T element)
{
	for (int i = 0; i < length; i++)
	{
		if(*(data+i) == element)
			return i+1;
	}
	return 0;
}

template<typename T> bool Sqlist<T>::ListInsert(int location, T element)
{
	if (location > (length + 1))
		return false;
	for (int i = length; i >= location; i--)
	{
		*(data + i) = *(data + i - 1);
	}
	*(data+location-1) = element;
	length+=1;
	return true;
}

template<typename T> bool Sqlist<T>::ListDelete(int location, T &element)
{
	if (location > length)
	{
		return false;
	}
	element = *(data + location - 1);
	for (int i = location - 1; i < (length - 1); i++)
	{
		*(data + i) = *(data + i + 1);
	}
	length -= 1;
	return true;
}

template<typename T> int Sqlist<T>::ListLength()
{
	return length;
}

int main()
{
	//例項化一個類
	Sqlist<int> mySql;
	//生成空表
	mySql.InitList();
	//往表中插入資料
	mySql.ListInsert(1,5);
	mySql.ListInsert(2,8);
	mySql.ListInsert(3,2);
	mySql.ListInsert(4,6);
	mySql.ListInsert(5,15);
	mySql.ListInsert(2,9);
	//輸出表的長度和表中資料
	cout<<"插值後,表的當前長度為:"<<mySql.ListLength()<<endl;
	for (int i = 0; i < mySql.ListLength(); i++)
	{
		int element(0);
		mySql.GetElem(i+1, element);
		cout<<"表中第"<<(i+1)<<"個元素為"<<element<<endl;
	}
	//刪除表中某個資料
	int deleteLocation(0);
	cout<<"請輸入要刪除的元素的位置:";
	cin>>deleteLocation;
	int deleteElement(0);
	mySql.ListDelete(deleteLocation, deleteElement);
	cout<<"刪除了表中第"<<deleteLocation<<"個元素,該元素為:"<<deleteElement<<endl;
	//輸出刪除後表中的資料
	for (int i = 0; i < mySql.ListLength(); i++)
	{
		int element(0);
		mySql.GetElem(i+1, element);
		cout<<"刪除後的表中第"<<(i+1)<<"個元素為"<<element<<endl;
	}
	//查詢表中資料
	int searchElement(0);
	cout<<"請輸入要查詢的元素的值:";
	cin>>searchElement;
	int searchLocation = mySql.LocateElem(searchElement);
	if (!searchLocation)
	{
		cout<<"當前表中無此元素"<<endl;
	}
	else
		cout<<"所要查詢的元素位於表中第"<<searchLocation<<"個位置"<<endl;
	//清空表
	mySql.ClearList();
	//判斷表是否為空
	if (mySql.ListEmpty())
	{
		cout<<"當前表為空表!"<<endl;
	}
	else
		cout<<"當前表非空!"<<endl;
}
 

以後我會繼續貼上其他儲存結構的實現程式碼,請各位多多指教!