`
Dev|il
  • 浏览: 121963 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

3.4.2 -队列的链式表示和实现

 
阅读更多
队列的链式表示和实现

   队列是一种先进先出的数据结构,和食堂排队打饭类似,在前面的先打到饭,而后来者只有等前面的打完饭,后面的才能进行
以下给出C++实现
#include <iostream>
using namespace std;

#define ElemType int
typedef struct QNode{
	ElemType data;
	struct QNode *next;
}Node, *NodePtr;

//72
//链队列
class Queue{
private:
	NodePtr front; //队头
	NodePtr rear;  //队尾
public:
	Queue()
	{
		InitQueue();
	}
	bool InitQueue(); //构造一个空队列
	bool DestoryQueue(); //销毁队列
	void ClearQueue(); //清空队列
	bool QueueEmpty(); //队列是否为空
	int QueueLength(); //队列长度
	void GetHead(ElemType &e); //获取队头元素
	bool EnQueue(ElemType e); //把e插入到队尾
	bool DeQueue(ElemType &e); //如果队列不为空,删除队头元素,用e返回其值
};

//初始队列
bool Queue::InitQueue()
{
	this->front = this->rear = (NodePtr)malloc(sizeof(Node));
	if(!this->front)
		return false; //创建失败 
	this->front->next = NULL;
	return true;
}

//销毁队列
bool Queue::DestoryQueue()
{
	while(this->front) //不用新开一个变量,直接用队尾指针
	{
		this->rear = this->front->next;
		delete this->front;
		this->front = this->rear;
	}
	return true;
}
//插入元素到队尾,相当于入队列
bool Queue::EnQueue(ElemType e)
{
	NodePtr cur = (NodePtr)malloc(sizeof(Node));
	if(!cur)
		return false;
	cur->data = e; 
	this->rear->next = cur; //让队尾的下一个元素为cur
	this->rear = cur;  //移动队尾指针
	this->rear->next = NULL; //让队尾指针为NULL
	return true;
}
//队列是这样的 队头(数据域为空) 第一个元素 ....第n个元素 队尾 出队列
bool Queue::DeQueue(ElemType &e)
{
	if(this->front == this->rear)
		return false; //队列为空
	NodePtr cur = this->front->next; //找到第一个元素,也就是队头后面的元素
	e = cur->data;
	this->front->next = cur->next; //删除第一个元素
	if(this->rear == cur) //如果第一个元素刚好是队尾元素
		this->rear = this->front; //则让 this->rear 和front为第一个空元素
	delete cur; //销毁
	return true;
}
//清空队列
void Queue::ClearQueue()
{
	this->rear = this->front;
}
//判断队列是否为空
bool Queue::QueueEmpty()
{
	return this->rear == this->front;
}
//获取队列长度
int Queue::QueueLength()
{
	NodePtr cur = this->front->next;
	int size = 0;
	while(cur)
	{
		size++;
		cur = cur->next;
	}
	return size;
}
//获取队头数据
void Queue::GetHead(ElemType &e)
{
	e = this->rear->data;
}
int main()
{
	int e;
	Queue queue;
	queue.EnQueue(3);
	queue.DeQueue(e);
	cout<<e<<endl;
	queue.EnQueue(4);
	cout<<"队列长度:"<<queue.QueueLength()<<endl;
	if(queue.QueueEmpty())
		cout<<"队列为空\n";
	else
		cout<<"队列不为空\n";
	queue.GetHead(e);
	cout<<"队头元素:"<<e<<endl;
	return 0;
}


java代码实现

Node(结点类):
package study.queue;
//结点类
public class Node<T> {
	
	private T data;
	
	private Node<T> next;

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public Node<T> getNext() {
		return next;
	}

	public void setNext(Node<T> next) {
		this.next = next;
	}
}


Queue(队列类):
package study.queue;

public class Queue<T> {
	//队头
	private Node<T> front = null;
	//队尾
	private Node<T> rear = null;
	//获取队列元素
	private T e; 
	
	public Queue(){
		this.InitQueue();
	}
	
	/**
	 * 获取元素
	 * @return
	 */
	public T getE() {
		return e;
	}
	/**
	 * 初始化队列
	 * @return
	 */
	private boolean InitQueue(){
		this.front = this.rear = new Node<T>();
		if(this.front == null)
			return false; //初始化失败
		this.front.setNext(null);
		return true;
	}
	/**
	 * 销毁队列
	 */
	public void DestoryQueue(){
		while(this.front != null){
			this.rear = this.front.getNext();
			this.front = this.rear;
		}
	}
	/**
	 * 入队列
	 * @param e
	 * @return
	 */
	public boolean EnQueue(T e){
		Node<T> cur = new Node<T>();
		if(cur == null)
			return false;
		cur.setData(e);
		this.rear.setNext(cur);
		this.rear = cur;
		this.rear.setNext(null);
		return true;
	}
	/**
	 * 出队列
	 * @return
	 */
	public boolean DeQueue(){
		if(this.front == this.rear)
			return false;
		Node<T> cur = this.front.getNext();
		e = cur.getData();
		this.front.setNext(cur.getNext());
		if(cur == this.rear)
			this.rear = this.front;
		return true;
	}
	/**
	 * 情况队列
	 */
	public void ClearQueue(){
		this.rear = this.front;
	}
	/**
	 * 判断队列是否为空 是则返回ture
	 * @return
	 */
	public boolean QueueEmpty(){
		return this.rear == this.front;
	}
	/**
	 * 获取队列长度
	 * @return
	 */
	public int QueueLength(){
		int size = 0;
		Node<T> cur = this.front.getNext();
		while(cur != null){
			size++;
			cur = cur.getNext();
		}
		return size;
	}
	/**
	 * 获取队头元素
	 * @return
	 */
	public T GetHead(){
		return this.rear.getData();
	}
}


Main(测试类):
package study.queue;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Queue<Integer> queue = new Queue<Integer>();
		queue.EnQueue(3);
		queue.DeQueue();
		System.out.println("出队列元素:" + queue.getE());
		queue.EnQueue(4);
		System.out.println("队列长度:" + queue.QueueLength());
	}

}
分享到:
评论

相关推荐

    严蔚敏:数据结构题集(C语言版)

    3.4.2 链队列--队列的链式表示和实现 3.4.3 循环队列--队列的顺序表示和实现 3.5 离散事件模拟 第4章 串 4.1 串类型的定义 4.2 串的表示和实现 4.2.1 定长顺序存储表示 4.2.2 堆分配存储表示 4.2.3 串的块链存储...

    严蔚敏 数据结构(C语言版) 代码 23490 书中算法

    3.4.2 循环队列——队列的顺序表示和实现 62 3.4.3 链队——队列的链式表示和实现 65 3.5 队列的应用 67 3.6 小结 69 习题 69 第4章 串、数组和广义表 73 4.1 串 73 4.1.1 串的类型定义 73 4.1.2 串...

    谭浩强C语言程序设计,C++程序设计,严蔚敏数据结构,高一凡数据结构算法分析与实现.rar

    1.4 C++程序的编写和实现 1.5 关于C++上机实践 习题 第2章 数据类型与表达式 2.1 C++的数据类型 2.2 常量 2.2.1 什么是常量 2.2.2 数值常量 2.2.3 字符常量 2.2.4 符号常量 2.3 变量 2.3.1 什么是变量 2.3.2 ...

    谭浩强C语言程序设计,C++程序设计,严蔚敏数据结构,高一凡数据结构算法分析与实现.rar )

    1.4 C++程序的编写和实现 1.5 关于C++上机实践 习题 第2章 数据类型与表达式 2.1 C++的数据类型 2.2 常量 2.2.1 什么是常量 2.2.2 数值常量 2.2.3 字符常量 2.2.4 符号常量 2.3 变量 2.3.1 什么是变量 2.3.2 ...

    jQuery权威指南-源代码

    《jQuery权威指南》除了理论知识丰富而全面外,它还有一个最大的特点就是注重实战,每个知识点都有一个完整的案例,包括需求分析、代码实现和结果展示三个部分,而且还包含两个综合性的案例,它的实践性之强是目前...

    C语言通用范例开发金典.part2.rar

    1.4.7 双亲、孩子和兄弟节点的查询(链式结构) 162 范例1-61 双亲、孩子和兄弟节点的查询 162 ∷相关函数:Parent函数 LeftChild函数 RightChild函数 LeftSibling函数 RightSibling函数 1.4.8 中序遍历二叉树...

    C语言通用范例开发金典.part1.rar

    1.4.7 双亲、孩子和兄弟节点的查询(链式结构) 162 范例1-61 双亲、孩子和兄弟节点的查询 162 ∷相关函数:Parent函数 LeftChild函数 RightChild函数 LeftSibling函数 RightSibling函数 1.4.8 中序遍历二叉树...

    C 开发金典

    1.4.7 双亲、孩子和兄弟节点的查询(链式结构) 162 范例1-61 双亲、孩子和兄弟节点的查询 162 ∷相关函数:Parent函数 LeftChild函数 RightChild函数 LeftSibling函数 RightSibling函数 1.4.8 中序遍历二叉树...

    Node.js MongoDB AngularJSWeb开发中文版.part1

    4.2.3 实现事件发射器和监听器 59 4.3 实现回调 62 4.3.1 向回调函数传递额外的参数 63 4.3.2 在回调中实现闭包 64 4.3.3 链式回调 65 4.4 小结 66 4.5 下一章 66 第5章 在Node.js中处理数据I/O 67 5.1 处理JSON 67 ...

    Python基础教程(第3版)-201802出版-文字版

    15 1.10.3 字符串表示 str 和 repr ............ 15 1.10.4 长字符串、原始字符串和字节 .......................................... 16 1.11 小结 ....................................................... 21 ...

Global site tag (gtag.js) - Google Analytics