C++使用數組的線性表

使用數組實現的線性表

本科讀書的時候學過數據結構這門課程,但是多年不用C++,想重新撿起來,建個分類用於做筆記,算是鞏固一下。

用途:

數組實現的線性表在工作中幾乎不使用,絕大多數時候都採用鏈表和散列表較多。
如:客戶端通知服務器的消息,適合採用鏈表形式,因爲消息存在順序性,所以從頭向後遍歷,消息採用尾插入。
調用服務端用戶的數據適合使用散列表,時間複雜度O(1)

arrayList類:

// arrayList.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <Windows.h>
#include <sstream>
#include "linearList.h"

using namespace std;

template<class T>
void changeLength(T* a, int oldLength, int newLength) {
	if (newLength < 0) {
		ostringstream s;
		s << "newLength must be large than 0";
		throw s.str();
	}

	T* temp = new T[newLength];
	int number = min(oldLength, newLength);
	copy(a, a + number, temp);
	delete[] a;
	a = temp;
}


template <class T>
class arrayList : public linearList<T>
{
public:
	arrayList(int capacity = 10);

	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; }

protected:
	void checkIndex(int theIndex) const;

	T* element;
	int arrayLength;
	int listSize;
};

template<class T>
arrayList<T>::arrayList(int capacity)
{
	if (capacity < 1) {
		ostringstream s;
		s << "newLength must be large than 0";
		throw s.str();
	}
	arrayLength = capacity;
	element = new T[capacity];
	element[0] = 0;
	listSize = 1;
}

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

template<class T>
T& arrayList<T>::get(int theIndex) const
{
	checkIndex(theIndex);
	return element[theIndex];
}

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;
}

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 << " - " << "Datasize = " << listSize + 1 << endl;
	}

	if (listSize == arrayLength) {
		changeLength(element, arrayLength, 2 * arrayLength);
		arrayLength *= 2;
	}

	copy_backward(element + theIndex, element + listSize, element + theIndex + 1);
	element[theIndex] = theElement;
	listSize++;
}

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

template<class T>
void arrayList<T>::checkIndex(int theIndex) const
{
	if (theIndex < 0 || theIndex >= listSize) 
	{
		ostringstream s;
		s << "index = " << theIndex << "size = " << listSize;
		cout << s.str() << endl;
	}
}

linearList抽象類:

#pragma once
template<class T>
class linearList
{
public:
	virtual ~linearList() {};

	virtual bool empty() const = 0;

	virtual int size() const = 0;

	virtual T& get(int theIndex) const = 0;

	virtual int indexOf(const T& theElement) const = 0;

	virtual void erase(int theIndex) = 0;
	
	virtual void insert(int theIndex, const T& theElement) = 0;

	virtual void output() const = 0;

private:

};

main函數:

int main()
{
	arrayList<int> * x = new arrayList<int>(10);

	int ele = 111;
	int index = 0;

	//insert
	x->insert(++index, ele);
	x->insert(++index, 222);
	x->insert(++index, 333);
	x->insert(++index, 444);
	x->insert(++index, 555);

	x->output();

	//del
	x->erase(3);
	x->output();

	//find
	cout << "------------" << endl;
	cout << x->indexOf(222) << endl;

	system("pause");
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章