1. 程式人生 > 其它 >【C++】arrayList類的實現

【C++】arrayList類的實現

技術標籤:# 資料結構和演算法分析

lineartList.h :

#pragma once
template<class T>
class lineartList
{
public:
	virtual ~lineartList() {};
	virtual bool empty() const = 0; 
	// 判斷線性表是否為空
	virtual int size() const = 0;
	// 返回線性表的長度
	virtual T& get(int theIndex) const = 0;
	//返回索引為theIndex的元素
	virtual int indexOf(
const T& theElement) const = 0; //返回元素theElement第一次出現時的索引 virtual void erase(int theIndex) = 0; //刪除索引theIndex的元素 virtual void insert(int theIndex, const T& theElement) = 0; //把theElement插入線性表中索引為theIndex的位置上 virtual void output()const= 0; //把線性表插入輸出流out };

arrayList.h :

#pragma once
#include"lineartList.h"
//不可省略 template<class T> class arrayList:public lineartList<T> { protected: void checkIndex(int theIndex) const; T* element; int arrayLength;//一維陣列的容量 int listSize;//線性表元素的個數 public: arrayList(int initialCapacity = 0); arrayList(const arrayList<T>&); ~arrayList() { delete[]element;
} bool empty()const { return listSize == 0; } int size()const { return listSize; } T& get(int theIndex) const; int indexOf(const T& theElement) const; void erase(int theIndex); void insert(int theIndex, const T& theElement); void output()const; int capacity()const { return arrayLength; } void changeLengthID(T *& a,int oldlength,int newlength)const; };

arrayList.cpp :

#include "arrayList.h"//不可省略
#include<iostream>
using namespace std;
template<class T>
void arrayList<T>::checkIndex(int theIndex) const
{
	if (theIndex < 0 || theIndex >= listSize ){
		cout << "index = " << theIndex << " size = " << listSize;
		exit(-1);
	}
}
template<class T>
inline arrayList<T>::arrayList(int initialCapacity)
{
	if (initialCapacity < 1) {
		cout << "Indital capacity = " << initialCapacity << " must be > 0";
		exit(-1);
	}
	else
	{
		arrayLength = initialCapacity;
		element = new T[arrayLength];
		listSize = 0;
	}
}

template<class T>
arrayList<T>::arrayList(const arrayList<T>& theList)
{
	arrayLength = theList.arrayLength;
	listSize = theList.listSize;
	copy(theList.element, theList.element + listSize, element);
}

template<class T>
T& arrayList<T>::get(int theIndex) const
{
	checkIndex(theIndex);
	return *(element + theIndex);
	// TODO: 在此處插入 return 語句
}

template<class T>
int arrayList<T>::indexOf(const T& theElement) const
{
	int theIndex = (int)(find(element, element + listSize, theElement) - element);
	if (theIndex == listSize) {
		return -1;
	}
	else {
		return theIndex;
	}
	return 0;
}

template<class T>
void arrayList<T>::erase(int theIndex)
{
	checkIndex(theIndex);
	copy(element + theIndex + 1, element + listSize, element + theIndex);
	element[--listSize].~T();
}

template<class T>
void arrayList<T>::insert(int theIndex, const T& theElement)
{
	if (theIndex < 0 || theIndex > listSize){
		cout << "index = " << theIndex << " size = " << listSize;
		exit(-1);
		}
	if (listSize == arrayLength) {
		changeLengthID(element, arrayLength, 2 * arrayLength);
		arrayLength *= 2;
	}
	copy(element + theIndex, element + listSize, element + theIndex + 1);
	*(element + theIndex) = theElement;
	listSize++;
}

template<class T>
void arrayList<T>::output() const
{
	for (int i = 0;i < listSize;i++) {
		cout << *(element + i) << " ";
	}
}

template<class T>
void arrayList<T>::changeLengthID(T*& a, int oldlength, int newlength) const
{
	if (newlength < 0) {
		cout << "newlength must be >= 0";
		exit(-1);
	}
	T* temp = new T[newlength];
	int number = min(oldlength, newlength);
	copy(a, a + number, temp);
	delete []a;
	a = temp;
}

Test.cpp :

#include<iostream>
using namespace std;
#include"arrayList.h"
#include"arrayList.cpp"//兩者皆不可省略
int main() {
	arrayList<int>  arr(6);
	if (arr.empty()) {
		cout << "陣列為空" << "\n";
	}
	else {
		cout<< "陣列不為空" << "\n";
	}
	arr.insert(0, 9);
	if (arr.empty()) {
		cout << "陣列為空" << "\n";
	}
	else {
		cout << "陣列不為空" << "\n";
	}
	arr.insert(1, 11);
	arr.insert(2, 13);
	arr.insert(3, 15);
	arr.insert(4, 17);
	cout << "size = "<<arr.size()<<"\n";
	cout<<"index of 2 is " <<arr.get(2)<<"\n";
	cout << "the index of the element 17 is " << arr.indexOf(17) << "\n";
	arr.output();
	cout << "\n";
	arr.erase(2);
	arr.output();

	return 0;
}

在這裡插入圖片描述
這次讓我記憶特別深刻啊!第一次使用模板類有一個問題一直無法解決,被困在這裡大半個小時。
在這裡插入圖片描述
模板類必須要求:

#include"arrayList.h"
#include"arrayList.cpp"//兩者皆不可省略

這兩個匯入都不可省略。

還有就是對const類也有了深刻印象,非const類可以呼叫非const函式和const函式,但是const類只能呼叫const函式