1

so this is a code iv'e written for an assignment, in essence it's an implementation of a queue using a stack.

this is an abstract class (class1.h):

#pragma once
#include <iostream>
class Queue
{
public:
    virtual void clear() = 0;
    virtual void enqueue(int value) = 0;
    virtual int dequeue() = 0;
    virtual int front() = 0;
    virtual bool isEmpty() const = 0;
};

this is the actual implementation (class2.h)

#pragma once
#include"class1.h"
#include<stack>
using namespace std;
class QueueStack : public Queue
{
protected:
    stack<int>* data;
public:
    QueueStack();//ctor
    ~QueueStack();//dtor
    void clear();//empties queue
    int dequeue();//dequeues
    void enqueue(int value);//enqueues
    int front();//returns front of stack
    bool isEmpty() const;//is empty
};

and the cpp functions of class2 (class2.cpp)

#include"class2.h"
void QueueStack::clear()
{
    if (!data)
    {
        throw runtime_error("error");
    }
    while (this->data->empty())
    {
        data->pop();
    }
}
int QueueStack::dequeue()
{
    stack<int> temp;
    int temp2;
    while (!data->empty())
    {
        temp.push(data->top());
        data->pop();
    }
    temp2 = temp.top();
    temp.pop();
    while (!temp.empty())
    {
        data->push(temp.top());
        temp.pop();
    }
    return temp2;
}
void QueueStack::enqueue(int num)
{
    data->push(num);
}
int QueueStack::front()
{
    stack<int> temp;
    int temp2;
    while (!data->empty())
    {
        temp.push(data->top());
        data->pop();
    }
    temp2 = temp.top();
    while (!temp.empty())
    {
        data->push(temp.top());
        temp.pop();
    }
    return temp2;
}
bool QueueStack::isEmpty() const
{
    return (data->empty());
}
QueueStack::QueueStack()
{
    data = new stack<int>;
}
QueueStack::~QueueStack()
{
    if (data)
    {
        delete data;
    }
}

and finally this is my main:

#include"class1.h"
#include"class2.h"
int main()
{
    int j;
    Queue* Q;
    Q = new QueueStack();
    try
    {
        for (int i = 0; i < 10; i++)
            Q->enqueue(i);
    }
    catch (runtime_error err)
    {
        cout << err.what();
    }

    cout << "first on Q is: " << Q->front() << endl;
    cout << "take out 2 elemets:" << endl;
    cout << Q->dequeue() << ' ' << Q->dequeue() << endl;
    cout << "first on Q is: " << Q->front() << endl;

    Q->enqueue(8);
    Q->enqueue(9);
    while (!Q->isEmpty())
        cout << Q->dequeue() << " ";
    return 0;
}

now if i run this main as is , i get the following output:

first on Q is: 0
take out 2 elemets:
1 0
first on Q is: 2
2 3 4 5 6 7 8 9 8 9

but if i replace

cout << "first on Q is: " << Q->front() << endl;
cout << "take out 2 elemets:" << endl;
cout << Q->dequeue() << ' ' << Q->dequeue() << endl;
cout << "first on Q is: " << Q->front() << endl;

with

 cout << "first on Q is: " << Q->front() << endl
         << "take out 2 elemets:" << endl
         << Q->dequeue() << ' ' << Q->dequeue() << endl
         << "first on Q is: " << Q->front() << endl;

now the output changes to:

first on Q is: 2
take out 2 elemets:
1 0
first on Q is: 0
2 3 4 5 6 7 8 9 8 9

the only difference is that instead of using one cout command, i divide it into 5 different cout commands. anyone got a clue why this happens?

user73
  • 11
  • 1

0 Answers0