简体   繁体   中英

I am writing c++ code to implement Queue using pointer array.Memory allocation is not happening correctly.Any suggestion?

This is my code for queue using pointer array.

#include <iostream>
#include <Queue>

using namespace std;

#define size 2

class Queue{  
public:      
    int f;
    int r;
    int* arr;
public:
    Queue(){
        arr = new int [size];
        int f, r = -1;
    }

    int isEmpty(){
        if(r == -1 && f == -1){
            cout<<"Queue underflow";
        }
        return 0;
    }

    int isFull(){
        if(r == size - 1){
            cout<<"Queue overflow";
        }
        return 0;
    }

    void enqueue(int val){
        if( isFull()){
            cout << "Cannot push element in Queue";
        }
        else{
            arr[r] = val;
            r++;
            cout << "The value in Queue is" << " " << val << endl;
        }
    }

    int dequeue(){
        int a = -1;
        if( isEmpty()){
            cout << "Cannot pop element from Queue";
            return 0;
        }
        else{
            a = arr[f]; 
            f++;
            return a;
        }
    } 
};

int main(){
    Queue q;
    q.f = q.r = 0;
    q.enqueue(1);
    q.enqueue(2);
    q.enqueue(3);
    cout << "Dequeuing element is" << q.dequeue() << endl;
    cout << "Dequeuing element is" << q.dequeue() << endl;
    cout << "Dequeuing element is" << q.dequeue() << endl;
    cout << "Dequeuing element is" << q.dequeue() << endl;
    if( q.isEmpty()){
        cout << "Queue is empty";
    }
    return 0;
}

Here is the output

The value in Queue is 1
Queue overflow The value in Queue is 2
The value in Queue is 3
Dequeuing element is1
Dequeuing element is2
Dequeuing element is3
Dequeuing element is563

Though I have set size as 2 still after printing one it says 'Queue Overflow' and then print the data also when I dequeue 4th element (though there is no forth element) it prints the line with absurd numbers.

As Nathan Pierson correctly pointed out, your functions isFull and isEmpty always return 0, which gets implicitly converted to false in an if statement, hence your checks in enqueue and dequeue are not working and the functions are always executed. Additionally, I would like to point out a few other things:

  1. Please don't use preprocessor directive to define a size variable, use constexpr instead. ( constexpr int size = 2; ). The name you have chosen (size) is very dangerous, most of the STL containers have a method also called size. Your preprocessor directive would then substitute function call with 2 , breaking compilation.
  2. You have two member variables in your Queue class: r and f . What do they mean? You check them in your methods isFull and isEmpty so I guess they are quite important. Please use descriptive names for variables (for example use front and back)
  3. Your methods isFull and isEmpty return int to indicate true or false values. In C++ there is a special type designed to indicate true or false: bool . Please use it instead of int.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM