线性表实现--数组描述,vector描述,链表描述


加深了解线性表,跟书上敲了一遍,了解一下线性表的实现,异常类出错有点多,后期学底层时候,再回来好好看看

线性表实现-数组描述

//#pragma once
#ifndef _数组_
#define _数组_

#include<iostream>
using namespace std;
#include<algorithm>
#include<exception>//异常处理头文件
#include<string>
#include<sstream>//ostringstream的头文件


class illegalParameterValue//异常类
{
public:
	illegalParameterValue(string theMessage = "Illegal parameter value")
	{
		message = theMessage;
	}
	void outputMessage() { cout << message << endl; }
private:
	string message;
};


// illegal index
class illegalIndex
{
public:
	illegalIndex(string theMessage = "Illegal index")
	{
		message = theMessage;
	}
	void outputMessage() { cout << message << endl; }
private:
	string message;
};



template<class T>
class linearList
{
public:
	virtual ~linearList() {};
	virtual bool empty() const = 0;//表示纯虚函数,const不能修改其数据成员 // 空返回1
	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& theElemet) = 0;//把theELement插入到索引为theIndex的位置上
	virtual void output(ostream& out)const = 0;//把线性表插入输出流out
};


//定义派生类
template<class T>
class arrayList : public linearList<T>
{
	friend ostream& operator<<(ostream& out, const arrayList<T>& x);
public:
	arrayList(int initialCapacity = 10);//普通构造函数
	arrayList(const arrayList<T>&);//拷贝构造
	~arrayList() { delete[] element; }

	//ADT(抽象数据类型)方法
	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& theElemet);
	void output(ostream& out)const;

	//其他方法
	int capacity() { return arrayLength; }
	void changeLength1D(T*& a, int oldLength, int newLength);

protected:
	void checkIndex(int theIndex)const;//若索引无效,则抛出异常
	T* element;//存储线性表元素的一维数组
	int arrayLength;//一维数组的容量
	int listSize;//线性表的元素个数
};

