顺序栈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;
}
相关推荐
NAGNIP7 小时前
一文搞懂树模型与集成模型
算法·面试
NAGNIP7 小时前
万字长文!一文搞懂监督学习中的分类模型!
算法·面试
技术狂人1687 小时前
工业大模型工程化部署实战!4 卡 L40S 高可用集群(动态资源调度 + 监控告警 + 国产化适配)
人工智能·算法·面试·职场和发展·vllm
D_FW8 小时前
数据结构第六章:图
数据结构·算法
你怎么知道我是队长8 小时前
C语言---头文件
c语言·开发语言
期待のcode8 小时前
Java虚拟机的运行模式
java·开发语言·jvm
hqwest8 小时前
码上通QT实战25--报警页面01-报警布局设计
开发语言·qt·qwidget·ui设计·qt布局控件
a程序小傲8 小时前
京东Java面试被问:动态规划的状态压缩和优化技巧
java·开发语言·mysql·算法·adb·postgresql·深度优先
HellowAmy8 小时前
我的C++规范 - 玩一个小游戏
开发语言·c++·代码规范
自学不成才8 小时前
深度复盘:一次flutter应用基于内存取证的黑盒加密破解实录并完善算法推理助手
c++·python·算法·数据挖掘