队列实现C ++

时间:2017-08-14 22:34:43

标签: c++ queue implementation

您好我正在为我的作业创建一个队列,并且我不断获得" 4 4 4 4 4"。我不确定我是否亲自在纸上排队错误,或者我是否在程序中搞砸了。我只是想确认队列是否真的出来了。我包括了Enqueue和Dequeue FIles。谢谢

#include <iostream>
#include <cstdlib>
using namespace std;

const int MAX_SIZE = 100;

class QueueOverFlowException
{
public:
   QueueOverFlowException()
   {
       cout << "Queue overflow" << endl;
   }
};

class QueueEmptyException
{
public:
   QueueEmptyException()
   {
       cout << "Queue empty" << endl;
   }
};

class ArrayQueue
{
private:
   int data[MAX_SIZE];
   int front;
   int rear;
public:
   ArrayQueue()
   {
       front = -1;
       rear = -1;
   }

   void Enqueue(int element)
   {
       // Don't allow the queue to grow more
       // than MAX_SIZE - 1
       if ( Size() == MAX_SIZE - 1 )
           throw new QueueOverFlowException();

       data[rear] = element;

       // MOD is used so that rear indicator
       // can wrap around
       rear = ++rear % MAX_SIZE;
   }

   int Dequeue(int n)
   {
       if ( isEmpty() )
           throw new QueueEmptyException();

       int ret = data[front];

       // MOD is used so that front indicator
       // can wrap around
       front = ++front % MAX_SIZE;

       return ret;
   }

   int Front()
   {
       if ( isEmpty() )
           throw new QueueEmptyException();

       return data[front];
   }

   int Size()
   {
       return abs(rear - front);
   }

   bool isEmpty()
   {
       return ( front == rear ) ? true : false;
   }
};

int main()
{
   ArrayQueue q;

   int x =2;
   int y = 4;

   q.Enqueue(x);
   q.Enqueue(y);
   q.Dequeue(x);
   q.Enqueue(x+5);
   q.Enqueue(16);
   q.Enqueue(x);
   q.Enqueue(y-3);

   cout << "Queue: ";

   while(!q.isEmpty())
   {
       q.Dequeue(y);
       cout<<" " << y;
   }
}

2 个答案:

答案 0 :(得分:1)

您的代码有以下错误。

  1. 在Enqueue中,应增加第一个后部,然后插入值。
  2. 出队功能不应该采取任何论据。
  3. 在main函数中,您以错误的方式调用Dequeue函数。由于Dequeue函数返回整数,因此您应该将返回值存储在某个变量中,或者使用cout直接输出到输出屏幕。
  4. 正确的代码是:

        #include <iostream>
        #include <cstdlib>
        using namespace std;
    
        const int MAX_SIZE = 100;
    
        class QueueOverFlowException
        {
            public:
               QueueOverFlowException()
               {
                   cout << "Queue overflow" << endl;
               }
        };
    
        class QueueEmptyException
        {
            public:
               QueueEmptyException()
               {
                   cout << "Queue empty" << endl;
               }
        };
    
        class ArrayQueue
        {
            private:
               int data[MAX_SIZE];
               int front;
               int rear;
            public:
               ArrayQueue()
               {
                   front = -1;
                   rear = -1;
               }
    
           void Enqueue(int element)
           {
               // Don't allow the queue to grow more
               // than MAX_SIZE - 1
               if ( Size() == MAX_SIZE - 1 )
                   throw new QueueOverFlowException();
    
               // MOD is used so that rear indicator
               // can wrap around
               rear = ++rear % MAX_SIZE;
               data[rear] = element;
    
    
           }
    
           int Dequeue()
           {
               if ( isEmpty() )
                   throw new QueueEmptyException();
    
               int ret = data[front];
    
               // MOD is used so that front indicator
               // can wrap around
               front = ++front % MAX_SIZE;
    
               return ret;
           }
    
           int Front()
           {
               if ( isEmpty() )
                   throw new QueueEmptyException();
    
               return data[front];
           }
    
           int Size()
           {
               return abs(rear - front);
           }
    
           bool isEmpty()
           {
               return ( front == rear ) ? true : false;
           }
        };
    
        int main()
        {
           ArrayQueue q;
    
           int x =2;
           int y = 4;
    
           q.Enqueue();
           q.Enqueue(y);
           q.Dequeue();
           q.Enqueue(x+5);
           q.Enqueue(16);
           q.Enqueue(x);
           q.Enqueue(y-3);
    
           cout << "Queue: ";
    
           while(!q.isEmpty())
           {
               cout<<q.Dequeue();
           }
        }
    

答案 1 :(得分:0)

我的队列实现中有一些我觉得奇怪的事情......

将前后初始化为-1并不是一个好主意。不是那么重要,但通常的惯例是在前面(头部)添加元素并从后面(尾部)移除。

class ArrayQueue
{
private:
   int data[MAX_SIZE];
   int front;
   int rear;
public:
   ArrayQueue()
   {
       front = -1;   // Why -1 and not 0 ??  See Enqueue ....
       rear = -1;    // front == rear is enough to indicate empty queue
   }

   void Enqueue(int element)
   {
       // Don't allow the queue to grow more
       // than MAX_SIZE - 1
       if ( Size() == MAX_SIZE - 1 )
           throw new QueueOverFlowException();

       data[rear] = element;  // <-- The first time around, rear == -1 !!

       // MOD is used so that rear indicator
       // can wrap around
       rear = ++rear % MAX_SIZE;  // maybe having a private incrementPtr() function 
                                  // would be a good idea.
   }

   int Dequeue(int n)  // what is n?? what does it do?
   {
       if ( isEmpty() )
           throw new QueueEmptyException();

       int ret = data[front];  // front == -1 the first time around. !!!

       // MOD is used so that front indicator
       // can wrap around
       front = ++front % MAX_SIZE;

       return ret;
   }

   int Front()   // could be const, since returning by value.
   {
       if ( isEmpty() )
           throw new QueueEmptyException();

       return data[front];
   }

   int Size()   // should be const
   {
       return abs(rear - front);  // There is definitely a problem here

       // correct way to compute # of elements:
       return (front >= rear) ? (front - rear) : (MAX_SIZE - (rear - front));

   }

   bool isEmpty()  // should be const
   {
       return ( front == rear ) ? true : false;  // redundant: (front == rear) 
                                                 // is already a bool
   }

   // no bool isFull() const function ? 
};