Exercises 3.3 E7(原创)

来源:互联网 发布:照片拼图软件 编辑:程序博客网 时间:2024/05/29 09:31

E7    Rewrite the methods for queue processing from the text ,using a flag to indicate a full queue  instead of keeping a count of the entries in the queue

#include<iostream>
#include<queue>
using namespace std;
#include"Queue.cpp"
typedef int Queue_entry;
enum Error_code(success,underflow,overflow);
int main()
{
 const int maxqueue=10;
 class Queue
 {
 public:
  Queue();
  bool empty()const;
  Error_code serve();
  Error_code append(const Queue_entry &item);
  Error_code retrieve(Queue_entry &item)const;
 protected:
  int front,rear;
  int flag;
  Queue_entry[maxqueue];
 }
 Queue::Queue()
 {
  flag=1;
  rear=maxqueue-1;
  front=0;
 }
 bool Queue::empty()const
 {
  return flag=1;
 }
 Error_code Queue::append(const Queue_entry &item)
 {
  if(flag==1)
  {
   rear=(rear+1)%maxqueue;
   entry[rear]=item;
   return success;
  }
  else return overflow;
 }
 Error_code Queue::serve()
 {
  if(flag!=1)
  {
   front=(front+1)%maxqueue;
   return success;
  }
  else return underflow;
 }
 Error_code Queue::retrieve(Queue_entry&item)const
 {
  if(flag!=1)
  {
   item=entry[front];
   return success;
  }
  else return underflow;
 }

原创粉丝点击