顺序栈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;
}
相关推荐
高洁012 小时前
知识图谱构建
人工智能·深度学习·算法·机器学习·知识图谱
成为大佬先秃头2 小时前
渐进式JavaScript框架:Vue
开发语言·javascript·vue.js
yugi9878382 小时前
基于MATLAB实现神经网络电能扰动信号特征识别
开发语言·神经网络·matlab
追光天使2 小时前
元组、列表、字符串、字典定义及切割
开发语言·python
AndrewHZ2 小时前
【图像处理基石】什么是神经渲染?
图像处理·人工智能·神经网络·算法·cnn·计算机图形学·神经渲染
2401_841495642 小时前
【LeetCode刷题】爬楼梯
数据结构·python·算法·leetcode·动态规划·滑动窗口·斐波那契数列
guygg882 小时前
一维信号模糊熵(Fuzzy Entropy)计算原理与MATLAB实现
开发语言·matlab
byzh_rc2 小时前
[模式识别-从入门到入土] 组合分类器
人工智能·算法·机器学习·支持向量机·概率论
今夕资源网2 小时前
go-tcnat内网端口映射 端口穿透 GO语言 免费开源
开发语言·后端·golang·go语言·端口映射·内网端口映射