【问题标题】:Queue implementation C++队列实现 C++
【发布时间】:2018-01-22 20:26:43
【问题描述】:

您好,我正在为我的任务创建一个队列,并且我不断得到“4 4 4 4 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();

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

【问题讨论】:

  • 您只是在循环的每次迭代中输出 y 的值。
  • OT: throw new QueueEmptyException() 应该是throw QueueEmptyException(),你可以很容易地让QueueEmptyException 扩展std::runtime_error。异常构造函数中的cout &lt;&lt; ... 不应存在
  • int Dequeue(int n)为什么要带参数?您是否打算在实施中使用它?
  • 为什么QueueOverFlowException 不继承自std::exception?不是说它应该;只是好奇作为“为什么不”的原因..
  • rear = ++rear % MAX_SIZE; 看起来很奇怪。我可以让你感兴趣一个漂亮、易于关注的rear = (rear+1) % MAX_SIZE;吗?

标签: c++ queue implementation


【解决方案1】:

您的代码有以下错误。

  1. 在 Enqueue 中,先增加后部,然后插入值。
  2. 出队函数不应接受任何参数。
  3. 在 main 函数中,您以错误的方式调用 Dequeue 函数。由于 Dequeue 函数返回整数,因此您应该将返回值存储在某个变量中或使用 cout 直接输出到输出屏幕。

正确的代码是:

    #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 初始化器上写满了错误。
  • 我消除了这个错误。
【解决方案2】:

在您的队列实现中有几处我觉得很奇怪...

将前后初始化为-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 ? 
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-03
    • 2011-06-28
    • 2018-08-30
    • 2021-05-20
    相关资源
    最近更新 更多