C++数据结构之链式队列,实现的基本思想和链式栈的实现差不多,比较不同的一点也是需要注意的一点是,链式队列的指向指针有两个,一个是队头指针(front),一个是队尾指针(rear),注意指针的指向是从队首指到队尾(front -> Front_Node -> …… -> rear -> Rear_Node).

代码:

Node.h文件

/*
 * Node.h
 *
 *  Created on: 2015年9月10日
 *      Author: Lv_Lang
 */

#ifndef NODE_H_
#define NODE_H_

#include "stdio.h" 	// for NULL

typedef int Queue_entry;
typedef Queue_entry Node_entry;

struct Node
{
	//	data members
	Node_entry entry; // 存放的元素
	Node *next;		  // 指向下一个元素的指针
	//	Constructors
	Node();
	Node(Node_entry item, Node *add_on = NULL);
};

#endif /* NODE_H_ */

Node.cpp文件:

/*
 * Node.cpp
 *
 *  Created on: 2015年9月10日
 *      Author: Lv_Lang
 */
#include "Node.h"

Node::Node()
{
	next = NULL;
}
Node::Node(Node_entry item, Node *add_on)
{
	entry = item;
	next = add_on;
}

Queue.h文件:

/*
 * Queue.h
 *
 *  Created on: 2015年9月10日
 *      Author: Lv_Lang
 */

#ifndef QUEUE_H_
#define QUEUE_H_

#include "stdio.h"
#include "Node.h"
enum Error_code {overflow,underflow,success};

class Queue
{
public:
//  Standard Queue methods
	Queue();
	bool empty()const;
	Error_code append(const Queue_entry &item);
	Error_code serve();
	Error_code retrieve(Queue_entry &item)const;
//	Safety features for linked structures
	~Queue();
	Queue(const Queue &original);
	void operator = (const Queue &original);
//  Extended linked queue
	bool full()const;
	int size()const;
	void clear();
protected:
	Node *front, *rear;
};

#endif /* QUEUE_H_ */

Queue.cpp文件

/*
 * Queue.cpp
 *
 *  Created on: 2015年9月10日
 *      Author: Lv_Lang
 */
#include "Queue.h"

Queue::Queue()
{
	front = NULL;
	rear = NULL;
}
bool Queue::empty()const
{
	if((front == NULL) && (rear == NULL))
		return true;
	else
		return false;
}
Error_code Queue::append(const Queue_entry &item)
{
	Node *new_rear = new Node(item);
	if(new_rear == NULL)
		return overflow;
	if(rear == NULL) front = rear = new_rear;
	else
	{
		rear->next = new_rear;
		rear = new_rear;
	}
	return success;
}
Error_code Queue::serve()
{
	if(empty())
		return underflow;
	else
	{
		Node *old_front = front;
		front = front->next;
		if(front == NULL) rear = NULL;// 此处需注意将rear也置为0
		delete	old_front;
		return success;
	}
}
Error_code Queue::retrieve(Queue_entry &item)const
{
	if(empty())
			return underflow;
	else
	{
		item = front->entry;
		return success;
	}
}
Queue::~Queue()
{
	if(!empty())
	{
		serve();
	}
}
Queue::Queue(const Queue &original)
{
	Node *new_front, *original_front = original.front;
	if(original.front == NULL)
		front = rear= NULL;
	else
	{
		front = new_front = new Node(original_front->entry);
		while(original_front->next != NULL)
		{
			original_front = original_front->next;
			new_front->next = new Node(original_front->entry);
			new_front = new_front->next;
		}
	}
}
void Queue::operator =(const Queue &original)
{
	Node *new_front, *original_front = original.front;
	if(original.front == NULL)
		front = rear= NULL;
	else
	{
		if(!empty())
			clear();
		else
		{
			front = new_front = new Node(original_front->entry);
			while(original_front->next != NULL)
			{
				original_front = original_front->next;
				new_front->next = new Node(original_front->entry);
				new_front = new_front->next;
			}
		}
	}
}
bool Queue::full()const
{
	Node *new_front = new Node();
	if(new_front == NULL)
<span style="white-space:pre">	</span>{		
<span style="white-space:pre">		delete new_front;</span>
<span style="white-space:pre">		</span>return true;
<span style="white-space:pre">	</span>}
	else
	{
		delete new_front;
		return false;
	}
}
int Queue::size()const
{
	Node *window = front;
	int count = 0;
	while(window != NULL)
	{
		window = window->next;
		count++;
	}
	return count;
}
void Queue::clear()
{
	if(!empty())
	{
		serve();
	}
}

main函数测试文件:

/*
 * main.cpp
 *
 *  Created on: 2015年9月10日
 *      Author: Lv_Lang
 */
#include "Queue.h"

int main()
{
	Queue myqueue;
	myqueue.append(2);
	myqueue.append(6);
	int size;
	size = myqueue.size();
	printf("%s %d\n","Size is ",size);
	myqueue.serve();
	int a;
	myqueue.retrieve(a);
	printf("%d\n",a);
	size = myqueue.size();
	printf("%s %d\n","Size is ",size);

	Queue QUEUE(myqueue);
	int b;
	QUEUE.retrieve(b);
	printf("%d\n",b);
	size = QUEUE.size();
	printf("%s %d\n","Size for QUEUE is ",size);

	myqueue.clear();
	size = myqueue.size();
	printf("%s %d\n","Size is ",size);

	return 0;
}

