设为首页 加入收藏

TOP

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

 

  Error_code serve_and_retrieve(Queue_entry &item);

  };

  #endif

  #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

  [cpp] view plaincopyprint

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

  }

  #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

  [cpp] view plaincopyprint

  #include "queue.h"

  #include

  #include

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

  <<"First you should type in a line of characters which is supposed to consist of two parts "

  <<"separated by a colon ':' ."<

  <<"And then enter to quit put in"<

  <<"Then I will show you 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 the right parts have the same length but different"<

  <<"[S] The left and the right parts are exactly the same"<

  <<"And enter [Ctrl+'Z'] to stop input"<

  <<"So , just try it!"<

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

  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;

  }

  #include "queue.h"

  #include

  #include

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

  <<"First you should type in a line of characters which is supposed to consist of two parts "

  <<"separated by a colon ':' ."<

  <<"And then enter to quit put in"<

  <<"Then I will show you 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 the right parts have the same length but different"<

  <<"[S] The left and the right parts are exactly the same"<

  <<"And enter [Ctrl+'Z'] to stop input"<

  <<"So , just try it!"<

  return;

  }

        

首页 上一页 1 2 3 下一页 尾页 2/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)