顺序栈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;
}
相关推荐
NAGNIP3 小时前
一文搞懂深度学习中的通用逼近定理!
人工智能·算法·面试
颜酱12 小时前
单调栈:从模板到实战
javascript·后端·算法
CoovallyAIHub15 小时前
仿生学突破:SILD模型如何让无人机在电力线迷宫中发现“隐形威胁”
深度学习·算法·计算机视觉
CoovallyAIHub15 小时前
从春晚机器人到零样本革命:YOLO26-Pose姿态估计实战指南
深度学习·算法·计算机视觉
CoovallyAIHub15 小时前
Le-DETR:省80%预训练数据,这个实时检测Transformer刷新SOTA|Georgia Tech & 北交大
深度学习·算法·计算机视觉
CoovallyAIHub15 小时前
强化学习凭什么比监督学习更聪明?RL的“聪明”并非来自算法,而是因为它学会了“挑食”
深度学习·算法·计算机视觉
CoovallyAIHub16 小时前
YOLO-IOD深度解析:打破实时增量目标检测的三重知识冲突
深度学习·算法·计算机视觉
祈安_16 小时前
C语言内存函数
c语言·后端
NAGNIP1 天前
轻松搞懂全连接神经网络结构!
人工智能·算法·面试
NAGNIP1 天前
一文搞懂激活函数!
算法·面试