堆栈(顺序栈)的创建,栈满与栈空判断,入栈,出栈等操作(C语言)

目录

一、堆栈的结构体定义

二、堆栈的操作

1.创建一个空栈

2.栈满与栈空判断

3.入栈操作

4.出栈操作


一、堆栈的结构体定义

//重命名,可根据不同的存储元素随时更改
typedef int ElementType;
typedef int Position;
typedef struct SNode *PtrToNode;
struct SNode{
    Position top;//确定栈顶位置
    ElementType *data;//利用数组存储元素
    int MaxSize;//栈容量
};
typedef PtrToNode Stack;

二、堆栈的操作

1.创建一个空栈

// 创建堆栈
Stack CreateStack(int MaxSize){
    Stack S = (Stack)malloc(sizeof(struct SNode));
    S->data = (ElementType*)malloc(sizeof(ElementType) * MaxSize);//申请元素内存空间
    S->top = -1;//初始化栈顶位置
    S->MaxSize = MaxSize;//初始化栈容量
    return S;
}

2.栈满与栈空判断

// 判断栈满
bool IsFull(Stack S){
    return (S->top == S->MaxSize - 1);
}

// 判断栈空
bool IsEmpty(Stack S){
    return (S->top == -1);
}

3.入栈操作

// ERROR通过define自定义,设定ERROR值为-1
// 入栈
bool Insert(Stack S, ElementType data){
    if(!IsFull(S)){
        S->data[++(S->top)] = data;
        printf("%d", S->top);
        return true;
    }
    else{
        printf("栈满!!!");
        return ERROR;
    }
}

4.出栈操作

// 出栈
ElementType Pop(Stack S){
    if(!IsEmpty(S)){
        return S->data[(S->top)--];
    }
    else{
        printf("栈空!!!");
        return ERROR;
    }
}

全部代码如下:

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

#define ERROR -1

typedef int ElementType;
typedef int Position;
typedef struct SNode *PtrToNode;
struct SNode{
    Position top;
    ElementType *data;
    int MaxSize;
};
typedef PtrToNode Stack;

// 创建堆栈
Stack CreateStack(int MaxSize){
    Stack S = (Stack)malloc(sizeof(struct SNode));
    S->data = (ElementType*)malloc(sizeof(ElementType) * MaxSize);
    S->top = -1;
    S->MaxSize = MaxSize;
    return S;
}

// 判断栈满
bool IsFull(Stack S){
    return (S->top == S->MaxSize - 1);
}

// 判断栈空
bool IsEmpty(Stack S){
    return (S->top == -1);
}

// 出栈
ElementType Pop(Stack S){
    if(!IsEmpty(S)){
        return S->data[(S->top)--];
    }
    else{
        printf("栈空!!!");
        return ERROR;
    }
}

// 入栈
bool Insert(Stack S, ElementType data){
    if(!IsFull(S)){
        S->data[++(S->top)] = data;
        printf("%d", S->top);
        return true;
    }
    else{
        printf("栈满!!!");
        return ERROR;
    }
}


int main(){
    int MaxSize;
    scanf("%d", &MaxSize);
    Stack S = CreateStack(MaxSize);
    ElementType data;
    // 入栈
    while(1){
        scanf("%d", &data);
        if(!IsFull(S)){
            Insert(S, data);
        }
        else{
            printf("栈满,无法入栈!!!");
            break;
        }
    }
    // 出栈
    while(1){
        // 注意,堆栈元素中不能出现-1,如果出现,那么出栈之后将进入下面if判断从而跳出循环!!!
        ElementType res = Pop(S);
        if(res == ERROR){
            break;
        }
        else{
            printf("%d ", res);
        }
    }
    return 0;
}

如有错误,欢迎批评指正!!!

  • 16
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
顺序栈是一种线性数据结构,它具有后进先出(LIFO)的特点。顺序栈的实现需要一个数组和一个指向顶元素的指针。以下是顺序栈的初始化、判断栈空入栈出栈操作C语言代码: ```c #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 100 typedef struct Stack { int data[MAX_SIZE]; int top; } Stack; // 初始化 void init(Stack *s) { s->top = -1; } // 判断栈空 int is_empty(Stack *s) { return s->top == -1; } // 入栈 void push(Stack *s, int x) { if (s->top == MAX_SIZE - 1) { printf("Error: stack overflow\n"); exit(1); } s->data[++s->top] = x; } // 出栈 int pop(Stack *s) { if (is_empty(s)) { printf("Error: stack underflow\n"); exit(1); } return s->data[s->top--]; } int main() { Stack s; init(&s); push(&s, 1); push(&s, 2); push(&s, 3); while (!is_empty(&s)) { printf("%d ", pop(&s)); } printf("\n"); return 0; } ``` 在上面的代码中,我们使用了一个结构体来表示,其中包含一个整型数组和一个指向顶元素的整型指针。在初始化操作中,我们将顶指针初始化为-1,表示为空。在入栈操作中,我们首先判断是否,如果了则输出错误信息并退出程序,否则将元素插入到顶。在出栈操作中,我们首先判断是否为空,如果为空则输出错误信息并退出程序,否则弹出栈顶元素并返回。在主函数中,我们初始化一个,然后依次将元素1、2、3入栈,最后将中的元素依次弹出并输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

施霁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值