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 ...
随机推荐
- 文档模型(JSON)使用介绍
一.背景 E.F.Codd在1970年首次提出了数据库系统的关系模型,从此开创了数据库关系方法和关系数据理论的研究,为数据库技术奠定了理论基础,数据库技术也开始蓬勃发展.而随着几大数据库厂商陆续发布的 ...
- js 实现图片压缩并转换成base64(data:image/jpeg;base64)格式
<!DOCTYPE html> <html> <head> <!--by 0o晓月メ http://www.cnblogs.com/final-elysion ...
- OpenCV 之 图像分割 (一)
1 基于阈值 1.1 基本原理 灰度阈值化,是最简单也是速度最快的一种图像分割方法,广泛应用在硬件图像处理领域 (例如,基于 FPGA 的实时图像处理). 假设输入图像为 f,输出图像为 g,则经 ...
- Bash : 索引数组
Bash 提供了两种类型的数组,分别是索引数组(indexed array)和关联数组(associative array).本文主要介绍索引数组的基本用法. 索引数组的基本特点 Bash 提供的数组 ...
- docker~写个容器启动的bash脚本
回到目录 bash脚本在linux里就相当于win里的bat和cmd及ps脚本,可以把一般指令组织在一起,统一去执行,比如我有一些docker容器需要统一去启动,这时,你可以把它们写成一个bash脚本 ...
- poj_1679: The Unique MST【次小生成树】
题目链接 参考博客 希望注释足够清楚..欢迎指出不足~ #include<cstdio> #include<cstring> #include<algorithm> ...
- Protobuf动态解析在Java中的应用 包含例子程序
最近在做ProtoBuf相关的项目,其中用到了动态解析,网上看了下相关资料和博文都比较少,自己来写一个记录一下学习过程. Protocol Buffers是结构化数据格式标准,提供序列化和反序列方 ...
- [luogu P1967][NOIp2013] 货车运输
题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最多 ...
- form表单提交图片禁止跳转
问题: 最近在做项目时,遇到上传图片需求,且在不跳转的情况下获取到返回信息 思路: 1. 使用ajax发送异步请求,经多次测试,最终以失败告终 2. iframe 禁止跳转(未尝试) 3. 修改fo ...
- Web Service--第一次接触web service
Web Service 首发于开源中国 1. 背景 中国移动短信网关需求,要能够发送短信.开发材料只有一个短信发送配置:包括ID,password,code,url.一个jar包还有一个老旧的html ...