数据结构与算法基础-学习-10-线性表之顺序栈的清理、销毁、压栈、弹栈

一、函数实现

顺序栈的其他函数实现,请看之前的博客链接《数据结构与算法基础-学习-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
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值