2012-04-16 163 views
1

我在打印队列中使用的数组内容时遇到问题。在队列中打印阵列

我的模板队列的一部分:

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

template<class T> 
class Queue 
{ 
private: 
    int front;  //front position 
    int rear;  //rear position 
    int maxQue;  //maximum number of elements in the queue 
    T* items;  //points to a dynamically-allocated array code here 
public: 
    Queue() // default constructor: Queue is created and empty 
    { 
     front = -1; 
     rear = 0; 
     maxQue = 10; 
     items = new T[maxQue]; 
    } 

    void Print() // print the value of all elements in the queue 
    { 
     while(front != rear) 
     { 
      cout<<items[front]; 
      front++; 
      if(front==rear) 
       break; 
      cout<<" - "; 
     } 
     cout<<endl; 
    } 

    void Enqueue(T add)  // insert x to the rear of the queue 
    {       // Precondition: the queue is not full 
     if(IsFull()) 
     { 
      cout<<"Queue is full!"<<endl; 
     } 
     else 
     { 
      items[rear] = add; 
      rear++; 
      rear = rear % maxQue; 
     } 
    } 

    void Dequeue(T &x) // delete the element from the front of the queue 
    {      // Precondition: the queue is not empty 
     if(!IsEmpty()) 
     { 
      front = (front+1)%maxQue; 
      x = items[front]; 
     } 
    } 

    bool IsEmpty() // test if the queue is empty 
    { 
     return (rear==front); 
    } 

    bool IsFull() // test if the queue is full 
    { 
     return ((rear+1)%maxQue==front); 
    } 

    int length() // return the number of elements in the queue 
    { 
     return abs(rear-front); 
    } 

    ~Queue() // Destructor: memory for the dynamic array needs to be deallocated 
    { 
     delete [] items; 
    } 
}; 

主程序的一部分:

int main() 
{ 
    Queue<float>FloatQueue; 
    float y; 
    FloatQueue.MakeEmpty(); 

    FloatQueue.Dequeue(y); 
    FloatQueue.Enqueue(7.1); 
    cout << "float length 3 = " << FloatQueue.length() << endl; 

    FloatQueue.Enqueue(2.3); 
    cout << "float length 4 = " << FloatQueue.length() << endl; 

    FloatQueue.Enqueue(3.1); 
    FloatQueue.Dequeue(y); 
    cout << "The float queue contains: "; 
    FloatQueue.Print(); 

    return 0; 
} 

代码编译罚款,直到它试图打印,在这一点上,我得到这些错误。

0 00000000 0x00466a7f in std::__convert_from_v() (??:??) 
1 00000000 0x00430302 in std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_float<double>() (??:??) 
2 00000000 0x00430da8 in std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put() (??:??) 
3 00000000 0x00447455 in std::ostream::_M_insert<double>() (??:??) 
4 00000000 0x00448988 in std::ostream::operator<<() (??:??) 
5 0041CB37 Queue<float>::Print(this=0x28ff00) 

我一直坚持在这几天现在,任何帮助将不胜感激。

+1

你忘了'#include '? – 2012-04-16 22:12:40

+0

哦,是的,并使用'std :: cout'和**不要**使用'#using命名空间标准;' – 2012-04-16 22:13:16

+0

我确实有#include ,我会编辑我的帖子。没有使用命名空间std有什么好处?我对C++有点新鲜。 – 2012-04-16 22:15:43

回答

0

看起来你正在实现一个固定大小的循环缓冲区。如果是的话(或者即使没有),你有几个问题:

  1. 如果秋毫出来之前比排队队列的最大尺寸更大,它不会注册为充分。
  2. 如果你的“前”指针大于你的后指针,你的打印功能将永远不会停止,并且前端将持续到MAX_INT,并可能再次循环。您没有对缓冲区的最大大小进行mod操作。
  3. 你没有析构函数,所以你每次创建和销毁这些对象时都会泄漏你的缓冲区。
  4. 您的长度功能不正确。任何时候前方比后方都大(这是一半的时间),它将会出错。这样想,当它满了,大小就会说零。

也许除此之外的其他一些事情。我会重新考虑你的设计。你很近,但你有一些数学错误。