设为首页 加入收藏

TOP

顺序队列 Queue(三)
2013-11-20 14:24:18 来源: 作者: 【 】 浏览:377
Tags:顺序 队列   Queue

 

  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!"<

  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"<

  //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"<

  //print [N]

  return ;

  }

  if(!left_queue.empty()){

  cout<<"L"<

  //left must longer than right

  //print [L]

  return ;

  }

  if(left_queue.empty() && same){

  cout<<"S"<

  //left and right have the exactly same characters

  //print [S]

  return;

  }

  return;

  }

  【过程记录】

  实验截图:

  【结果分析】

  1.实验中我以课本后面的练习题为例,实现并验证了顺序队列的更种功能。

  2.队列与栈一样是在类中以数组存储数据,但由于队列先进先出的特点在实现存储形式上与栈有一定的不同。因为如果与栈一样对数据操作,队列会无限向后扩大,而前面取出过数据的地方将不会再被利用,十分浪费,也很容易溢出。所以我们采用循环数组来存储,这样合理利用了资源。但在类的实现要要极为时刻考虑周全rear和front的各种可能,要判断是不是循环到了前面,count在此时的功能也极为突出。

  3.书中的程序看似简单,但实际判断各种输出情况的时候却极难考虑周全。我首先做出了简易的流程图,然后才写函数,具体分析及思路可见我源码的注释。另外本题还有另外一种实现思路:即将左右输入分别存放在两个队列中,边取去边比较,那样在逻辑上容易理解一些。但鉴于题目的要求,我还是用边从左边队列中取出边比较右边输入的方法。

  4.我在实验中遇到的问题:

  (1)自己一开始在循环判断用的是cin.get()=='\n'即遇到回车就停止输入,但却无法如料想中结束循环……最终采用cin>>a && waiting(用以标志‘:’的输入)来作为循环终止的条件,这样虽然可以运行,但用户必须输入Ctrl+‘Z’以结束输入。看来自己对输入流的理解与掌握还没有到位。

  (2)另外在检验的时候,我发现输入‘:’之前是否输入回车情况是有区别的。如

  输入“sam:sam”(无空格),结果为“R”

  输入“sam : sam”(有空格),结构为“S”

  显然后者是我希望得到的结果,我分析可能是前面情况‘:’被列入了right的判断,从而使结构右边比左边长。还没有想到如何改进。

        

首页 上一页 1 2 3 下一页 尾页 3/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇顺序栈 Stack 下一篇通过二叉树和一个数找到路径

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·PostgreSQL 索引 - (2025-12-25 22:20:43)
·MySQL Node.js 连接 (2025-12-25 22:20:41)
·SQL 撤销索引、表以 (2025-12-25 22:20:38)
·Linux系统简介 (2025-12-25 21:55:25)
·Linux安装MySQL过程 (2025-12-25 21:55:22)