#include<iostream>
using namespace std;
class my_stack
{
public:
my_stack(int n = 0);
virtual ~my_stack();
bool empty(void)const;
bool full(void)const;
bool push(const int& new_data);
bool pop(void);
bool get_top(int& top)const;
private:
int *a;
int max_size;
int size;
};
my_stack::my_stack(int n)
{
size = 0;
if(n <= 0)
{
a = NULL;
max_size = 0;
}
else
{
a = new int[n];
if(a == NULL)
{
max_size = 0;
}
else
{
max_size = n;
}
}
}
my_stack::~my_stack()
{
delete []a;
}
bool my_stack::empty(void)const
{
return size == 0 ? true : false;
}
bool my_stack::full(void)const
{
return size == max_size ? true : false;
}
bool my_stack::push(const int& new_data)
{
if(!full())
{
a[size++] = new_data;
return true;
}
else
{
return false;
}
}
bool my_stack::pop(void)
{
if(empty())
{
return false;
}
else
{
size--;
return true;
}
}
bool my_stack::get_top(int &top)const
{
if(empty())
{
return false;
}
else
{
top = a[size-1];
return true;
}
}
class stack_queue
{
public:
stack_queue(int n);
virtual ~stack_queue();
bool full(void)const;
bool empty(void)const;
bool push(const int& new_data);
bool pop(int &front);
private:
my_stack *s1;
my_stack *s2;
int max_size;
int size;
};
stack_queue::stack_queue(int n)
{
s1 = new my_stack(n);
s2 = new my_stack(n);
size = 0;
max_size = 2*n;
}
stack_queue::~stack_queue()
{
delete s1;
delete s2;
}
bool stack_queue::empty(void)const
{
return s1->empty() && s2->empty() ? true : false;
}
bool stack_queue::full(void)const
{
return s1->full() ? true : false;
}
bool stack_queue::push(const int& new_data)
{
if(full())
{
return false;
}
if(!s1->full())
{
s1->push(new_data);
}
if(s1->full() && s2->empty())
{
while(!s2->full() && !s1->empty())
{
int top = -1;
if(s1->get_top(top))
{
s2->push(top);
s1->pop();
}
}
}
return true;
}
bool stack_queue::pop(int& front)
{
if(empty())
{
return false;
}
if(!s2->empty())
{
int top = -1;
s2->get_top(top);
front = top;
s2->pop();
}
else
{
int top = -1;
while(!s1->empty())
{
s1->get_top(top);
front = top;
s2->push(top);
s1->pop();
}
s2->get_top(top);
s2->pop();
}
return true;
}
int main()
{
stack_queue q(5);
//入队
for(int i = 0; i < 10; i++)
{
q.push(i);
}
//出队
for(i = 0; i < 10; i++)
{
int top = -1;
if(q.pop(top))
{
cout<<top<<endl;
}
}
return 0;
}