//修改数组长度
template<class T>
void arrayList<T>::changeLength1D(T*& a, int oldLength, int newLength)
{
	if (newLength < 0)
		throw illegalParameterValue("newlength must be >= 0");

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


template<class T> //普通构造函数类外实现
arrayList<T>::arrayList(int initialCapacity)
{
	if (initialCapacity < 1)
	{
		ostringstream s;
		s << "Initial capacity =" << initialCapacity << "Must be > 0";
		throw illegalParameterValue(s.str()); //str()将ostringstream转换为string
	}

	arrayLength = initialCapacity;
	element = new T[arrayLength];
	listSize = 0;
}

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>//checkIndex函数类外实现
void arrayList<T>::checkIndex(int theIndex)const
{
	if (theIndex < 0 || theIndex >= listSize)
	{
		ostringstream s;
		s << "index = " << theIndex << "size = " << listSize;
		throw illegalIndex(s.str());
	}
}

template<class T>//get函数类外实现
T& arrayList<T>::get(int theIndex)const
{
	checkIndex(theIndex);
	return element[theIndex];
}

template<class T>//indexOf函数类外实现
int arrayList<T>::indexOf(const T& theElement)const
{
	int theIndex = (int)(find(element, element + listSize, theElement) - element);//find如果没找到会返回end,所以减去begin就是listsize

	if (theIndex == listSize) return -1;
	else return theIndex;
}

//template<class T>//erase函数类外实现
//void arrayList<T>::erase(int theIndex)
//{
//	checkIndex(theIndex);
//	copy(element + theIndex + 1, element + listSize, element + theIndex);
//	element[--listSize].~T();//调用析构函数
//}


template<class T>//insert函数类外实现
void arrayList<T>::insert(int theIndex, const T& theElement)
{
	checkIndex(theIndex);
	if (listSize == arrayLength)
	{
		changeLength1D(element, arrayLength, 2 * arrayLength);
		arrayLength *= 2;
	}

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

//打印线性表
template<class T>
void arrayList<T>::output(ostream &out)const
{
	copy(element, element + listSize, ostream_iterator<T>(out, " "));
}
template<class T>
ostream& operator<<(ostream& out, const arrayList<T>& x)
{
	x.output(out);
	return out;
}

//改进erase函数
template<class T>//erase函数类外实现
void arrayList<T>::erase(int theIndex)
{
	checkIndex(theIndex);
	copy(element + theIndex + 1, element +` listSize, element + theIndex);

	if (listSize < arrayLength / 4)
	{
		changeLength1D(element, arrayLength, arrayLength / 2);//减少释放数组空间
	}

	element[--listSize].~T();//调用析构函数
}

#endif // !_数组_


线性表实现-vector描述

#ifndef _vectorList_
#define _vectorList_

#include<iostream>
using namespace std;

#include<algorithm>
#include<exception>
#include<string>
#include<sstream>
#include<vector>

class illegalParameterValue
{
public:
	illegalParameterValue(string theMessage = "Illegal parameter value")
	{
		message = theMessage;
	}
	void outputMessage() { cout << message << endl; }
private:
	string message;
};
// illegal index
class illegalIndex
{
public:
	illegalIndex(string theMessage = "Illegal index")
	{
		message = theMessage;
	}
	void outputMessage() { cout << message << endl; }
private:
	string message;
};

template<class T>
class linearList
{
public:
	virtual ~linearList() {};
	virtual bool empty() const = 0;//表示纯虚函数,const不能修改其数据成员 // 空返回1
	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& theElemet) = 0;//把theELement插入到索引为theIndex的位置上
	virtual void output(ostream& out)const = 0;//把线性表插入输出流out
};

template<class T>
class vectorList : public linearList<T>
{
public:
	vectorList(int initialCapcity = 10);
	vectorList(const vectorList<T>&);
	~vectorList() { delete element; }

	//Adt
	bool empty() const { return element->empty(); }
	int size() const { return element->size(); }
	T& get(int theIndex) const;
	int indexOf(const T& theElement)const;
	void erase(int theIndex);
	void insert(int theIndex, const T& theElemet);
	void output(ostream& out)const;

	//其他方法
	int capacity() { return element->capacity(); }
	
	//线性表的起始和结束位置的迭代器
	typedef typename vector<T>::iterator iterator;
	iterator begin() { return element->begin(); }
	iterator end() { return element->end(); }

protected:
	void checkIndex(int theIndex)const;
	vector<T>* element;//存储线性表元素的向量
};

//数组的长度是动态增长的

//构造函数
template<class T>
vectorList<T>::vectorList(int initialCapacity)
{
	if (initialCapacity < 1) {
		ostringstream s;
		s << "Initial capacity =" << initialCapacity << "Must be > 0";
		throw illegalParameterValue(s.str()); //str()将ostringstream转换为string
	}
	element = new vector<T>;
	element->reserve(initialCapacity);//扩容到规定容量
}

template<class T>
vectorList<T>::vectorList(const vectorList<T>& theLIst)
{
	element = new vector<T>(*theLIst.element);//定义里面是指针
}

template<class T>//checkIndex函数类外实现
void vectorList<T>::checkIndex(int theIndex)const
{
	if (theIndex < 0 || theIndex >= size())
	{
		ostringstream s;
		s << "index = " << theIndex << "size = " << size();
		throw illegalIndex(s.str());
	}
}


template<class T>//get函数类外实现
T& vectorList<T>::get(int theIndex)const
{
	checkIndex(theIndex);
	return element[theIndex];
}

template<class T>//indexOf函数类外实现
int vectorList<T>::indexOf(const T& theElement)const
{
	int theIndex = (int)(find(element, element + size(), theElement) - element);//find如果没找到会返回end,所以减去begin就是listsize

	if (theIndex == size()) return -1;
	else return theIndex;
}

template<class T>//erase函数类外实现
void vectorList<T>::erase(int theIndex)
{
	checkIndex(theIndex);
	element->erase(begin() + theIndex);
}

template<class T>//insert函数类外实现
void vectorList<T>::insert(int theIndex, const T& theElement)
{
	checkIndex(theIndex);
	element->insert(element->begin() + theIndex, theElement);
}

//打印线性表
template<class T>
void vectorList<T>::output(ostream& out)const
{
	copy(element, element + size(), ostream_iterator<T>(out, " "));
}
template<class T>
ostream& operator<<(ostream& out, const vectorList<T>& x)
{
	x.output(out);
	return out;
}



#endif // !_vectorList_


线性表实现–链表

#ifndef _chain_
#define _chain_

#include<iostream>
using namespace std;

#include<algorithm>
#include<exception>
#include<sstream>


class illegalParameterValue//异常类
{
public:
	illegalParameterValue(string theMessage = "Illegal parameter value")
	{
		message = theMessage;
	}
	void outputMessage() { cout << message << endl; }
private:
	string message;
};


// illegal index
class illegalIndex
{
public:
	illegalIndex(string theMessage = "Illegal index")
	{
		message = theMessage;
	}
	void outputMessage() { cout << message << endl; }
private:
	string message;
};

//定义父类
template<class T>
class linearList
{
public:
	virtual ~linearList() {};
	virtual bool empty() const = 0;//表示纯虚函数,const不能修改其数据成员 // 空返回1
	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& theElemet) = 0;//把theELement插入到索引为theIndex的位置上
	virtual void output(ostream& out)const = 0;//把线性表插入输出流out
};

//定义节点
template<class T>
class chainNode
{
private:
	//数据成员
	T element;
	chainNode<T>* next;

public:
	//方法
	chainNode() {}
	chainNode(const T& element)
	{
		this->element = element;
	}
	chainNode(const T& element, chainNode<T>* next)
	{
		this->element = element;
		this->next = next;
	}
};

//定义结构
template<class T>
class chain :public linearList<T>
{
public:
	chain(int initialCapacity = 10);//普通构造函数
	chain(const chain<T>&);//拷贝构造
	~chain();//析构

	//ADT(抽象数据类型)方法
	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& theElemet);
	void output(ostream& out)const;

	//其他方法
	int capacity() { return arrayLength; }
	void changeLength1D(T*& a, int oldLength, int newLength);

protected:
	void checkIndex(int theIndex)const;//若索引无效,则抛出异常
	chainNode<T>* firstNode;//第一个节点指针
	int listSize;//线性表元素个数
};

template<class T>
chain<T>::chain(int initialCapacit)
{
	if (initialCapacity < 1)
	{
		ostringstream s;
		s << "Initial capacity =" << initialCapacity << "Must be > 0";
		throw illegalParameterValue(s.str()); //str()将ostringstream转换为string
	}
firstNode = NULL;
listSize = 0;
}

template<class T>
chain<T>::chain(const chain<T>& theList)//拷贝构造
{
	listSize = theList.listSize;

	if (listSize == 0) //链表为空
	{
		firstNode = NULL;
		return;
	}

	//不为空
	chainNode<T>* sourceNode = theList.fistNode;
	firstNode = new chainNode<T>(sourceNode->element);
	sourceNode = sourceNode->next;
	chainNode<T>* targetNode = firstNode;

	while (sourceNode != NULL)
	{
		targetNode->next = new chainNode<T>(sourceNode->element);
		targetNode = targetNode->next;
		sourceNode = sourceNode->next;
	}
	targetNode->next = NULL;
}

//析构函数
template<class T>
chain<T>::~chain()
{
	while (firstNode != NULL)
	{
		chainNode<T>* nextNode = firstNode->next;
		delete firstNode;
		firstNode = nextNode;
	}
}


template<class T>//checkIndex函数类外实现
void chain<T>::checkIndex(int theIndex)const
{
	if (theIndex < 0 || theIndex >= size())
	{
		ostringstream s;
		s << "index = " << theIndex << "size = " << size();
		throw illegalIndex(s.str());
	}
}

template<class T>
T& chain<T>::get(int theIndex)const
{
	checkIndex(theIndex);

	chainNode<T>* currentNode = firstNode;
	for (int i = 0; i < theIndex; i++)
	{
		currentNode = currentNode->next;
	}
	return currentNode->next;
}

template<class T>
int chain<T>::indexOf(const T& theElement)const
{
	chainNode<T>* currentNode = firstNode;
	int index = 0;//当前节点的索引
	while (currentNode != NULL && currentNode->element != theElement)
	{
		currentNode = currentNode->next;
		index++;
	}
	if (currentNode == NULL)
	{
		return -1;
	}
	else
		return index;
}

template<class T>
void chain<T>::erase(int theIndex)
{
	checkIndex(theIndex);

	chainNode<T>* deleteNode;
	if (theIndex == 0)
	{
		deleteNode = firstNode;
		firstNode = firstNode->next;
	}
	else
	{
		chainNode<T>* p = firstNode;
		for (int i = 0; i < theIndex - 1; i++)
		{
			p = p->next;
		}
		deleteNode = p->next;
		p->next = p->next->next;
	}
	listSize--;
	delete deleteNode;
}

template<class T>
void chain<T>::insert(int theIndex, const T& theElement)
{
	checkIndex(theIndex);
	
	if (theIndex == 0)
	{
		firstNode = new chainNode<T>(theElement, firstNode);//next  -> firstNode
	}
	else
	{
		chainNode<T>* p = firstNode;
		for (int i = 0; i < theIndex - 1; i++)
		{
			p = p->next;
		}
		p->next = new chainNode<T>(theElement, p->next);
	}
	listSize++;
}

template<class T>
void chain<T>::output(ostream& out)const
{
	for (chainNode<T>* currentNode = firstNode; currentNode != NULL; currentNode = currentNode->next)
	{
		cout << currentNode->element << " ";
	}
}
template<class T>
ostream& operator<<(ostream& out, const chain<T>& x)
{
	x.output(out);
	return out;
}


//迭代器
template<class T>
class iterator
{
public:
	iterator(chainNode<T>* theNode = NULL) {
		node = theNode;
	}

	T& operator*()const { return node->element; }
	T* operator->()const { return &node->element; }

	iterator& operator++()
	{
		node = node->next;
		return *this;
	}
	iterator operator++(int)
	{
		iterator old = *this;
		node = node->next;
		return *this;
	}

	bool operator !=(const iterator right)const
	{
		return node != right.node;
	}
	bool operator ==(const iterator right)const
	{
		return node == right.node;
	}


protected:
	chainNode<T>* node;
};


#endif


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值