#include "stdlib.h"
#include "stdio.h"
#include "assert.h"
#define BOT -1
#define MGN 1
class DbStk{
int top_1;
int top_2;
int C;
int* Stk;
DbStk(int C)
:C(C),
top_1(BOT),
top_2(C)
{
Stk = (int*)malloc(this->C*sizeof(int));
}
~DbStk()
{
free(Stk);
}
void Push(int Tag,int elem)
{
assert(("[error]:Stk Full",top_2-top_1<=MGN));
if(Tag)
this->Stk[top_1++] = elem;
else
this->Stk[top_2--] = elem;
}
int Pop(int Tag)
{
switch(Tag)
{
case 1:
assert(("[error]:Stk Empty",this->top_1 == BOT));
return this->Stk[this->top_1--];
break;
case 2:
assert(("[error]:Stk Empty",this->top_2 == this->C));
return this->Stk[this->top_2++];
}
}
};
双向顺序栈(内存压缩优化)
最新推荐文章于 2024-05-30 20:15:33 发布