LinkQueue(链队列)
关于Node.h,请参考LinkStack
#include"Node.h"
template<typename ElemType>
class LinkQueue
{
protected:
Node<ElemType> *front,*rear;
int count;
public:
LinkQueue();
virtual ~LinkQueue();
int Length()const;
bool Empty()const;
void Clear();
bool Inqueue(const ElemType &e);
bool Outqueue(ElemType &e);
bool GetHead(ElemType &e) const;
void Traverse(void(*visit)(const ElemType &))const;
LinkQueue(const LinkQueue<ElemType> ©);
LinkQueue<ElemType> &operator=(const LinkQueue<ElemType> ©);
};
template<typename ElemType>
//构造一个空队列;
LinkQueue<ElemType>::LinkQueue()
{
front=rear=new Node<ElemType>;
count=;
}
template<typename ElemType>
//虚虚构函数
LinkQueue<ElemType>::~LinkQueue()
{
Clear();
}
template<typename ElemType>
//返回队列长度
int LinkQueue<ElemType>::Length()const
{
return count;
}
template<typename ElemType>
//判断队列为空
bool LinkQueue<ElemType>::Empty()const
{
return count==;
}
template<typename ElemType>
//清除队列
void LinkQueue<ElemType>::Clear()
{
ElemType tmpElem;
while(!Empty())
Outqueue(tmpElem);
}
template<typename ElemType>
//队尾进队
bool LinkQueue<ElemType>::Inqueue(const ElemType &e)
{
Node<ElemType> *tmpPtr=new Node<ElemType>(e);
rear->next=tmpPtr;
rear=tmpPtr;
count++;
return true; }
template<typename ElemType>
//队头出队
bool LinkQueue<ElemType>::Outqueue(ElemType &e)
{
if(!Empty())
{
Node<ElemType> *tmpPtr=front->next;
e=tmpPtr->data;
front->next=tmpPtr->next;
if(rear==tmpPtr)
rear=front;
delete tmpPtr;
count--;
return true;
}
else
return false;
}
template<typename ElemType>
//返回队头元素
bool LinkQueue<ElemType>::GetHead(ElemType &e) const
{
if(!Empty())
{
e=front->next->data;
return true;
}
else
return false; }
template<typename ElemType>
//visit
void LinkQueue<ElemType>::Traverse(void(*visit)(const ElemType &))const
{
for(Node<ElemType> *tmpPtr=front->next;tmpPtr;tmpPtr=tmpPtr->next)
(*visit)(tmpPtr->data);
}
template<typename ElemType>
//复制构造
LinkQueue<ElemType>::LinkQueue(const LinkQueue<ElemType> ©)
{
rear=front=new Node<ElemType>;
count=;
for(Node<ElemType> *tmpPtr=copy.front->next;tmpPtr!=NULL;tmpPtr=tmpPtr->next)
Inqueue(tmpPtr->data);
}
template<typename ElemType>
//重载=operator
LinkQueue<ElemType> &LinkQueue<ElemType>::operator=(const LinkQueue<ElemType> ©)
{
if(©!=this)
{
Clear();
for(Node<ElemType> *tmpPtr=copy.front->next;tmpPtr!=NULL;tmpPtr=tmpPtr->next)
Inqueue(tmpPtr->data);
return *this;
} }
LinkQueue(链队列)的更多相关文章
- C语言实现链队列的初始化&进队&出队
/*链表实现队列的一系列操作*/ #include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 typed ...
- 【Java】 大话数据结构(7) 循环队列和链队列
本文根据<大话数据结构>一书,实现了Java版的循环队列.链队列. 队列:只允许在一端进行插入操作,而在另一端进行删除操作的线性表. 1.循环队列 队列的顺序储存结构:用数组存储队列,引入 ...
- 队列的理解和实现(二) ----- 链队列(java实现)
什么是链队列 链队是指采用链式存储结构实现的队列,通常链队用单链表俩表示.一个链队显然需要两个分别指示队头和队尾的指针,也称为头指针和尾指针,有了这两个指针才能唯一的确定. package 链队列; ...
- C语言链队列
链队列类似于单链表,为了限制只能从两端操作数据,其结构体内有2个指针分别指向头尾,但队列里的节点用另一种结构体来表示,头尾指针则为指向该结构体的类型.只能通过操作头尾指针来操作队列. typedef ...
- java实现链队列
java实现链队列的类代码: package linkqueue; public class LinkQueue { class Element { Object elem; Element next ...
- 数据结构 - 链队列的实行(C语言)
数据结构-链队列的实现 1 链队列的定义 队列的链式存储结构,其实就是线性表的单链表,只不过它只能尾进头出而已, 我们把它简称为链队列.为了操作上的方便,我们将队头指针指向链队列的头结点,而队尾指针指 ...
- 用OC基于链表实现链队列
一.简言 在前面已经用C++介绍过链队列的基本算法,可以去回顾一下https://www.cnblogs.com/XYQ-208910/p/11692065.html.少说多做,还是上手撸代码实践一下 ...
- javascript实现数据结构与算法系列:队列 -- 链队列和循环队列实现及示例
1 队列的基本概念 队列(Queue):也是运算受限的线性表.是一种先进先出(First In First Out ,简称FIFO)的线性表.只允许在表的一端进行插入,而在另一端进行删除. 队首(fr ...
- java与数据结构(8)---java实现链队列
链队列 实际上就是单链表,只是规定了删除在队头进行,添加在队尾进行. 链队列代码结构 package list.queue; public interface Queuable<T>; p ...
- 链队列之C++实现
链队列时建立在单链表的基础之上的.由于是动态分配节点内存,所以无需判满. 链队列的形式如下: 1.队列空 2.队列存在数据 下面介绍下C++实现的链队列,VC6下调试通过. 1.文件组织 2.lq.h ...
随机推荐
- 既然函数也是对象,那么为什么this不指向普通函数?
function a(){ var b=1; function exp(){ alert(this.b); } exp(); } var b=2; a(); 既然函数是对象,且exp是在a中运行的,那 ...
- Spring Boot 集成swagger实例
原文:https://github.com/x113773/testall/issues/5 1. 首先添加maven依赖``` <dependency> <groupId>i ...
- [leetcode-566-Reshape the Matrix]
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...
- JAVA基础——方法笔记
java方法_学习笔记 由于我本人对java方法已经比较熟悉了,java方法的定义和使用也比较简单,这里只列举了基于我自身容易搞错的地方,希望对大家的学习有帮助!! 方法的参数可以是基本数据类型,如 ...
- Mathematica学习笔记2
导入文件中的矩阵 mat = Import["...", "Table"] 转化为向量矩阵(元素为数对) data = Table[{mat[[i, j]], ...
- Java程序性能优化-读书笔记(一) 单例模式
单例模式: 目的: 确保系统中一个类只产生一个实例. 好处: 1.对于频繁使用的对象,可以省略创建对象所花费的时间,这对于那些重量级对象而言,是非常可观的一笔系统开销. 2.由于new操作的次数减少, ...
- JavaScript深入之从原型到原型链(本文转载)
JavaScript深入之从原型到原型链(本文转载) https://github.com/mqyqingfeng/Blog.原文地址 构造函数创建对象 我们先使用构造函数创建一个对象: functi ...
- Oracle 左连接 left join、右连接right join说明
Oracle 左.右连接 + 在等号 左边表示右连接 获取右表所有记录,即使左表没有对应匹配的记录. + 在等号 右边表示左连接 获取左表所有记录,即使右表没有对应匹配的记录. 例子: selec ...
- 关于 Eclipse中的Web项目 部署的文件位置 查看jsp源码的部署位置
使用 eclipse 开发web项目 会默认 部署在 工作目录下: .metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps 在这里 ...
- Maven-FAQ
1.Failure to transfer org.apache.maven.plugins:maven-resources-plugin:pom:2.5...: Q: 第一次使用maven+ecli ...