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 ...
随机推荐
- php后台拼接输出table表格
<?php header("Content-type:text/html;charset=utf-8"); $str=''; $str.='<table border= ...
- 优化mysql数据库的几个步骤
析问题: 1. 开启慢查询日志. 这个步骤就是为了记录慢查询的sql,为下个步骤做准备,此步骤相关的知识点有如下: 1. show variables like '%slow_query_log%'; ...
- CentOS7.2上用KVM安装虚拟机window10踩过的坑
最近两个星期一直在琢磨kvm安装window10操作系统,并且通过桥接模式与外界通信,经历了九九八十一难,终于搞定.下面就记录以下我们在探索的过程中踩过的坑. 安装KVM 1. 系统要求:需要一台可以 ...
- 关于Latex中插入Visio图片文字不显示的问题
经过探索,将Visio保存为pdf格式是最完美的解决方式,因为pdf文件保存了所有格式和字体信息. Visio输出pdf时要使其符合PDF/A标准.如果包含Visio的多余信息,就会在一些低版本Lat ...
- 解决java.lang.NumberFormatException: For input string: "id"
今天,项目突然报"java.lang.NumberFormatException:For input string:"id"",项目框架是spring,spri ...
- (转载)在spring的bean中注入内部类
原文链接:http://outofmemory.cn/java/spring/spring-DI-inner-class 在spring中注入内部类,有可能会遇到如下异常信息: 2014-5-14 2 ...
- NewsServiceImpl
package com.pb.news.service.impl; import java.util.List; import com.pb.news.dao.NewsDao;import com.p ...
- Html 学习
行内元素和块级元素 行内元素(行级元素) 多个元素会在一行内显示 块级元素 独立成行 注意:块级元素能够嵌套行内元素 <div> <span></span> < ...
- updateByPrimaryKey和updateByPrimaryKeySelective insert和insertSelective
这两个update都是使用generator生成的mapper.xml文件中,对dao层的更新操作 updateByPrimaryKey对你注入的字段全部更新(不判断是否为Null) updateBy ...
- 【亲测】Appium测试Android混合应用时,第二次切换到WebView失败
要解决的问题:Appium测试Android混合应用时,第二次切换到WebView时失败 原因分析:在用Appium测试Android混合应用时,当程序第一次切换到WebView时,可以正常进行自动化 ...