struct _STACKDATE
{
int *Top;
int *Next; //The address of top element
int *FstPosition; //The head of address from calloc function
int CrntEmptyCnt; //The total number of empty elements
int Cnt; //Total number of elements
};
class STACK
{
private:
_STACKDATE *StackDate;
public:
STACK(int Num=15 ) //未给初始值时,栈元素默认初始化为15个
{
StackDate->Top = (int*)calloc(Num, sizeof(int));
StackDate->FstPosition = StackDate->Top;
StackDate->Cnt = Num;
StackDate->CrntEmptyCnt = Num;
}
~STACK(){ free(StackDate->FstPosition); }
void Push(int Num); //Push a number into the stack
int GetTop(); //Get a number from the top of stack
bool IsEmpty(); //To judge whether the stack is empty
void Pop(); //Remove the element at the top
};
void STACK::Push(int Num)
{
if (IsEmpty())
{
*(StackDate->Top) = Num;
StackDate->Next = (StackDate->Top);
++(StackDate->Top);
--StackDate->CrntEmptyCnt;
}
}
int STACK::GetTop()
{
return *(StackDate->Next);
}
bool STACK::IsEmpty()
{
if ((StackDate->CrntEmptyCnt)>=0)
{
return true;
}
else
{
return false;
}
}
void STACK::Pop()
{
if (StackDate->CrntEmptyCnt <= StackDate->Cnt)
{
*(StackDate->Next) = 0;
StackDate->Top = StackDate->Next;
--StackDate->Next;
++StackDate->CrntEmptyCnt;
}
}
线性表:栈的实现(源码)
最新推荐文章于 2024-05-21 21:20:55 发布