2-1 栈——顺序栈(C/C++语言)

文章提供了两种顺序栈的实现,一种是将top初始化为-1,另一种是将top初始化为0。分别实现了初始化栈、判断栈是否为空、进栈、出栈、读栈顶元素和清空栈的功能。此外,讨论了共享栈的概念,通过一个数组实现两个栈来更有效地利用存储空间。
摘要由CSDN通过智能技术生成

实现的功能

void InitStack(SqStack0& S);//初始化一个栈
bool StackEmpty(SqStack0& S);//判断栈是否为空
bool Push(SqStack0& S, int x);//进栈
bool Pop(SqStack0& S, int& x);//出栈
bool GetTop(SqStack0 S, int& x);//读栈顶元素
void ClearStack(SqStack0& S);//清空栈

top在元素所在位置,即初始化top=-1

在这里插入图片描述

完整代码:

/*
    Name:SqStack0(顺序栈top为-1)
    Copyright:
    Author: Sdjzu_Nxy
    Date: 2023 年 2 月 27 日
    Description:
*/
#include<stdio.h>
#include<stdbool.h>

#define MaxSize 10			//定义最大长度

typedef struct {
    int data[MaxSize];		//用静态的“数组”存放数据元素
    int top;				//栈顶指针
    int stackSize;
}SqStack0;					//类型定义

//初始化一个栈
void InitStack(SqStack0& S) {
    S.top = -1;
    S.stackSize = 0;
}

//判断栈是否为空
bool StackEmpty(SqStack0 &S) {
    //if (S.top == -1) {
    //    return true;
    //}
    //else
    //{
    //    return false;
    //}
    //return S.stackSize == 0 ? true : false;


    return S.top == -1 ? true : false;
}

//进栈
bool Push(SqStack0& S, int x) {
    //if (S.stackSize == MaxSize)
    //{
    //    return false;
    //}

    if (S.top == MaxSize - 1)
    {
        return false;
    }

    S.data[++S.top] = x;
    S.stackSize++;
    return true;
}

//出栈
bool Pop(SqStack0& S, int& x) {
    //if (S.top == -1)
    //{
    //    return false;
    //}

    if (S.stackSize == 0)
    {
        return false;
    }

    x = S.data[S.top--];
    S.stackSize--;
    return true;
}

//读栈顶元素
bool GetTop(SqStack0 S, int& x) {
    if (S.top == -1)
    {
        return false;
    }
    x = S.data[S.top];
    return true;
}

//清空栈
void ClearStack(SqStack0& S) {
    while (S.stackSize != 0)
    {
        S.data[S.top--] = 0;
        S.stackSize--;
    }
}

int main() {
    SqStack0 S;

    InitStack(S);

    printf("The SqStack is Empty? %d\n", StackEmpty(S));

    Push(S, 1);
    Push(S, 2);
    Push(S, 3);

    int x;
    if (GetTop(S, x)) {
        printf("The Sqstack Top data is %d.\n", x);
    }

    if (Pop(S, x))
    {
        printf("Pop SqStack data is %d.\n", x);
    }

    if (Pop(S, x))
    {
        printf("Pop SqStack data is %d.\n", x);
    }

    printf("The SqStack size is %d.\n", S.stackSize);

    ClearStack(S);
    printf("The SqStack size is %d, it is clear!", S.stackSize);
}

运行结果:
在这里插入图片描述

top在元素上一个位置,即top=0

在这里插入图片描述

完整代码:

/*
    Name:SqStack0(顺序栈top为0)
    Copyright:
    Author: Sdjzu_Nxy
    Date: 2023 年 2 月 27 日
    Description:
*/
#include<stdio.h>
#include<stdbool.h>

#define MaxSize 10      //定义最大长度

typedef struct {
    int data[MaxSize];		//用静态的“数组”存放数据元素
    int top;				//栈顶指针
    int stackSize;
}SqStack1;					//类型定义

void InitStack(SqStack1& S);    //初始化一个栈
bool StackEmpty(SqStack1& S);   //判断栈是否为空
bool Push(SqStack1& S, int x);  //进栈
bool Pop(SqStack1& S, int& x);  //出栈
bool GetTop(SqStack1 S, int& x);    //读栈顶元素
void ClearStack(SqStack1& S);   //清空栈

//初始化一个栈
void InitStack(SqStack1& S) {
    S.top = 0;
    S.stackSize = 0;
}

//判断栈是否为空
bool StackEmpty(SqStack1& S) {
    //if (S.top == 0) {
    //    return true;
    //}
    //else
    //{
    //    return false;
    //}
    //return S.stackSize == 0 ? true : false;


    return S.top == 0 ? true : false;
}

//进栈
bool Push(SqStack1& S, int x) {
    //if (S.stackSize == MaxSize)
    //{
    //    return false;
    //}

    if (S.top == MaxSize)
    {
        return false;
    }

    S.data[S.top++] = x;
    S.stackSize++;
    return true;
}

//出栈
bool Pop(SqStack1& S, int& x) {
    //if (S.top == 0)
    //{
    //    return false;
    //}

    if (S.stackSize == 0)
    {
        return false;
    }

    x = S.data[--S.top];
    S.stackSize--;
    return true;
}

//读栈顶元素
bool GetTop(SqStack1 S, int& x) {
    if (S.top == 0)
    {
        return false;
    }
    x = S.data[S.top - 1];
    return true;
}

//清空栈
void ClearStack(SqStack1& S) {
    while (S.stackSize != 0)
    {
        S.data[--S.top] = 0;
        S.stackSize--;
    }
}

int main() {
    SqStack1 S;

    InitStack(S);

    printf("The SqStack is Empty? %d\n", StackEmpty(S));

    Push(S, 1);
    Push(S, 2);
    Push(S, 3);

    int x;
    if (GetTop(S, x)) {
        printf("The Sqstack Top data is %d.\n", x);
    }

    if (Pop(S, x))
    {
        printf("Pop SqStack data is %d.\n", x);
    }

    if (Pop(S, x))
    {
        printf("Pop SqStack data is %d.\n", x);
    }

    printf("The SqStack size is %d.\n", S.stackSize);

    ClearStack(S);
    printf("The SqStack size is %d, it is clear!", S.stackSize);
}

运行结果:
在这里插入图片描述

扩展:共享栈

采用一个一维数组表示两个栈,其目的是更有效的利用存储空间,如图所示:
在这里插入图片描述
top0=-1 0号栈为空;
top1=MaxSize 1号栈为空;
栈满:top0+1==top1
代码实现不再赘述。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值