I am trying to implement a Queue using 2 stacks in c++, this is what I did so fa
ID: 3642717 • Letter: I
Question
I am trying to implement a Queue using 2 stacks in c++, this is what I did so far and now I am stuck , I am not sure if I am in the right track.. Could you please help me finish this, thanks
#include <iostream>
using namespace std;
class Stack
{
public:
void stack_init(int size);
void push(int number);
int pop();
int top();
bool isEmpty( );
void enqueue(int value);
int dequeue( int value);
private:
int A [10];
int topOfStack;
int max_len;
};
Stack in;
Stack out;
void Stack::stack_init (int size)
{
max_len = size-1;
topOfStack= -1;
}
bool Stack::isEmpty()
{
return (topOfStack == -1);
}
int Stack:: pop()
{
if(!isEmpty())
return A[topOfStack--];
else
return NULL;
}
void Stack::push(int number)
{
if (topOfStack != max_len)
A[++topOfStack] = number;
}
int Stack::top()
{
return A[topOfStack];
}
void Stack:: enqueue(int value)
{
in.push(value);
}
int Stack:: dequeue(int value)
{
if(out.isEmpty())
{
while( ! in.isEmpty())
{
out.push(in.pop());
}
}
return out.pop();
}
int main()
{
Stack s;
s.enqueue(1);
s.enqueue(2);
s.enqueue(3);
s.enqueue(4);
return 0;
}