【数据结构】顺序队列 Queue

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


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


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

【实验说明】

我选择的题目:课本中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:
【数据结构】顺序队列 Queue_第2张图片
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.简单测试程序的几种情况,分析需要改进的地方
【数据结构】顺序队列 Queue_第3张图片

【相关代码】

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;
}

comparequeue.cpp
#include "queue.h"
#include <iostream>
#include <string>
using namespace std;

void Introduction();
void Compare();

int main(){
	Introduction();
	Compare();
	return 0;
}
//show the introduction of the program
void Introduction(){
	cout<<"Hi! This program is compare two parts of characters."<<endl
		<<"First you should type in a line of characters which is supposed to consist of two parts "
		<<"separated by a colon ':' ."<<endl
		<<"And then enter <Enter> to quit put in"<<endl
		<<"Then I will show you a single character as follows :"<<endl
		<<"[N]  No colon on the line"<<endl
		<<"[L]  The left part (before the colon) is longer than the right"<<endl
		<<"[R]  The right part (after the colon) is longer than the left"<<endl
		<<"[D]  The left and the right parts have the same length but different"<<endl
		<<"[S]  The left and the right parts are exactly the same"<<endl
		<<"And enter [Ctrl+'Z'] to stop input"<<endl
		<<"So , just try it!"<<endl;
	return;
}

void Compare(){
	Extended_queue left_queue;
	char left_char;
	bool waiting=true;
	while(cin>>left_char && waiting){
		if(left_char==':'){
			waiting=false;  //if ':' is entered, quit the left input
			break;
		}
		else if(left_queue.full())
			cout<<"Queue is full!"<<endl; //Warning for the queue full
		else 
			left_queue.append(left_char);
		        //if the input is not ':' and there is space in the queue
		        //append the charcter to the queue
	}
	char right_char;
	bool same=true;
	while(!waiting && cin>>right_char){  //if ':' is input before 
		                                 //now input the righter charachters
		if(left_queue.empty()){
			cout<<"R"<<endl;    //if the left characters are all taken out
			                    //and the user is still inputing, right must longer than left
			                    //print [R]
			return;
		}
		else{
			left_queue.serve_and_retrieve(left_char);
			if(left_char!=right_char)
				same=false;    //if the character input now is different from
			                  //the one put into left before, set the bool same to 'false'
		}
	}
	if(waiting){
		cout<<"N"<<endl;   //if the user stop inputing while there is no ':' has been input
		                   //print [N]
		return ;
	}
	if(!left_queue.empty()){
		cout<<"L"<<endl;   //if user stop inputing while characters in left_queue are not all taken out
		                   //left must longer than right
		                   //print [L]
		return ;
	}
	if(left_queue.empty() && same){
		cout<<"S"<<endl;   //if user stop inputing while left_quque is just empty and bool same is true
		                   //left and right have the exactly same characters
		                   //print [S]
		return;
	}

	return;

}


【过程记录】

实验截图:
【数据结构】顺序队列 Queue_第4张图片


【结果分析】

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


实验代码下载:http://download.csdn.net/detail/xiaowei_cqu/4431332

(转载请注明作者和出处:http://blog.csdn.net/xiaowei_cqu 未经允许请勿用于商业用途)





你可能感兴趣的:(数据结构,Queue)