最近在学习数据结构算法与应用-C++语言描述这本书,看着书写了一些代码。
LinearList.h:
#ifndef LINEARLIST_H_
#define LINEARLIST_H_
#include <iostream>
using namespace std;
template <class T>
class LinearList
{
public:
LinearList();//构造函数,无参数
LinearList(int nMaxListSize);//构造函数
LinearList(const LinearList<T>& llValue);//复制构造函数
~LinearList(){delete [] m_element;};//析构函数
bool IsEmpty() const {return m_nLength == 0;};//是否为空
int Length() const {return m_nLength;};//返回长度
bool Find(int nIndex, T& nValue) const;//查找第nIndex个元素,放到nValue中
int Search(T& nValue) const;//查找元素nValue,并返回其位置
LinearList<T>& Delete(int nIndex,T& nValue);//删除第nIndex元素,并将其值放入nvalue中
LinearList<T>& Insert(int nIndex,const T& nValue);//在nIndex后插入nValue
void OutPut(ostream& out) const;//输出
LinearList<T>& Reverse();//反序操作
LinearList<T>& Half();//减半操作
void Reset() { m_nCurrent = 1;};//重置当前位置为1
int Current() const { return m_nCurrent;};//返回当前位置
bool End() const;//当且仅当当前元素为表的最后一个元素时,返回true
bool Front() const;//当且仅当当前元素为表的第一个元素时,返回true
void Next();//移动current至表中的下一个元素,如果操作失败则引发一个异常
void PreVious();//移动current至表中的前一个元素,如果操作失败则引发一个异常
LinearList<T>& Alternate(const LinearList<T>& llValueA,const LinearList<T>& llValueB);//轮流合并
LinearList<T>& Merge(const LinearList<T>& llValueA,const LinearList<T>& llValueB);//有序合并
void Split(LinearList<T>& llValueA,LinearList<T>& llValueB);
void InsertSort();
protected:
private:
int m_nLength;//实际长度
int m_nMaxSize;//最大可允许长度
int m_nCurrent;//当前位置
T* m_element;//一维动态数组
};
#endif
LinearList.cpp:
#include "LinearList.h"
#include "xcept.h"
/************************************************************************
Function:构造函数,建造一个确定初始大小的线性表
Parameter:
Return:
************************************************************************/
template <class T>
LinearList<T>::LinearList(int nMaxListSize /* = 10 */)
{
m_nMaxSize = nMaxListSize;
m_element = new T[m_nMaxSize];
m_nLength = 0;
m_nCurrent = 1;
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
LinearList<T>::LinearList()
{
m_nMaxSize = 1;
m_element = new T[m_nMaxSize];
m_nLength = 0;
m_nCurrent = 1;
}
/************************************************************************
Function:复制构造函数
Parameter:
Return:
************************************************************************/
template <class T>
LinearList<T>::LinearList(const LinearList<T>& llValue)
{
int nIndex;
m_nLength = llValue.m_nLength;
m_nMaxSize = llValue.m_nMaxSize;
m_element = new T[m_nMaxSize];
for (nIndex = 0;nIndex < m_nLength; nIndex++) {
llValue.Find(nIndex+1,m_element[nIndex]);
}
m_nCurrent = 1;
}
/************************************************************************
Function:查找第nIndex个元素,放到nValue中
Parameter:
Return:
************************************************************************/
template <class T>
bool LinearList<T>::Find(int nIndex, T& nValue) const
{
if (nIndex<1||nIndex>m_nLength) {
return false;//不存在
}
nValue = m_element[nIndex-1];
return true;
}
/************************************************************************
Function:查找元素nValue,并返回其位置
Parameter:
Return:
************************************************************************/
template <class T>
int LinearList<T>::Search(T& nValue) const
{
int nIndex;
for (nIndex = 0;nIndex < m_nLength; nIndex++) {
if (m_element[nIndex]==nValue) {
return ++nIndex;
}
}
return 0;
}
/************************************************************************
Function:删除第nIndex元素,并将其值放入nvalue中
Parameter:
Return:
************************************************************************/
template <class T>
LinearList<T>& LinearList<T>::Delete(int nIndex,T& nValue)
{
int i;
if (Find(nIndex,nValue)) {
for (i = nIndex;i < m_nLength; i++) {
m_element[i-1] = m_element[i];
}
m_nLength--;
if (m_nLength<m_nMaxSize/4) {//动态减小数组大小
T* element;
m_nMaxSize /=2;
element = new T[m_nMaxSize];
for (i = 0;i < m_nLength; i++) {
element[i] = m_element[i];
}
delete m_element;
m_element = element;
}
return *this;
}else{
throw OutOfBounds();
}
}
/************************************************************************
Function:在nIndex后插入nValue
Parameter:
Return:
************************************************************************/
template <class T>
LinearList<T>& LinearList<T>::Insert(int nIndex,const T& nValue)
{
int i;
T* element;
if (nIndex < 0||m_nLength < nIndex) {
throw OutOfBounds();
}
if (m_nLength == m_nMaxSize) {//动态增加数组大小
m_nMaxSize *=2;
element = new T[m_nMaxSize];
for (i = 0;i < m_nLength; i++) {
element[i] = m_element[i];
}
delete m_element;
m_element = element;
}
for (i = m_nLength-1; i >= nIndex; i--) {
m_element[i+1] = m_element[i];
}
m_element[nIndex] = nValue;
m_nLength++;
return *this;
}
/************************************************************************
Function:输出
Parameter:
Return:
************************************************************************/
template <class T>
void LinearList<T>::OutPut(ostream& out) const
{
int nIndex;
for (nIndex = 0;nIndex < m_nLength;nIndex++) {
out << m_element[nIndex] << " ";
}
}
/************************************************************************
Function:反序操作
Parameter:
Return:
************************************************************************/
template <class T>
LinearList<T>& LinearList<T>::Reverse()
{
int nIndex;
T temp;
for (nIndex = 0;nIndex < m_nLength/2; nIndex++) {
temp = m_element[nIndex];
m_element[nIndex] = m_element[m_nLength-1-nIndex];
m_element[m_nLength-1-nIndex] = temp;
}
return *this;
}
/************************************************************************
Function:减半操作
Parameter:
Return:
************************************************************************/
template <class T>
LinearList<T>& LinearList<T>::Half()
{
int nIndex,nLength;
if (m_nLength==0) {//空表
return *this;
}
if (m_nLength%2==0) {
nLength = m_nLength/2;
}else{
nLength = m_nLength/2+1;
}
for (nIndex = 0;nIndex < nLength;nIndex++) {
m_element[nIndex] = m_element[2*nIndex];
}
m_nLength = nLength;
return *this;
}
/************************************************************************
Function:是否结尾
Parameter:
Return:
************************************************************************/
template <class T>
bool LinearList<T>::End() const
{
if (m_nCurrent == m_nLength) {
return true;
}else{
return false;
}
}
/************************************************************************
Function:是否表头
Parameter:
Return:
************************************************************************/
template <class T>
bool LinearList<T>::Front() const
{
if (m_nCurrent == 1) {
return true;
}else{
return false;
}
}
/************************************************************************
Function:当前位置后移
Parameter:
Return:
************************************************************************/
template <class T>
void LinearList<T>::Next()
{
if (m_nCurrent == m_nLength) {
throw OutOfBounds();
}else{
m_nCurrent++;
}
}
/************************************************************************
Function:当前位置前移
Parameter:
Return:
************************************************************************/
template <class T>
void LinearList<T>::PreVious()
{
if (m_nCurrent == 1) {
throw OutOfBounds();
}else{
m_nCurrent--;
}
}
/************************************************************************
Function:轮流合并
Parameter:
Return:
************************************************************************/
template <class T>
LinearList<T>& LinearList<T>::Alternate(const LinearList<T>& llValueA,const LinearList<T>& llValueB)
{
int nIndex;
m_nLength = llValueA.Length()+llValueB.Length();
int nMin = min(llValueA.Length(),llValueB.Length());
m_nMaxSize = m_nLength;
delete m_element;
m_element = 0;
m_element = new T[m_nMaxSize];
for (nIndex = 0;nIndex < nMin;nIndex++) {
llValueA.Find(nIndex+1,m_element[2*nIndex]);
llValueB.Find(nIndex+1,m_element[2*nIndex+1]);
}
if (llValueA.Length() < llValueB.Length()) {
for (nIndex = nMin; nIndex < llValueB.Length();nIndex++) {
llValueB.Find(nIndex+1,m_element[nIndex+nMin]);
}
}else{
for (nIndex = nMin; nIndex < llValueA.Length();nIndex++) {
llValueA.Find(nIndex+1,m_element[nIndex+nMin]);
}
}
return *this;
}
/************************************************************************
Function:有序合并
Parameter:
Return:
************************************************************************/
template <class T>
LinearList<T>& LinearList<T>::Merge(const LinearList<T>& llValueA,const LinearList<T>& llValueB)
{
int nIndex;
int nIndexA = 0;
int nIndexB = 0;
T tempA,tempB;
m_nLength = llValueA.Length()+llValueB.Length();
m_nMaxSize = m_nLength;
delete m_element;
m_element =0;
m_element = new T[m_nMaxSize];
llValueA.Find(nIndexA+1,tempA);
llValueB.Find(nIndexB+1,tempB);
for (nIndex = 0;nIndex < m_nLength;nIndex++) {
if (tempA < tempB) {
m_element[nIndex] = tempA;
nIndexA++;
if (nIndexA < llValueA.Length()) {
llValueA.Find(nIndexA+1,tempA);
}else{
llValueB.Find(llValueB.Length(),tempA);
}
}else{
m_element[nIndex] = tempB;
nIndexB++;
if (nIndexB < llValueB.Length()) {
llValueB.Find(nIndexB+1,tempB);
}else{
llValueA.Find(llValueA.Length(),tempB);
}
}
}
return *this;
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
void LinearList<T>::Split(LinearList<T>& llValueA,LinearList<T>& llValueB)
{
int nIndex;
for (nIndex = 0;nIndex < m_nLength;nIndex++) {
if (nIndex%2==0) {
llValueA.Insert(0,m_element[nIndex]);
}else{
llValueB.Insert(0,m_element[nIndex]);
}
}
}
/************************************************************************
Function:插入排序
Parameter:
Return:
************************************************************************/
template <class T>
void LinearList<T>::InsertSort()
{
if (m_nLength<=1) {
return;
}
int i,j;
T temp;
for (i = 1;i < m_nLength; i++) {
temp = m_element[i];
for (j = i-1; j>=0&&m_element[j]>temp;j--) {
m_element[j+1] = m_element[j];
}
m_element[j+1] = temp;
}
}
/************************************************************************
Function:重载操作符<<
Parameter:
Return:
************************************************************************/
template <class T>
ostream& operator<<(ostream& out,const LinearList<T>& llValue)
{
llValue.OutPut(out);
return out;
}