C++数据结构之链式队列(Linked Queue)的更多相关文章

  1. 数据结构之链式队列(C实现)

    1.1  linkqueue.h #ifndef LINKQUEUE_H #define LINKQUEUE_H #include <stdio.h> #include <mallo ...

  2. C语言数据结构-链式队列的实现-初始化、销毁、清空、长度、队列头元素、插入、删除、显示操作

    1.数据结构-链式队列的实现-C语言 typedef struct QNode { int data; struct QNode *next; }QNode,*QueuePtr; typedef st ...

  3. 数据结构-链式队列-C++

    用链表搭建的栈与队列相对简单,队列的特点是先进先出,不啰嗦了,由于代码比较简单,相信光顾的人不会太多,下面直接贴代码. 头文件 #ifndef QUEUELI_H #define QUEUELI_H ...

  4. 数据结构——线性表——队列(queue)

    队列也是一种特殊的线性表,它的特点是先入先出(FIFO,即first in first out).它的意思也很直观,想象一下排队买票,先排的人先买(插队是不对的,所以别去想).它也是很常用的数据结构, ...

  5. 数据结构14:队列(Queue),“先进先出”的数据结构

    队列是线性表的一种,在操作数据元素时,和栈一样,有自己的规则:使用队列存取数据元素时,数据元素只能从表的一端进入队列,另一端出队列,如图1. 图1 队列示意图 称进入队列的一端为“队尾”:出队列的一端 ...

  6. python 队列(QUEUE)

    QUEUE python中多线程编程的数据结构 基本FIFO队列 class Queue.Queue(maxsize=0) 先进先出,maxsize为队列中能存放的数据个数上限. import Que ...

  7. [置顶] ※数据结构※→☆线性表结构(queue)☆============优先队列 链式存储结构(queue priority list)(十二)

    优先队列(priority queue) 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有 ...

  8. 利用链式队列(带头节点)解决银行业务队列简单模拟问题(c++)-- 数据结构

    题目: 7-1 银行业务队列简单模拟 (30 分)   设某银行有A.B两个业务窗口,且处理业务的速度不一样,其中A窗口处理速度是B窗口的2倍 —— 即当A窗口每处理完2个顾客时,B窗口处理完1个顾客 ...

  9. 数据结构Java实现07----队列:顺序队列&顺序循环队列、链式队列、顺序优先队列

    一.队列的概念: 队列(简称作队,Queue)也是一种特殊的线性表,队列的数据元素以及数据元素间的逻辑关系和线性表完全相同,其差别是线性表允许在任意位置插入和删除,而队列只允许在其一端进行插入操作在其 ...

随机推荐

  1. ajax再接触

    贴三个目前写的已经工作了的ajax,并且给出备注: 1.这是第一个ajax以及它的接口文档,需要注意的是1.因为返回数据是xml所以多加了contentType:"application/x ...

  2. js的预编译机制

    1.var定义的是“当前作用域下的一个变量”,当在函数内部不使用var声明时,会被当做全局变量而不是函数内的局部变量(严格模式下还会报错) 2.js的预编辑:[对定义式函数]直接创建为作用域上的函数变 ...

  3. linux笔记:网络命令ping,traceroute,ifconfig,netstat;挂载和卸载命令mount,umount

    命令名称:ping功能:测试网络连通性命令所在路径:/bin/ping用法:ping [-c] IP地址参数:-c 指定发送次数 命令名称:ifconfig功能:查看和设置网卡信息(临时设置)命令所在 ...

  4. js时间差转为天数

    function DateDiff(sDate1, sDate2){ //sDate1和sDate2是2006-12-18格式 var aDate, oDate1, oDate2, iDays aDa ...

  5. db:seed 更好的生成测试数据

    make:model -m -> 在database/migrations/目录下生成的table表中设置表的字段名和字段类型->在app/目录下对应的模型文件中设置可添加字段 -> ...

  6. (18)odoo规范

    * 约定    # 命名会用  蛇形式或驼峰式        todo_task_stage 蛇形式        class TodoTask 驼峰式      变量名还是蛇形居多, 类名和方法名驼 ...

  7. java中的异常和处理

    算术异常类:ArithmeticExecption 空指针异常类:NullPointerException 类型强制转换异常:ClassCastException 数组负下标异常:NegativeAr ...

  8. 关于位域如何节省内存(C++)

    位域:  最先使用在c语言中后来C++继承了这一优良的特点. 举个栗子:     int  -->  4字节   2^32位 ,如果我们只需要其表达一个0~16的数字, 使用一个int就显得稍稍 ...

  9. 给文本标签UILabel添加长按复制功能

    http://www.111cn.net/sj/iOS/104236.htm http://blog.csdn.net/lrenjun/article/details/12582927 自定义一个可复 ...

  10. iOS开发之——从零开始完成页面切换形变动画

    前言 某天我接到了UI发给我的两张图: 需求图.png 看到图的时候我一脸懵逼,显然我需要做一个页面切换的指示动画.老实说,从大三暑假开始做iOS开发也一年有余了,但是遇到复杂动画总是唯恐避之不及,只 ...