链队列基本操作的C++实现
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include <iostream>
using namespace std;
template <class ElemType>
class QueueLink
{
protected:
Node<ElemType> *front,*rear;
void Init();
public:
LinkQueue(); //无参数的构造函数
virtual ~LinkQueue();
int Length() const; //求队列长度
bool QueueEmpty() const;
void Clear();
void Traverse(void(*Visit)(ElemType &)); //遍历队列
StatusCode OutQueue(ElemType &e); //出队操作
if(rear == tmpPtr)
rear == front;
delete tmpPtr;
return SUCCESS;
}
else
return UNDER_FLOW;
}
template <class ElemType>
StatusCode QueueLink<ElemType>::GetHead(ElemType &e) const
}
}
template <class ElemType>
StatusCode QueueLink<ElemType>::OutQueue(ElemType &e)
{
if(!Empty())
{
Node<ElemType> *tmpPtr = front->next;
e = tmpPtr->data;
front ->next = tmpPtr ->next;
{
Init();
}
template <class ElemType>
QueueLink<ElemType>::~LinkQueue()
{
Clear();
}
template <class ElemType>
int QueueLink<ElemType>::Length()const
{
int count = 0;
{
return rear == front;
}
template <class ElemType>
void QueueLink<ElemType>::Clear()
{
ElemType tmpElem;
while(Lengh() > 0)
{
OutQueue(tmpElem);
}
}
template <class ElemType>
StatusCode GetHead(ElemType &e) const; //取队头操作
StatusCode EnQueue(const ElemType &e); //入队操作
LinkQueue(const LinkQueue<ElemType> ©); //复制构造函数
LinkQueue<ElemType>&operator = (const LinkQueue<ElemType> ©); //赋值运算符重载
void QueueLink<ElemType>::Traverse(void(*Visit)(ElemType &))
{
for(Node<ElemType> *tmpPtr = front->next;tmpPtr != NULL;tmpPtr = tmpPtr->next)
{
(*Visit)(tmpPtr->data);
{
if(© != this)
{
Clear();
for(Node<ElemType> *tmpPtr = copy.front ->next;tmpPtr != NULL;tmpPtr = tmpPtr->next;)
{
InQueue(tmpPtr->data);
}
}
return *this;
{
if(!Empty())
{
Node<ElemType>* tmpPtr = front->next;
e = tmpPtr->data;
return SUCCESS;
}
else
return UNDER_FLOW;
}
template <class ElemType>
StatusCode QueueLink<ElemType>::EnQueue(const ElemType &e)
}
template <class ElemType>
void Queue百度文库ink<ElemType>::Init()
{
rear = front = new Node<ElemType>;
}
template <class ElemType>
QueueLink<ElemType>::LinkQueue()
for(Node<ElemType> *tmpPtr = front->next;tmpPtr != NULL;tmpPtr = tmpPtr->next)
count++;
return count;
}
template <class ElemType>
bool QueueLink<ElemType>::QueueEmpty() const
}
{
Init();
for(Node<ElemType> *tmpPtr = copy.front ->next;tmpPtr != NULL;tmpPtr = tmpPtr->next;)
InQueue(tmpPtr->data);
}
template <class ElemType>
QueueLink<ElemType>& QueueLink<ElemType>::operator=(const QueueLink<ElemType> ©)
{
Node<ElemType> *tmpPtr = new Node<ElemType>(e);
rear->next = tmpPtr;
rear = tmpPtr;
return SUCCESS;
}
template <class ElemType>
QueueLink<ElemType>& QueueLink<ElemType>::LinkQueue(const QueueLink<ElemType> ©)
using namespace std;
template <class ElemType>
class QueueLink
{
protected:
Node<ElemType> *front,*rear;
void Init();
public:
LinkQueue(); //无参数的构造函数
virtual ~LinkQueue();
int Length() const; //求队列长度
bool QueueEmpty() const;
void Clear();
void Traverse(void(*Visit)(ElemType &)); //遍历队列
StatusCode OutQueue(ElemType &e); //出队操作
if(rear == tmpPtr)
rear == front;
delete tmpPtr;
return SUCCESS;
}
else
return UNDER_FLOW;
}
template <class ElemType>
StatusCode QueueLink<ElemType>::GetHead(ElemType &e) const
}
}
template <class ElemType>
StatusCode QueueLink<ElemType>::OutQueue(ElemType &e)
{
if(!Empty())
{
Node<ElemType> *tmpPtr = front->next;
e = tmpPtr->data;
front ->next = tmpPtr ->next;
{
Init();
}
template <class ElemType>
QueueLink<ElemType>::~LinkQueue()
{
Clear();
}
template <class ElemType>
int QueueLink<ElemType>::Length()const
{
int count = 0;
{
return rear == front;
}
template <class ElemType>
void QueueLink<ElemType>::Clear()
{
ElemType tmpElem;
while(Lengh() > 0)
{
OutQueue(tmpElem);
}
}
template <class ElemType>
StatusCode GetHead(ElemType &e) const; //取队头操作
StatusCode EnQueue(const ElemType &e); //入队操作
LinkQueue(const LinkQueue<ElemType> ©); //复制构造函数
LinkQueue<ElemType>&operator = (const LinkQueue<ElemType> ©); //赋值运算符重载
void QueueLink<ElemType>::Traverse(void(*Visit)(ElemType &))
{
for(Node<ElemType> *tmpPtr = front->next;tmpPtr != NULL;tmpPtr = tmpPtr->next)
{
(*Visit)(tmpPtr->data);
{
if(© != this)
{
Clear();
for(Node<ElemType> *tmpPtr = copy.front ->next;tmpPtr != NULL;tmpPtr = tmpPtr->next;)
{
InQueue(tmpPtr->data);
}
}
return *this;
{
if(!Empty())
{
Node<ElemType>* tmpPtr = front->next;
e = tmpPtr->data;
return SUCCESS;
}
else
return UNDER_FLOW;
}
template <class ElemType>
StatusCode QueueLink<ElemType>::EnQueue(const ElemType &e)
}
template <class ElemType>
void Queue百度文库ink<ElemType>::Init()
{
rear = front = new Node<ElemType>;
}
template <class ElemType>
QueueLink<ElemType>::LinkQueue()
for(Node<ElemType> *tmpPtr = front->next;tmpPtr != NULL;tmpPtr = tmpPtr->next)
count++;
return count;
}
template <class ElemType>
bool QueueLink<ElemType>::QueueEmpty() const
}
{
Init();
for(Node<ElemType> *tmpPtr = copy.front ->next;tmpPtr != NULL;tmpPtr = tmpPtr->next;)
InQueue(tmpPtr->data);
}
template <class ElemType>
QueueLink<ElemType>& QueueLink<ElemType>::operator=(const QueueLink<ElemType> ©)
{
Node<ElemType> *tmpPtr = new Node<ElemType>(e);
rear->next = tmpPtr;
rear = tmpPtr;
return SUCCESS;
}
template <class ElemType>
QueueLink<ElemType>& QueueLink<ElemType>::LinkQueue(const QueueLink<ElemType> ©)