栈和队列相关

栈的顺序存储结构

复制代码
#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;
}
相关推荐
2601_967980112 小时前
Python中heapq模块的各种用法实例
数据结构·python·算法·应用实例·heapq模块
zaizai10073 小时前
算法(左程云)2
数据结构
漂流瓶jz3 小时前
UVA-1615 高速公路 题解答案代码 算法竞赛入门经典第二版
数据结构·算法·贪心算法·贪心·aoapc·算法竞赛入门经典·uva
夊4 小时前
数据结构第四课:栈与队列
数据结构
凌冰_13 小时前
数据结构 - 时间空间复杂度(一)
数据结构·算法·软件工程
郝学胜-神的一滴20 小时前
Effective Python 条款36:优雅拼装迭代器与生成器
开发语言·数据结构·人工智能·python·numpy·scikit-learn
console.log('npc')1 天前
Python 学习教程 · 第4课:数据结构
数据结构·python·学习
郝学胜-神的一滴2 天前
Effective STL 条款03:容器中的对象复制要廉价且正确
开发语言·数据结构·c++·算法·rpc·stl
FR02EN7772 天前
[LeetCode] 26、80.删除有序数组中的重复项 题解
数据结构·算法
H.莓飛3 天前
【C++】【STL】stack_OJ题
服务器·c语言·开发语言·数据结构·c++·算法·visual studio