【数据结构】顺序队列 Queue

前端之家收集整理的这篇文章主要介绍了【数据结构】顺序队列 Queue前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

08年9月入学,12年7月毕业,结束了我在软件学院愉快丰富的大学生活。此系列是对四年专业课程学习的回顾,索引参见:http://www.jb51.cc/article/p-srsfcefa-vo.html


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


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


【实验说明】

我选择的题目:课本中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:

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.h
  1. #ifndef QUEUE_H
  2. #define QUEUE_H
  3.  
  4. const int maxqueue=10;
  5. enum Error_code {success,overflow,underflow};
  6. typedef char Queue_entry ;
  7.  
  8. class Queue{
  9. public:
  10. Queue();
  11. bool empty() const;
  12. Error_code append(const Queue_entry &item);
  13. Error_code serve();
  14. Error_code retrieve(Queue_entry &item)const;
  15. protected:
  16. int count;
  17. int front,rear;
  18. Queue_entry entry[maxqueue];
  19. };
  20.  
  21. class Extended_queue:public Queue{
  22. public:
  23. bool full()const;
  24. int size()const;
  25. void clear();
  26. Error_code serve_and_retrieve(Queue_entry &item);
  27. };
  28.  
  29. #endif
queue.cpp
  1. #include"queue.h"
  2.  
  3. Queue::Queue()
  4. {
  5. count=0;
  6. rear=maxqueue-1;
  7. front=0;
  8. }
  9.  
  10. bool Queue::empty() const
  11. {
  12. return count==0;
  13. }
  14.  
  15. Error_code Queue::append(const Queue_entry &item)
  16. {
  17. if(count>=maxqueue)return overflow;
  18. count++;
  19. rear=((rear+1)==maxqueue)?0:(rear+1);
  20. entry[rear]=item;
  21. return success;
  22. }
  23.  
  24. Error_code Queue::serve()
  25. {
  26. if(count<=0)return underflow;
  27. count--;
  28. front=((front+1)==maxqueue)?0:(front+1);
  29. return success;
  30. }
  31.  
  32. Error_code Queue::retrieve(Queue_entry &item) const
  33. {
  34. if(count<=0)return underflow;
  35. item=entry[front];
  36. return success;
  37. }
  38. bool Extended_queue::full() const{
  39. return count==maxqueue;
  40. }
  41. int Extended_queue::size()const{
  42. return count;
  43. }
  44. void Extended_queue::clear(){
  45. count=0;
  46. rear=front;
  47. }
  48. Error_code Extended_queue::serve_and_retrieve(Queue_entry &item){
  49. if(count==0)return underflow;
  50. count--;
  51. item=entry[front];
  52. front=((front+1)==maxqueue)?0:(front+1);
  53. return success;
  54. }

comparequeue.cpp
  1. #include "queue.h"
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. void Introduction();
  7. void Compare();
  8.  
  9. int main(){
  10. Introduction();
  11. Compare();
  12. return 0;
  13. }
  14. //show the introduction of the program
  15. void Introduction(){
  16. cout<<"Hi! This program is compare two parts of characters."<<endl
  17. <<"First you should type in a line of characters which is supposed to consist of two parts "
  18. <<"separated by a colon ':' ."<<endl
  19. <<"And then enter <Enter> to quit put in"<<endl
  20. <<"Then I will show you a single character as follows :"<<endl
  21. <<"[N] No colon on the line"<<endl
  22. <<"[L] The left part (before the colon) is longer than the right"<<endl
  23. <<"[R] The right part (after the colon) is longer than the left"<<endl
  24. <<"[D] The left and the right parts have the same length but different"<<endl
  25. <<"[S] The left and the right parts are exactly the same"<<endl
  26. <<"And enter [Ctrl+'Z'] to stop input"<<endl
  27. <<"So,just try it!"<<endl;
  28. return;
  29. }
  30.  
  31. void Compare(){
  32. Extended_queue left_queue;
  33. char left_char;
  34. bool waiting=true;
  35. while(cin>>left_char && waiting){
  36. if(left_char==':'){
  37. waiting=false; //if ':' is entered,quit the left input
  38. break;
  39. }
  40. else if(left_queue.full())
  41. cout<<"Queue is full!"<<endl; //Warning for the queue full
  42. else
  43. left_queue.append(left_char);
  44. //if the input is not ':' and there is space in the queue
  45. //append the charcter to the queue
  46. }
  47. char right_char;
  48. bool same=true;
  49. while(!waiting && cin>>right_char){ //if ':' is input before
  50. //now input the righter charachters
  51. if(left_queue.empty()){
  52. cout<<"R"<<endl; //if the left characters are all taken out
  53. //and the user is still inputing,right must longer than left
  54. //print [R]
  55. return;
  56. }
  57. else{
  58. left_queue.serve_and_retrieve(left_char);
  59. if(left_char!=right_char)
  60. same=false; //if the character input now is different from
  61. //the one put into left before,set the bool same to 'false'
  62. }
  63. }
  64. if(waiting){
  65. cout<<"N"<<endl; //if the user stop inputing while there is no ':' has been input
  66. //print [N]
  67. return ;
  68. }
  69. if(!left_queue.empty()){
  70. cout<<"L"<<endl; //if user stop inputing while characters in left_queue are not all taken out
  71. //left must longer than right
  72. //print [L]
  73. return ;
  74. }
  75. if(left_queue.empty() && same){
  76. cout<<"S"<<endl; //if user stop inputing while left_quque is just empty and bool same is true
  77. //left and right have the exactly same characters
  78. //print [S]
  79. return;
  80. }
  81.  
  82. return;
  83.  
  84. }


【过程记录】

实验截图:


【结果分析】

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未经允许请勿用于商业用途)

猜你在找的数据结构相关文章