#include"stdafx.h"
#include<iostream>
#include<vector>
usingnamespacestd;
template<classT>
classListNode //节点类
{
protected:
typedefListNode<T>*pListNode;
typedefListNode<T>&rListNode;
public:
ListNode<T>*Next;
Tval;
public:
ListNode(constT&value,pListNodep=NULL):val(value),Next(p){};
ListNode():Next(NULL){};
~ListNode(){};
};
template<classT>
classQueue
{
public:
typedefListNode<T>*pNode;
typedefconstT*iterator;
private:
pNodepFront;
pNodepRear; //定义头和尾指针
public:
Queue():pFront(NULL),pRear(NULL){};
~Queue(){deleteAllNode();};
boolIsEmpty()const{return(pFront==NULL);};
boolIsFull()const{returnfalse;}
voiddeleteAllNode()
{
pNodep;
pNodepNext;
for(p=pFront;p!=NULL;p=pNext)
{
pNext= p->Next;
deletep;
}
pFront= pRear = NULL;
}
constT&getFront()const
{
if(pFront==NULL)
returnNULL;
returnpFront->val;
}
constT&getRear()
{
if(pRear==NULL)
returnNULL;
returnpRear->val;
}
voidpush_back(constT&val) //进行数据的压入
{
if(IsEmpty())
pFront= pRear= newListNode<T>(val);
else
pRear= pRear->Next= newListNode<T>(val);
}
voidpop_back() //进行数据退出
{
if(IsEmpty())
return;
ListNode<T>*q= pFront;
pFront= pFront->Next;
deleteq; //将数据产品从链表中删除
}
constQueue&operator=(constQueue&src);
};
template<classT>
constQueue<T>&Queue<T>::operator=(constQueue&src)
{
if(this==&src|| src.IsEmpty())
return*this;
deleteAllNode();//删除所有的节点
ListNode<T>*p= newListNode<T>(src.pFront->val);
ListNode<T>*pNext= src.pFront->Next; //指向头节点的下一个节点
pFront= p;
while(pNext!= NULL)
{
p->Next= newListNode<T>(pNext->val);
p= p->Next;
if(pNext->Next== NULL)pRear= p;
pNext= pNext->Next;
} //到此数据赋值完毕
return*this;
}
intmain()
{
Queue<int>iQue;
iQue.push_back(3);
iQue.push_back(4);
iQue.push_back(5);
iQue.push_back(6);
iQue.push_back(7);
iQue.push_back(8);
iQue.push_back(9);
intval= iQue.getRear();
cout<<"Rear:"<<val<<endl;
val= iQue.getFront();
cout<<"Front:"<<val<<endl;
getchar();
}
//泛型指针就没写了要重载一下++运算符可以指定一个pCur的当前节点来进行重载