顺序栈C语言版本

cpp 复制代码
// 栈FILO-C++.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

typedef int32_t ElemType;
#define STACK_DEPTH  8

typedef struct stStack_Typedef {
    ElemType data[STACK_DEPTH];
    int top;
} stStack_Typedef;

void Init_Stack(stStack_Typedef* S)
{
    int i = 0;
     
    while (i != STACK_DEPTH) {
        S->data[i++] = 0;
    }
    S->top = -1;
}

bool IsStack_Empty(stStack_Typedef* S)
{
    if (S->top == -1) {
        return true;
    }
    return false;
}

bool IsStack_Full(stStack_Typedef* S)
{
    if (S->top == STACK_DEPTH - 1) {
        return true;
    }
    return false;
}

bool PushElem_ToStack(stStack_Typedef* S, ElemType x)
{
    if (IsStack_Full(S)) {
        return false;
    }
    S->data[S->top++] = x;
    return true;
}

bool PopElem_FromStack(stStack_Typedef* S, ElemType* x)
{
    if (S->top == -1) {
        return false;
    }
    *x = S->data[--S->top];
    if (S->top < 0) {
        S->top = -1;
        return false;
    }
    return true;
}

bool GetTopElem(stStack_Typedef* S, ElemType* x)
{
    if (S->top == -1) {
        return false;
    }
    *x = S->data[S->top];
    return true;
}

