队列的示范和调试

 Queue.h

#pragma once

typedef int Queue_entry;enum Error_code{success,overflow,underflow};const int maxqueue = 10;//小值测试class Queue{public: Queue(void); bool empty() const; Error_code serve(); Error_code append(const Queue_entry &item); Error_code retrieve(Queue_entry &item)const;  ~Queue(void);protected: int count; int front,rear; Queue_entry entry[maxqueue];};Queue.cpp

#include "Queue.h"

Queue::Queue(void) //初始化队列为空{ count = 0; rear = maxqueue - 1; front = 0;}bool Queue::empty()const //如果队列为空返回true,否则返回false{ 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;}/*int Extended_queue::size()const//返回扩展的队列的条目数量{ return count;}

void  Extended_queue::clear()

{    count=0;    rear=front;Queue::~Queue(void){*/Test.cpp//主程序

#include<iostream>using namespace std;

#include"Queue.h"int main()//{ Extended_queue test_queue; introduction(); while(do_command(get_command(),test_queue));}void help(){ cout<<endl     <<"This program allows the user to enter one command"<<endl  <<"(but only one)on each input line."<<end  <<"For example,if the command s is entered,then"<<endl  "the program will serve the front of the queue"<<endl  <<endl     <<"The vaild commands are:"<<endl     <<"a - Append the next input character to the extended queue"<<endl     <<"s - Serve the front of the extended queue"<<endl     <<"r - Retrieve and print the front entry"<<endl     <<"# - The current size of the extended queue"<<endl     <<"c - Clear the extended queue (same as delete)"<<endl     <<"p - Print the extended queue"<<endl     <<"h - This help screen"<<endl     <<"q - Quit"<<endl

 <<"Press <Enter> to continue"<<flush;   char c; do{  cin.get(c); }while (c!='\n');}

void introduction(){ cout<<"press <h> for help"<<endl;

}

char get_command(){ char c; cout<<"select command and press <enter>"; cin>>c; if(c=='a'||c=='s'||c=='r'||c=='#'||c=='c'||c=='p'||c=='h'||c=='q')  return c; else  cout<<"please enter a valid command or <h> for help"<<endl;}

 


 

 

 

bool do_command(char c,Extended_queue &test_queue)
{
 bool continue_input = true;
 Queue_entry x;
 switch(c){
  case 'a'://追加下一个输入字符的扩展的队列
   cout<<"enter new data to insert";
  cin>>x;
  if(test_queue.append(x)==overflow)
   cout<<"queue is full"<<endl;

   break;

  case 's'://前面的扩展队列

   if(test_queue.serve_front() == underflow)
    cout<<"Serve failed, the queue is empty."<<endl;

   break;
  case 'r'://检索和打印前面的条目
   if(test_queue.retrieve(x) == underflow)
    cout<<"Queue is empty."<<endl;
   else
    cout<<endl<<"The first entry is:"<<x<<endl;
   break;
  case '#'://当前扩展队列的大小
   cout<<"The current size of the extended queue is:"<<endl;

test_queue.size();
  break;
  case 'c'://清除扩展队列
   test_queue.clear();
  break;
  case 'p'://打印和扩展队列

  if(Extended==Null)

   cout<<"The queue is empty"<<endl

  else
   cout<<"The Extended is:"<<endl;
   break;
  case 'h'://帮助
   help();
   break;
  case 'q'://退出
   cout<<"Extended queue demonstration finished."<<endl;
   continue_input = false;
   break;
 }
 return continue_input;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值