0

The issue is portrayed clearly in the display function.I wanted to display the array items in top, but I couldn't figure out a way to do so as it was constantly being incremented.

Instead of this...:
void CircularQueue :: Display() const {
    for(int i = front; i < MAX; i++) {
        cout << a[i] << endl;
    }
}
**I want to display the circular queue using top:**
void CircularQueue :: Display() const {
    for(int i = front; i < top; i++) {
        cout << a[i] << endl;
    }
}

In my main function, I enqueue'd 15 items, and as a result, top incremented 15 times. So obviously there would be 5 garbage values. How can I manipulate top so that the display function shows all 10 array values?


#include <iostream>
using namespace std;
#define MAX 10

class CircularQueue {
    private:    
        int top;
        int front;
    public:
        //assume that the max number of items in this circular queue is 10.
        int a[MAX]; 
        CircularQueue() {top = -1; front = -1;}
        int enqueue(int x);
        void dequeue();
        void peekFront() const;
        void peekBack() const;
        void Display() const;
        bool isEmpty();

};
//1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
//10,11,12,13,14,15,6,7,8,9

int CircularQueue :: enqueue(int x) {
    //Problem: The array is actually growing in size. 
    ++top;
    a[top%10] = x;   
    int y = a[top%10];
    cout << "Adding " << y << " to the queue." << endl; 
    if(top == 0) {
        front = 0;
    }
    return y;
}

void CircularQueue :: dequeue() {
    if(top < 0) {
        cout << "The queue is empty." << endl;
    } else {
        int x = a[top];
        cout << x << " will now be removed." << endl;
        for(int i = 0; i <= top - 1; i++) {
            a[i] = a[i+1];
        }
        top--;
        x = a[top];
        cout << "The last element of the queue is now: " << x << endl;
    }
}

bool CircularQueue :: isEmpty() {
    return top < 0;
}

void CircularQueue :: peekFront() const {
    if(front < 0) {
        cout << "The queue is empty." << endl;
    } else {
        int x = a[front];
        cout << "The Front value is: " << x << endl;
    }
}

void CircularQueue :: peekBack() const {
    if(top < 0) {
        cout << "The queue is empty." << endl;
    } else {
        int x;
        x = a[top];
        cout << "The back value is: " << a[top] << endl;
    }
}

void CircularQueue :: Display() const {
    for(int i = front; i < MAX; i++) {
        cout << a[i] << endl;
    }
}

int main() {
    CircularQueue Aq;
    Aq.enqueue(0); 
    Aq.enqueue(1); 
    Aq.enqueue(2); 
    Aq.enqueue(3);
    Aq.enqueue(4); 
    Aq.enqueue(5); 
    Aq.enqueue(6); 
    Aq.enqueue(7); 
    Aq.enqueue(8); 
    Aq.enqueue(9); Aq.Display();

    Aq.enqueue(10);
    Aq.enqueue(11);
    Aq.enqueue(12);
    Aq.enqueue(13);
    Aq.enqueue(14);
    Aq.enqueue(15); Aq.Display();


    return 0;
}

Expected output should be: 10,11,12,13,14,15,6,7,8,9

The array size should be 10 always. But when I keep enqueueing; The array size goes beyond 10.

TheRoadLessTaken
  • 531
  • 2
  • 7
  • 15
  • 1
    Can you please state more clearly what your issue is? You code produces the expected result at least in my test run: https://godbolt.org/z/vkJfZr – walnut Sep 07 '19 at 19:06
  • 2
    Perhaps instead of `++top;` in your `enqueue()` method, you'd prefer `top = (top+1)%MAX;` – Jeremy Friesner Sep 07 '19 at 19:26
  • 2
    I don't see this "array size" that's increasing. There is only one array (`a`) and the size of that array is necessarily constant (`MAX`). The logical size of your `CircularQueue` changes, but that's not an array size, and it's not calculated by the code in the question. – JaMiT Sep 07 '19 at 20:31
  • How do you observe this phenomenon? I can't see a way for that to happen. – Lightness Races in Orbit Sep 07 '19 at 20:43
  • Please try to avoid `using namespace std;` because it is considered bad practice. See [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721) – L. F. Sep 08 '19 at 07:39
  • Just made edits above. Essentially I wanted my display function to use 'i < top' instead of 'i < MAX'. But the top variable continues to get incremented, so it's inevitable to get garbage values. – TheRoadLessTaken Sep 10 '19 at 05:14

0 Answers0