I am trying to create a queue class and then allow the user to input integers at the back of the queue and take them from the front. This is what I have so far, it runs but will not output the size of the queue correctly. Any help would be much appreciated.

# include <iostream>
# include <conio.h>
# include <cstdlib>

using namespace std;

class queue
{
int a[100];
int front;
int rear;
public:
queue();
~queue();
void push(int i);
int pop();
int isempty();
int isfull();
int holds();
};

queue::queue()
{
front=0;
rear=0;
}
queue::~queue()
{
delete []a;
}
void queue::push(int i)
{
if(isfull())
{
cout<<"******Queue is FULL !!!No pushion allowed further.******";
return;
}
a[rear] = i;
rear++;
}
int queue::pop()
{
if(isempty())
{
cout<<"******Queue Empty !!!Value returned will be garbage.******";
return (-9999);
}

return(a[front++]);
}

int queue::isempty()
{
if(front == rear)
return 1;
else
return 0;
}
int queue::isfull()
{
if(rear == 100)
return 1;
else
return 0;
}
int queue::holds()
{
    return front;
}

int main() {
    queue a;
    int str;
    int choice;

    do {
        cout << "Queue holds " << a.holds() << " strings\n";
        cout << "(0) Push, (1) Pop, (2) Exit : ";
        cin >> choice;
        if (choice == 0) {
            cout << "Input string: ";
            cin >> str;
            a.push(str);
        } else if (choice == 1) {
            cout << a.holds() << " : " << a.pop() << "\n";
        }
     } while (choice != 2);

    return 0;
}

Recommended Answers

All 3 Replies

The front/rear is a little confusing.

If I push two items, rear will increase by 2. front remains unchanged, but in holds() you output front.

So, that explains why holds() gives the wrong size?

I am trying to create a queue class and then allow the user to input integers at the back of the queue and take them from the front. This is what I have so far, it runs but will not output the size of the queue correctly. Any help would be much appreciated.

# include <iostream>
# include <conio.h>
# include <cstdlib>

using namespace std;

class queue
{
int a[100];
int front;
int rear;
public:
queue();
~queue();
void push(int i);
int pop();
int isempty();
int isfull();
int holds();
};

queue::queue()
{
front=0;
rear=0;
}
queue::~queue()
{
delete []a;
}
void queue::push(int i)
{
if(isfull())
{
cout<<"******Queue is FULL !!!No pushion allowed further.******";
return;
}
a[rear] = i;
rear++;
}
int queue::pop()
{
if(isempty())
{
cout<<"******Queue Empty !!!Value returned will be garbage.******";
return (-9999);
}

return(a[front++]);
}

int queue::isempty()
{
if(front == rear)
return 1;
else
return 0;
}
int queue::isfull()
{
if(rear == 100)
return 1;
else
return 0;
}
int queue::holds()
{
    return front;
}

int main() {
    queue a;
    int str;
    int choice;

    do {
        cout << "Queue holds " << a.holds() << " strings\n";
        cout << "(0) Push, (1) Pop, (2) Exit : ";
        cin >> choice;
        if (choice == 0) {
            cout << "Input string: ";
            cin >> str;
            a.push(str);
        } else if (choice == 1) {
            cout << a.holds() << " : " << a.pop() << "\n";
        }
     } while (choice != 2);

    return 0;
}

You seem to be using the value of 'front' to give you the queue size. Shouldn't it be the difference between front and rear? Also you have a method which deletes an array that was not created with the 'new' keyword. I guess you are using the array for experiment only, your queue is gradually moving further away from the 'door'. As the values are added and removed the start and end move along the array. The array may not be full when rear == 100. In fact it would be out of bounds - rear should only go to 99 (int a[100] is 0-99). Hopefully someone else will add comment to this but you might want to look up how to create a linked list and increment and decrement a simple counter.

You seem to be using the value of 'front' to give you the queue size. Shouldn't it be the difference between front and rear? Also you have a method which deletes an array that was not created with the 'new' keyword. I guess you are using the array for experiment only, your queue is gradually moving further away from the 'door'. As the values are added and removed the start and end move along the array. The array may not be full when rear == 100. In fact it would be out of bounds - rear should only go to 99 (int a[100] is 0-99). Hopefully someone else will add comment to this but you might want to look up how to create a linked list and increment and decrement a simple counter.

Thanks, that was pretty helpful. I managed to figure it out, there were a good few things wrong with my code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.