栈和队列相关

栈的顺序存储结构

复制代码
#define maxsize 50
typedef int ElemType;
typedef struct {
    ElemType data[maxsize];
    int top;
}SqStack;

顺序栈的基本算法

初始化栈

cpp 复制代码
void InitStack(SqStack *s){
    s.top=-1;
    return;
}

判断栈空

cpp 复制代码
bool IsEmptyStack(SqStack *s){
    if(s.top==-1){
        return true;
    }else{
        return false;
    }
}

进栈

cpp 复制代码
bool Push(SqStack *s,ElemType e){
    if(s.top==maxsize-1){           //栈满
        return false;
    }else{
        s.top++;
        s.data[s.top]=e;
        return true;
    }
}

出栈

cpp 复制代码
bool Pop(SqStack *s,ElemType &e){
    if(IsEmptyStack(s)){           //栈空
        return false;
    }else{
        e=s.data[s.top];
        s.top--;
        return true;
    }
}

读栈顶元素

cpp 复制代码
bool GetTop(SqStack *S,ElemType &e){
    if(IsEmptyStack(s)){           //栈空
        return false;
    }else{
        e=s.data[s.top];
        return true;
    }
}

总代码

cpp 复制代码
#include <stdio.h>

#define maxsize 50
typedef int ElemType;
typedef struct {
    ElemType data[maxsize];
    int top;
}SqStack;

void InitStack(SqStack *s){
    s.top=-1;
    return;
}

bool IsEmptyStack(SqStack *s){
    if(s.top==-1){
        return true;
    }else{
        return false;
    }
}

bool Push(SqStack *s,ElemType e){
    if(s.top==maxsize-1){           //栈满
        return false;
    }else{
        s.top++;
        s.data[s.top]=e;
        return true;
    }
}

bool Pop(SqStack *s,ElemType &e){
    if(IsEmptyStack(s)){           //栈空
        return false;
    }else{
        e=s.data[s.top];
        s.top--;
        return true;
    }
}

bool GetTop(SqStack *S,ElemType &e){
    if(IsEmptyStack(s)){           //栈空
        return false;
    }else{
        e=s.data[s.top];
        return true;
    }
}
int main() {
    return 0;
}
相关推荐
(●—●)橘子……4 小时前
记力扣2009:使数组连续的最少操作数 练习理解
数据结构·python·算法·leetcode
iナナ4 小时前
Java优选算法——位运算
java·数据结构·算法·leetcode
Han.miracle5 小时前
数据结构二叉树——层序遍历&& 扩展二叉树的左视图
java·数据结构·算法·leetcode
筱砚.5 小时前
【数据结构——最小生成树与Kruskal】
数据结构·算法
蒙奇D索大7 小时前
【数据结构】考研数据结构核心考点:平衡二叉树(AVL树)详解——平衡因子与4大旋转操作入门指南
数据结构·笔记·学习·考研·改行学it
im_AMBER8 小时前
数据结构 04 栈和队列
数据结构·笔记·学习
CAU界编程小白10 小时前
数据结构系列之堆
数据结构·c
Excuse_lighttime11 小时前
只出现一次的数字(位运算算法)
java·数据结构·算法·leetcode·eclipse
liu****11 小时前
笔试强训(二)
开发语言·数据结构·c++·算法·哈希算法
Syntech_Wuz13 小时前
从 C 到 C++:容器适配器 std::stack 与 std::queue 详解
数据结构·c++·容器··队列