一、函数实现
顺序栈的其他函数实现,请看之前的博客链接《数据结构与算法基础-学习-09-线性表之栈的理解、初始化顺序栈、判断顺序栈空、获取顺序栈长度的实现》。
1、ClearSqStack
(1)用途
清理栈的空间。只需要栈顶指针和栈底指针相等,就说明栈已经清空,后续新入栈的数据可以直接覆盖,不用实际清理数据,提升了清理效率。
(2)源码
Status ClearSqStack(SqStack* S)
{
JudgeAllNullPointer(S);
S->TopPointer = S->BasePointer;
Log("Clear SqStack : OK\n",Info);
return SuccessFlag;
}
(3)参数
参数名 | 说明 |
S | 需要清理的SqStack*类型顺序栈。 |
2、DestroyStack
(1)说明
销毁栈。释放申请的资源。
(2)源码
Status DestroyStack(SqStack* S)
{
JudgeAllNullPointer(S);
free(S->BasePointer);
S->TopPointer = NULL;
S->BasePointer = NULL;
S->SqStackMaxSize = 0;
Log("Destroy SqStack : OK\n",Info);
return SuccessFlag;
}
(3)参数
参数名 | 说明 |
S | 需要销毁的SqStack*类型顺序栈。 |
3、PushSqStack
(1)说明
压栈。判断栈是否已满,如果已满报错,反之将数据压入栈顶即可。
(2)源码
Status PushSqStack(SqStack* S, SqElemType SE)
{
JudgeAllNullPointer(S);
//判断是否栈满
if(GetSqStackLen(S) >= S->SqStackMaxSize)
{
Log("SqStack is Full, Data cannot be pushed\n",Warning);
return FailFlag;
}
//相同结构体之间,可以直接赋值。
*(S->TopPointer) = SE;
//CopySqElemType(S->TopPointer, &SE);
//printf("%p, %p\n",S->TopPointer->StudentNum, (&SE)->StudentNum);
S->TopPointer++;
Log("Push SqStack : OK\n",Info);
return SuccessFlag;
}
(3)参数
参数名 | 说明 |
S | 需要压栈的SqStack*类型顺序栈。 |
SE | 需要压入栈的SqElemType类型数据。 |
4、PopSqStack
(1)说明
弹栈。判断栈是否已空,如果是,就抛出错误。如果不是,就下移栈顶指针,将数据赋值给SE,作为传出参数。
(2)源码
Status PopSqStack(SqStack* S, SqElemType* SE)
{
JudgeAllNullPointer(S);
JudgeAllNullPointer(SE);
if(JudgeSqStackIsEmpty(S) == SuccessFlag)
{
Log("SqStack is Empty, Data cannot be poped\n",Warning);
return FailFlag;
}
S->TopPointer--;
*SE = *(S->TopPointer);
//CopySqElemType(SE,S->TopPointer);
//printf("%p, %p\n",S->TopPointer->StudentNum, SE->StudentNum);
Log("Pop SqStack : OK\n",Info);
return SuccessFlag;
}
(3)参数
参数名 | 说明 |
S | 需要初始化的SqStack*类型顺序栈。 |
SE | 需要弹出栈的SqElemType*类型数据。 |
二、虚机测试
[gbase@czg2 LinearTable_SqStack]$ make
gcc -Wall -O3 ../Log/Log.c SqStack.c main.c -o TestSqStack -I ../Log/
[gbase@czg2 LinearTable_SqStack]$ ./TestSqStack
2023-2--Info--Init SqStack : OK
2023-2--Info--Push SqStack : OK
2023-2--Info--Push SqStack : OK
2023-2--Info--Push SqStack : OK
2023-2--Info--Push SqStack : OK
2023-2--Info--Push SqStack : OK
2023-2--Info--Push SqStack : OK
2023-2--Warning--SqStack is Full, Data cannot be pushed
2023-2--Warning--SqStack is Full, Data cannot be pushed
2023-2--Debug--Judge SqStack : Not Empty
2023-2--Debug--SqStack Data :
StudentNum : X666
StudentName : Sun
StudentScore : 100
+++++++++++++++
StudentNum : X666
StudentName : Sun
StudentScore : 101
+++++++++++++++
StudentNum : X666
StudentName : Sun
StudentScore : 102
+++++++++++++++
StudentNum : X666
StudentName : Sun
StudentScore : 103
+++++++++++++++
StudentNum : X666
StudentName : Sun
StudentScore : 104
+++++++++++++++
StudentNum : X666
StudentName : Sun
StudentScore : 105
+++++++++++++++
SqStackLen : 6
SqStackMaxSize : 6
2023-2--Debug--Judge SqStack : Not Empty
2023-2--Info--Pop SqStack : OK
2023-2--Debug--SqElemType Data:
StudentNum : X666
StudentName : Sun
StudentScore : 105
2023-2--Debug--Judge SqStack : Not Empty
2023-2--Info--Pop SqStack : OK
2023-2--Debug--SqElemType Data:
StudentNum : X666
StudentName : Sun
StudentScore : 104
2023-2--Debug--Judge SqStack : Not Empty
2023-2--Info--Pop SqStack : OK
2023-2--Debug--SqElemType Data:
StudentNum : X666
StudentName : Sun
StudentScore : 103
2023-2--Debug--Judge SqStack : Not Empty
2023-2--Info--Pop SqStack : OK
2023-2--Debug--SqElemType Data:
StudentNum : X666
StudentName : Sun
StudentScore : 102
2023-2--Debug--Judge SqStack : Not Empty
2023-2--Info--Pop SqStack : OK
2023-2--Debug--SqElemType Data:
StudentNum : X666
StudentName : Sun
StudentScore : 101
2023-2--Debug--Judge SqStack : Not Empty
2023-2--Info--Pop SqStack : OK
2023-2--Debug--SqElemType Data:
StudentNum : X666
StudentName : Sun
StudentScore : 100
2023-2--Debug--Judge SqStack : Empty
2023-2--Warning--SqStack is Empty, Data cannot be poped
2023-2--Debug--Judge SqStack : Empty
2023-2--Warning--SqStack is Empty, Data cannot be poped
2023-2--Debug--SqStack Data :
SqStackLen : 0
SqStackMaxSize : 6
2023-2--Info--Clear SqStack : OK
2023-2--Info--Destroy SqStack : OK