#include<iostream>
using namespace std;
class stack;
class stack_node
{
friend class stack;
public:
stack_node(int val, stack_node *p):value(val),next(p)
{}
~stack_node()
{}
private:
int value;
stack_node *next;
};
class stack
{
public:
stack()
{
stack_node *s = buynode(0,NULL);
sta = dir = s;
}
~stack()
{
while(!empty())
{
stack_node *d = dir;
dir = dir->next;
delete d;
}
delete sta;
}
stack_node* buynode(int val, stack_node *p)
{
stack_node *tmp = new stack_node(val, p);
if (tmp != 0)
{
return tmp;
}
return 0;
}
void push(int val)
{
stack_node *s = buynode(val, dir);
dir = s;
}
int pop()
{
if(!empty())
{
int tmp = dir->value;
dir = dir->next;
return tmp;
}
return -1;
}
bool empty()
{
if(dir->next != NULL)
{
return false;
}
return true;
}
private:
stack_node *sta;
stack_node *dir;
};
class queue
{
public:
queue()
{
sta1 = new stack();
sta2 = new stack();
}
~queue()
{
delete sta1;
delete sta2;
}
void appendtail(int val)
{
while(!sta1->empty())
{
sta2->push(sta1->pop());
}
sta1->push(val);
while(!sta2->empty())
{
sta1->push(sta2->pop());
}
}
int deletehead()
{
if(!sta1->empty())
{
return sta1->pop();
}
return -1;
}
private:
stack *sta1;
stack *sta2;
};
int main()
{
queue que;
for(int i=0; i<5; i++)
{
que.appendtail(i);
}
for(int i=0; i<5; i++)
{
cout<<que.deletehead()<<endl;
}
// stack sta;
// for(int i=0; i<5; i++)
// {
// sta.push(i);
// }
// for(int i=0; i<7; i++)
// {
// cout<<sta.pop()<<endl;
// }
return 0;
}
11-08
11-08