模版(手打)蒟蒻易吸收
线性表
数组描述
#include<bits/stdc++.h>
using namespace std;
template<class T>
class illegalParameterValue
{
public:
illegalParameterValue():
message("Illegal parameter value") {}
illegalParameterValue(char* theMessage) {message=theMessage;}
void outputMessage() {cout<<message<<endl;}
private:
string message;
};
template<class T>
class linearList
{
public:
virtual ~linearList() {};
virtual bool empty() const=0;//返回true,仅当线性表为空
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(ostream& out) const=0;//输出线性表
};
template<class T>
void changeLength1D(T*& a,int oldLength,int newLength)
{
if(newLength<0)
{cout<<"new length must be >=0\n"; return;}
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 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& theElement);
void output(ostream& out) const;
//其他
int capacity() const {return arrayLength;}
protected:
void checkIndex(int theIndex) const;//若索引theIndex无效,则抛出异常
T* element;//一维数组
int arrayLength;//数组容量
int listSize;
};
template<class T>
arrayList<T>::arrayList(int initialCapacity)
{//构造
if(initialCapacity<1){
ostringstream s;
s<<"Initial capacity="<<initialCapacity<<"must be>0";
cout<<s.str(); return;
}
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>
void arrayList<T>::checkIndex(int theIndex) const
{//确定索引在0和listSize-1 之间
if(theIndex<0 || theIndex>=listSize){
ostringstream s;
s<<"index ="<<theIndex<<"size ="<<listSize;
cout<<s.str();
}
}
template<class T>
T& arrayList<T>::get(int theIndex) const
{//返回索引值为theIndex的元素,若不存在,则抛出异常
checkIndex(theIndex);
return element[theIndex];
}
template<class T>
int arrayList<T>::indexOf(const T& theElement) const
{//返回元素theElement第一次出现时的索引,若不存在,则返回-1
//查找元素theElement
int theIndex=(int)(find(element,element+listSize,theElement)-element);
//确定元素theElement是否找到
if(theIndex==listSize)
//没有找到
return -1;
else return theIndex;
}
template<class T>
void arrayList<T>::erase(int theIndex)
{//删除索引为theIndex的元素,若不存在,抛出异常
checkIndex(theIndex);
//有效索引,移动其所引大于theIndex的元素
copy(element+theIndex+1,element+listSize,element+theIndex);
element[--listSize].~T(); //调用析构函数
}
template<class T>
void arrayList<T>::insert(int theIndex,const T& theElement)
{//在索引theIndex处插入元素theElement
if(theIndex<0 || theIndex>listSize)
{//无效索引
ostringstream s;
s<<"index ="<<theIndex<<"size ="<<listSize;
cout<<s.str(); return;
}
//有效索引,确定数组是否已满
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>(cout," "));
}
//重载 <<
template<class T>
ostream& operator<<(ostream out,const arrayList<T>& x)
{x.output(out);return out;}
arrayList<int> a;
int main()
{
return 0;
}
说明:
这个模版为一个不定长数组,提供以下操作
- 构造一个不定长数组,其中操作有:
- 用
arrayList<变量类型> 名称来定义一个不定长数组。以下将用arrayList<int> a来作为示范 - 可以用
arrayList<int> a(100)来定义一个长度为100 的不定长数组。数字可以更改。 - 可以用
arrayList<int> a(b)来定义一个不定长数组,且有不定长数组b 的初始值。
- 用
- empty 函数,使一个不定长数组清空。具体示例:
a.empty()。如果清空不成功则返回 false - size 函数,获取一个不定长数组中的元素个数。(注意不是数组长度)示例:
int l=a.size() - get 函数,返回索引值为 theIndex 的元素。如果不存在,抛出异常。示例:
int *c=a.get(2) - indexOf 函数,返回第一个等于 theElement 的元素。如果不存在,返回
-1 。示例:int d=a.indexOf(2) - erase 函数,删除索引为 theIndex 的元素,若不存在,抛出异常 。示例:
a.erase(2) - insert 函数,在索引为 theIndex 处插入元素 theElement。若索引无效,则抛出异常。示例:
a.insert(2,3) - output 函数,把线性表插入输出流。(注意不是直接输出)。示例:
a.output(cout)
无注释版
链式描述
#include<bits/stdc++.h>
using namespace std;
template<class T>
struct chainNode
{//数据成员
T element;
chainNode<T> *next;
//方法
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 linearList
{
public:
virtual ~linearList() {};
virtual bool empty() const=0;//返回true,仅当线性表为空
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(ostream& out) const=0;//输出线性表
};
template<class T>
class chain : public linearList<T>
{
public:
//构造+复制构造+析构
chain(int initialCapacity=10);
chain(const chain<T>& theList);
~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& theElement);
void output(ostream& out) const;
protected:
void checkIndex(int theIndex) const;//如果索引无效,则抛出异常
chainNode<T> *firstNode;//第一个节点
int listSize;//元素个数
};
template<class T>
void chain<T>::checkIndex(int theIndex) const
{//确定索引在0和listSize-1 之间
if(theIndex<0 || theIndex>=listSize){
ostringstream s;
s<<"index ="<<theIndex<<"size ="<<listSize;
cout<<s.str();
}
}
template<class T>
chain<T>::chain(int initialCapacity)
{//构造函数
if(initialCapacity<1)
{ostringstream s;
s<<"Initial capacity="<<initialCapacity<<"must be > 0";
cout<<s.str(); return;
}
firstNode=NULL;
listSize=0;
}
template<class T>
chain<T>::chain(const chain<T>& theList)
{//复制构造函数
listSize=theList.listSize;
if(listSize==0)
{//链表theList为空
firstNode=NULL;
return;
}
//链表theList为非空
chainNode<T>* sourceNode=theList.firstNode;//要复制链表theList的节点
firstNode=new chainNode<T>(sourceNode->element);//复制theList的首元素
sourceNode=sourceNode->next;
chainNode<T>* targetNode=firstNode;//当前链表*this的最后一个节点
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>
T& chain<T>::get(int theIndex) const
{//返回索引为theIndex的元素,若不存在,抛出异常
checkIndex(theIndex);
chainNode<T>* currentNode=firstNode;
for(int i=0;i<theIndex;i++)
currentNode=currentNode->next;
return currentNode->element;
}
template<class T>
int chain<T>::indexOf(const T& theElement) const
{//返回元素theElement第一次出现时的索引,如不存在,返回-1
//搜索链表寻找元素theElement
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)
{//删除索引为theIndex的元素,如不存在,抛出异常
checkIndex(theIndex);
//索引有效,需找要删除的元素节点
chainNode<T>* deleteNode;
if(theIndex==0)
{//删除链表的首节点
deleteNode=firstNode;
firstNode=firstNode->next;
}
else
{//用指针p指向要删除的节点
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)
{//在索引为theIndex位置上插入元素theElement
if(theIndex<0 || theIndex>listSize)
{//无效索引
ostringstream s;
s<<"Index = "<<theIndex<<"size = "<<listSize;
cout<<s.str(); return;
}
if(theIndex==0)
//在链表头插入
firstNode=new chainNode<T>(theElement,firstNode);
else
{//寻找新元素的前驱
chainNode<T>* p=firstNode;
for(int i=0;i<=theIndex-1;i++)
p=p->next;
//在p之后插入
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)
out<<currentNode->element<<" ";
}
//重载 <<
template<class T>
ostream& operator<<(ostream& out,const chain<T>& x)
{x.output(out);return out;}
chain<int> a;
int main()
{
return 0;
}
说明:
功能大致与arrayList(线性表——数组描述)相同。
经性能测验,比arrayList要慢一些。
矩阵
#include<bits/stdc++.h>
using namespace std;
template<class T>
class matrix
{
friend ostream& operator<<(ostream&,const matrix<T>&);
public:
matrix(int theRows=0,int theColumns=0);
matrix(const matrix<T>&);
~matrix(){delete [] element;}
int rows() const {return theRows;}
int columns() const {return theColumns;}
T& operator()(int i,int j) const;
matrix<T>& operator=(const matrix<T>&);
matrix<T> operator+() const;
matrix<T> operator+(const matrix<T>&) const;
matrix<T> operator-() const;
matrix<T> operator-(const matrix<T>&) const;
matrix<T> operator*(const matrix<T>&) const;
matrix<T>& operator+=(const T&);
private:
int theRows,theColumns;//行数和列数
T* element;
};
template<class T>
matrix<T>::matrix(int theRows,int theColumns)
{//矩阵构造函数
//检测行数和列数的有效性
if(theRows<0 || theColumns<0)
{cout<<"Rows and columns must be > 0\n"; return;}
if((theRows==0 || theColumns==0) && (theRows!=0 || theColumns!=0))
{cout<<"Either both or neither rows and columns should be zero"; return;}
//创建矩阵
this->theRows=theRows;
this->theColumns=theColumns;
element=new T [theRows*theColumns];
}
template<class T>
matrix<T>::matrix(const matrix<T>& m)
{//矩阵的复制构造函数
//创建矩阵
theRows=m.theRows;
theColumns=m.theColumns;
element=new T [theRows*theColumns];
//复制每一个元素
copy(m.element,m.element+theRows*theColumns,element);
}
template<class T>
matrix<T>& matrix<T>::operator=(const matrix<T>& m)
{//赋值
if(this!=&m)
{//不能自己复制自己
delete [] element;
theRows=m.theRows;
theColumns=m.theColumns;
element=new T [theRows*theColumns];
//复制每一个元素
copy(m.element,m.element+theRows*theColumns,element);
}
return *this;
}
template<class T>
T& matrix<T>::operator()(int i,int j) const
{//使a(i,j)的引用有效
if(i<1 || i>theRows || j<1 || j>theColumns)
{cout<<"matrix index out of bounds"; return;}
return element[(i-1)*theColumns+j-1];
}
template<class T>
matrix<T> matrix<T>::operator+(const matrix<T>& m) const
{//矩阵加法
if(theRows!=m.theRows || theColumns!=m.theColumns)
{cout<<"matrix size mismatch"; return;}
//生成结果矩阵
matrix<T> w(theRows,theColumns);
for(int i=0;i<theRows*theColumns;i++)
w.element=element[i]+m.element[i];
return w;
}
template<class T>
matrix<T> matrix<T>::operator*(const matrix<T>& m) const
{//矩阵乘法
if(theColumns!=m.theRows)
{cout<<"matrix size mismatch"; return;}
matrix<T> w(theRows,m.theColumns);
//定义矩阵*this,m和w的游标且初始化为(1,1)元素定位
int ct=0,cm=0,cw=0;
//对所有i和j计算w(i,j)
for(int i=1;i<=theRows;i++)
{//计算结果矩阵的第i行
for(int j=1;j<=m.theColumns;j++)
{//计算w(i,j)第一项
T sum=element[ct]*m.element[cm];
//累加其余所有项
for(int k=2;k<=theColumns;k++)
{
ct++;
cm+=m.theColumns;
sum+=element[ct]*m.element[cm];
}
w.element[cw++]=sum;
//从行的起点和下一列重新开始
ct-=theColumns-1;
cm=j;
}
//从下一行和下一列重新开始
ct+=theColumns;
cm=0;
}
return w;
}
matrix<int> a;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
return 0;
}
说明:
- 同样提供构造·复制构造·析构函数。用
matrix<变量类型> 矩阵名称(矩阵长度,矩阵宽度)来定义。示例:matrix<int> a(100,100)。以后均采用a 矩阵和同样大小的b 矩阵来说明。 - 重载
()运算符,使得可以直接用矩阵名称(行数,列数)来访问。如果越界,则抛出异常。示例:a(35,65) - 重载
=运算符,使矩阵可以直接赋值。示例:a=b - 重载
+运算符,使两个矩阵可以直接相加。(大小不一样则抛出异常)示例:a+b - 重载
-运算符,使两个矩阵可以直接相减。(大小不一样则抛出异常)示例:a-b - 重载
*运算符,实现矩阵乘法(详见本题)。示例:a*b栈
数组描述
链表描述
队列
数组描述
链表描述
跳表
散列
应用——文本压缩
树
二叉树
数组描述
链表描述
优先队列
左高树
竞赛树
搜索树
图
无权图
加权图