---
title: "【数据结构】顺序队列 Queue"
author: "SunRaIN_"
date: "2012-07-16T11:59:00Z"
tags: []
slug: "queue"
draft: false
lastmod: "2012-12-21T15:49:34Z"
id: 1751815
url: "https://751401909.iteye.com/blog/1751815"
---

08年9月入学，12年7月毕业，结束了我在软件学院愉快丰富的大学生活。此系列是对四年专业课程学习的回顾，索引参见：[http://blog.csdn.net/xiaowei\_cqu/article/details/7747205](http://blog.csdn.net/xiaowei_cqu/article/details/7747205)

# 顺序队列各种基本运算算法的实现

顺序队列是较为普遍的一种队列实现方式，采用环状数组来存放队列元素，并用两个变量分别指向队列的前端(front)和尾端(rear)，往队列中加进或取出元素时分别改变这两个变量的计数(count)。  
队列中用环状数组存储数据（合理利用空间、减少操作），通过基本的append（）将元素加入队列，serve（）将元素移出队列，先进入的先移出，retieve得到最先加入队列的元素。此外在继承的Extended\_queue（）中我增加了empty（）和serve\_and\_retrieve（）的功能。

![](http://my.csdn.net/uploads/201207/16/1342411552_3379.png)

## 【实验说明】

我选择的题目：课本中Programming Projects 3.3 P1  
问题描述：Write a function that will read one line of input from the terminal. The input is supposed to consist of two parts separated by a colon ':'. As its results, your function should produce a single character as follows:  
N No colon on the line.  
L The left part(before the colon) is longer than the right.  
R The right part(after the colon) is longer than the left.  
D The left and right parts have the same length but are different.  
S The left and right are exactly the same.

Examples:

![](http://my.csdn.net/uploads/201207/15/1342317698_6686.png)

Use either a queue or an extended queue to keep track of the left part of the line while reading the right part.  
1.分析队列要实现的基本功能以及继承的类要拓展的功能从而确定基本成员函数——append（），serve（）,retireve（），拓展队列中：empty（），serve\_and\_retrieve（），确定队列中以环形数组存储数据从而确定成员函数——Queue\_entry entry\[],count(记录队列中数据数量)  
2.编写队列的头文件及实现  
3.分析题目中结束程序并输出几种字母的条件，简略画出功能实现的流程图，编写程序。（具体思路见源代码注释）  
4.简单测试程序的几种情况，分析需要改进的地方

![](http://my.csdn.net/uploads/201207/15/1342317841_5777.png)

## 【相关代码】

queue.h

```
#ifndef QUEUE_H
#define QUEUE_H

const int maxqueue=10;
enum Error_code {success,overflow,underflow};
typedef char Queue_entry ;

class Queue{
public:
	Queue();
	bool empty() const;
	Error_code append(const Queue_entry &item);
	Error_code serve();
	Error_code retrieve(Queue_entry &item)const;
protected:
	int count;
	int front,rear;
	Queue_entry entry[maxqueue];
};

class Extended_queue:public Queue{
public:
	bool full()const;
	int size()const;
	void clear();
	Error_code serve_and_retrieve(Queue_entry &item);
};

#endif
```

queue.cpp

```
#include"queue.h"

Queue::Queue()
{
	count=0;
	rear=maxqueue-1;
	front=0;
}

bool Queue::empty() const
{
	return count==0;
}

Error_code Queue::append(const Queue_entry &item)
{
	if(count>=maxqueue)return overflow;
	count++;
	rear=((rear+1)==maxqueue)?0:(rear+1);
	entry[rear]=item;
	return success;
}

Error_code Queue::serve()
{
	if(count<=0)return underflow;
	count--;
	front=((front+1)==maxqueue)?0:(front+1);
	return success;
}

Error_code Queue::retrieve(Queue_entry &item) const
{
	if(count<=0)return underflow;
	item=entry[front];
	return success;
}
bool Extended_queue::full() const{
	return count==maxqueue;
}
int Extended_queue::size()const{
	return count;
}
void Extended_queue::clear(){
	count=0;
	rear=front;
}
Error_code Extended_queue::serve_and_retrieve(Queue_entry &item){
	if(count==0)return underflow;
	count--;
	item=entry[front];
	front=((front+1)==maxqueue)?0:(front+1);
	return success;
}
```

## 【过程记录】

实验截图：

![](http://my.csdn.net/uploads/201207/15/1342318011_5584.png)

## 【结果分析】

1.实验中我以课本后面的练习题为例，实现并验证了顺序队列的更种功能。  
2.队列与栈一样是在类中以数组存储数据，但由于队列先进先出的特点在实现存储形式上与栈有一定的不同。因为如果与栈一样对数据操作，队列会无限向后扩大，而前面取出过数据的地方将不会再被利用，十分浪费，也很容易溢出。所以我们采用循环数组来存储，这样合理利用了资源。但在类的实现要要极为时刻考虑周全rear和front的各种可能，要判断是不是循环到了前面，count在此时的功能也极为突出。  
3.书中的程序看似简单，但实际判断各种输出情况的时候却极难考虑周全。我首先做出了简易的流程图，然后才写函数，具体分析及思路可见我源码的注释。另外本题还有另外一种实现思路：即将左右输入分别存放在两个队列中，边取去边比较，那样在逻辑上容易理解一些。但鉴于题目的要求，我还是用边从左边队列中取出边比较右边输入的方法。  
4.我在实验中遇到的问题：  
（1）自己一开始在循环判断用的是cin.get()=='\\n'即遇到回车就停止输入，但却无法如料想中结束循环……最终采用cin&gt;&gt;a && waiting（用以标志‘：’的输入）来作为循环终止的条件，这样虽然可以运行，但用户必须输入Ctrl+‘Z’以结束输入。看来自己对输入流的理解与掌握还没有到位。  
（2）另外在检验的时候，我发现输入‘：’之前是否输入回车情况是有区别的。如  
输入“sam:sam”(无空格)，结果为“R”  
输入“sam : sam”(有空格)，结构为“S”  
显然后者是我希望得到的结果，我分析可能是前面情况‘：’被列入了right的判断，从而使结构右边比左边长。还没有想到如何改进。

### 实验代码下载：[http://download.csdn.net/detail/xiaowei\_cqu/4431332](http://download.csdn.net/detail/xiaowei_cqu/4431332)

### （转载请注明作者和出处：[http://blog.csdn.net/xiaowei\_cqu](http://blog.csdn.net/xiaowei_cqu)未经允许请勿用于商业用途）