Printing an Array in a Queue

I'm having a problem printing out the contents of an array used in a queue.

A portion of my template queue:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
    #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;
        }
    };


A portion of the main routine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    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;
    }


The code compiles fine up until it tries to print, at which point I get these errors.

    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)


I've been stuck on this for a few days now, any help would be greatly appreciated.
I think that instead of using FloatQueue.length(), you should do something like this: FloatQueue.size()

See this page, which will explain this a little bit better:
http://www.cplusplus.com/reference/stl/vector/size/
If he calls it size(), length(), or numberOfElements() doesn't really make much difference.

I think your length function is incorrect. Think about what happens if rear < front.
Your Queue class doesn't have a MakeEmpty member function so the code shouldn't even compile.

Shouldn't you set both front and rear to 0 in the constructor? Setting front to -1 could probably give various kind of bugs.

Inside the Print() function you have forgot to use % when incrementing front so if front > rear you are running outside the array until the program crashes. Also note that the Print removes all the elements in the queue because it actually changes front to equal rear.
Last edited on
Topic archived. No new replies allowed.