顺序栈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;
}
相关推荐
灰子学技术5 小时前
go response.Body.close()导致连接异常处理
开发语言·后端·golang
那个村的李富贵6 小时前
CANN加速下的AIGC“即时翻译”:AI语音克隆与实时变声实战
人工智能·算法·aigc·cann
二十雨辰6 小时前
[python]-AI大模型
开发语言·人工智能·python
power 雀儿6 小时前
Scaled Dot-Product Attention 分数计算 C++
算法
Yvonne爱编码6 小时前
JAVA数据结构 DAY6-栈和队列
java·开发语言·数据结构·python
Re.不晚6 小时前
JAVA进阶之路——无奖问答挑战1
java·开发语言
你这个代码我看不懂6 小时前
@ConditionalOnProperty不直接使用松绑定规则
java·开发语言
pas1366 小时前
41-parse的实现原理&有限状态机
开发语言·前端·javascript
琹箐6 小时前
最大堆和最小堆 实现思路
java·开发语言·算法
renhongxia17 小时前
如何基于知识图谱进行故障原因、事故原因推理,需要用到哪些算法
人工智能·深度学习·算法·机器学习·自然语言处理·transformer·知识图谱