int main()
{
    stStack_Typedef S = { 0 };
    ElemType x = 0;
    bool isStackFull = false;
    bool isStackEmpty = false;

    if (!isStackFull) {
        if (PushElem_ToStack(&S, 11)) {
            printf("入栈元素:%d\r\n", 11);
        }
        else {
            printf("Stack is full!\r\n");
            isStackFull = true;
        }
    }

    if (!isStackFull) {
        if (PushElem_ToStack(&S, 22)) {
            printf("入栈元素:%d\r\n", 22);
        }
        else {
            printf("Stack is full!\r\n");
            isStackFull = true;
        }
    }

    if (!isStackFull) {
        if (PushElem_ToStack(&S, 33)) {
            printf("入栈元素:%d\r\n", 33);
        }
        else {
            printf("Stack is full!\r\n");
            isStackFull = true;
        }
    }

    if (!isStackFull) {
        if (PushElem_ToStack(&S, 44)) {
            printf("入栈元素:%d\r\n", 44);
        }
        else {
            printf("Stack is full!\r\n");
            isStackFull = true;
        }
    }

    if (!isStackFull) {
        if (PushElem_ToStack(&S, 55)) {
            printf("入栈元素:%d\r\n", 55);
        }
        else {
            printf("Stack is full!\r\n");
            isStackFull = true;
        }
    }

    if (!isStackFull) {
        if (PushElem_ToStack(&S, 66)) {
            printf("入栈元素:%d\r\n", 66);
        }
        else {
            printf("Stack is full!\r\n");
            isStackFull = true;
        }
    }

    if (!isStackFull) {
        if (PushElem_ToStack(&S, 77)) {
            printf("入栈元素:%d\r\n", 77);
        }
        else {
            printf("Stack is full!\r\n");
            isStackFull = true;
        }
    }

    if (!isStackFull) {
        if (PushElem_ToStack(&S, 88)) {
            printf("入栈元素:%d\r\n", 88);
        }
        else {
            printf("Stack is full!\r\n");
            isStackFull = true;
        }
    }

    if (!isStackFull) {
        if (PushElem_ToStack(&S, 99)) {
            printf("入栈元素:%d\r\n", 99);
        }
        else {
            printf("Stack is full!\r\n");
            isStackFull = true;
        }
    }

    if (!isStackFull) {
        if (PushElem_ToStack(&S, 123)) {
            printf("入栈元素:%d\r\n", 123);
        }
        else {
            printf("Stack is full!\r\n");
            isStackFull = true;
        }
    }

    if (!isStackEmpty) {
        if (PopElem_FromStack(&S, &x)) {
            printf("出栈元素:%d\r\n", x);
        }
        else {
            printf("Stack is empty!\r\n");
            isStackEmpty = true;
        }
    }

    if (!isStackEmpty) {
        if (PopElem_FromStack(&S, &x)) {
            printf("出栈元素:%d\r\n", x);
        }
        else {
            printf("Stack is empty!\r\n");
            isStackEmpty = true;
        }
    }

    if (!isStackEmpty) {
        if (PopElem_FromStack(&S, &x)) {
            printf("出栈元素:%d\r\n", x);
        }
        else {
            printf("Stack is empty!\r\n");
            isStackEmpty = true;
        }
    }

    if (!isStackEmpty) {
        if (PopElem_FromStack(&S, &x)) {
            printf("出栈元素:%d\r\n", x);
        }
        else {
            printf("Stack is empty!\r\n");
            isStackEmpty = true;
        }
    }

    if (!isStackEmpty) {
        if (PopElem_FromStack(&S, &x)) {
            printf("出栈元素:%d\r\n", x);
        }
        else {
            printf("Stack is empty!\r\n");
            isStackEmpty = true;
        }
    }

    if (!isStackEmpty) {
        if (PopElem_FromStack(&S, &x)) {
            printf("出栈元素:%d\r\n", x);
        }
        else {
            printf("Stack is empty!\r\n");
            isStackEmpty = true;
        }
    }

    if (!isStackEmpty) {
        if (PopElem_FromStack(&S, &x)) {
            printf("出栈元素:%d\r\n", x);
        }
        else {
            printf("Stack is empty!\r\n");
            isStackEmpty = true;
        }
    }

    if (!isStackEmpty) {
        if (PopElem_FromStack(&S, &x)) {
            printf("出栈元素:%d\r\n", x);
        }
        else {
            printf("Stack is empty!\r\n");
            isStackEmpty = true;
        }
    }

    if (!isStackEmpty) {
        if (PopElem_FromStack(&S, &x)) {
            printf("出栈元素:%d\r\n", x);
        }
        else {
            printf("Stack is empty!\r\n");
            isStackEmpty = true;
        }
    }

    if (!isStackEmpty) {
        if (PopElem_FromStack(&S, &x)) {
            printf("出栈元素:%d\r\n", x);
        }
        else {
            printf("Stack is empty!\r\n");
            isStackEmpty = true;
        }
    }

    return 0;
}
相关推荐
微风中的麦穗4 小时前
【MATLAB】MATLAB R2025a 详细下载安装图文指南:下一代科学计算与工程仿真平台
开发语言·matlab·开发工具·工程仿真·matlab r2025a·matlab r2025·科学计算与工程仿真
2601_949146535 小时前
C语言语音通知API示例代码:基于标准C的语音接口开发与底层调用实践
c语言·开发语言
开源技术5 小时前
Python Pillow 优化,打开和保存速度最快提高14倍
开发语言·python·pillow
学嵌入式的小杨同学5 小时前
从零打造 Linux 终端 MP3 播放器!用 C 语言实现音乐自由
linux·c语言·开发语言·前端·vscode·ci/cd·vim
wfeqhfxz25887825 小时前
YOLO13-C3k2-GhostDynamicConv烟雾检测算法实现与优化
人工智能·算法·计算机视觉
Aaron15886 小时前
基于RFSOC的数字射频存储技术应用分析
c语言·人工智能·驱动开发·算法·fpga开发·硬件工程·信号处理
mftang6 小时前
Python 字符串拼接成字节详解
开发语言·python
爱编码的小八嘎7 小时前
C语言对话-21.模板特化,缺省参数和其他一些有趣的事情
c语言
jasligea7 小时前
构建个人智能助手
开发语言·python·自然语言处理
kokunka7 小时前
【源码+注释】纯C++小游戏开发之射击小球游戏
开发语言·c++·游戏