/*
栈,队列,二叉树;
栈的基本操作:对于插入,删除操作叫做,(PUSH压入)以及(POP弹出),对于栈的话是先进后出,而队列的话是先进先出,对于队列的话具体操作有(入队列ENQUEUE)以及(出队列DEQUEUE)
*/
/***************栈操作*****************************/
#include <iostream>
using namespace std;
class stack
{
public:
int top;//栈顶元素位置;
int *s;//栈采用数组进行底层操作;
int size;
stack(int n):top(0),size(n){
s = new int[size+1]();
}//n个底层为int的数组
~stack()
{
delete [] s;
}
void print()
{
int i;
for (i=1;i<=top;++i)
{
cout<<s[i]<<" ";
}
cout<<endl;
}//打印元素
bool empty()
{
if(top==0)
return true;
else
return false ;
}
void push(int num)
{
if(++top>size)
{
cerr<<"stack up flow "<<endl;
exit(1);
}
s[top]=num;//第0个放弃了
}
int pop()
{
if(empty())
{
cerr<<"stack down flow"<<endl;
exit(1);
}
return s[top--];
}
};
/****************队列操作*************************/
struct queue
{
int head;
int tail;
int size;
int *q;
queue(int n):size(n),head(0),tail(0){
q=new int[size+1] ();
}
~queue(){
delete [] q;
}
void enqueue(int num)
{
q[tail]=num;
if(tail==size)
tail=1;
else
++tail;
}
int dequeue()
{
int key=q[head];
if (head=size)
head=1;
else
++head;
return key;
}
void print()
{
int i;
if(tail>=head)
{
for (i=head;i<tail;++i)
{
cout<<q[i]<<" ";
}
cout<<endl;
}else
{
for (i=1;i<tail;++i)
{
cout<<q[i]<<" ";
}
for (i=head;i<size;++i )
{
cout<<q[i]<<" ";
}
cout<<endl;
}
}
};
int main()
{
stack stak(10);
queue que(10);
stak.push(101);
stak.push(121);
que.enqueue(110);
que.enqueue(131);
stak.print();
que.print();
cout<<"-----------"<<endl;
cout<<stak.pop()<<endl;
cout<<que.dequeue()<<endl;
}