【数据结构】【栈】算法汇总

一、顺序栈的操作

1.准备工作

cpp 复制代码
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct{
    SElemType*base;
    SElemType*top;
    int stacksize;
}SqStack;

2.栈的初始化

cpp 复制代码
Status InitStack(SqStack &S){
    S.base=(SElemType*)malloc(MAXSIZE*sizeof(SElemType));
    if(!S.base) exit(OVERFLOW);
    S.top=S.base;
    S.stacksize=STACK_INIT_SIZE;
    return OK;
}

3.判断栈是否为空

cpp 复制代码
status StackEmpty(Sqstack S){
    if(S.top=S.base)
        return TRUE;
    else
        return FALSE;
}

4.求栈的长度

cpp 复制代码
int StackLength(SqStack S){
    return S.top-S.base;
}

5.清空栈

cpp 复制代码
Status ClearStack(SqStack S){
    if(S.base) S.top=S.base;
    return OK
}

6.销毁顺序栈

cpp 复制代码
Status DestroyStack(SqStack &S){
    if(S.base){
        delete S.base;
        S.stacksize=0;
        S.base=S.top=NULL;
    }
    return OK;
}

7.顺序栈入栈

cpp 复制代码
Status Push(SqStack &S,SElemType e){
    if(S.top-S.base>=S.stacksize){
        newbase=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
        if(!newbase) exit(overflow);
        S.base=newbase;
        S.top=S.base+S.stacksize;
        S.stacksize+=STACKINCREMENT;
    }
    *S.top++=e;
    return ok;
}

8.顺序栈的出栈

cpp 复制代码
Status Pop(SqStack &S,SElemType &e){
    if(S.top==S.base)
        return ERROR;
    e=*--S.top;
    return OK;
}

9.取栈顶元素

cpp 复制代码
Status GetTop(SqStack S,ElemType &e){
    if(S.base==S.top) return ERROR;
    E=*(S.top-1);
    return OK;
}

二、双向栈

1.准备工作

cpp 复制代码
typedef struct{
    SElemType*base;
    int top1;
    int top2;
}sqdustack;

2.初始化

cpp 复制代码
status initsqdustack(sqdustack &tws){
    tws.base=(Selemtype*)malloc(maxsize*sizeof(Selemtype));
    if(!tws.base) exit(OVERFLOW);
    tws.top1=0;
    tws.top2=maxsize-1;
    return ok;
}

3.入栈

cpp 复制代码
Status push-sqdustack(sqdustack&tws,int i,selemtype x){
    if(tws.top1>tws.top2) return error;
    if(i==0) tws.base[tws.top1++]=x;
    else tws.base[tws.top2--]=x;
    return ok;
}

4.出栈

cpp 复制代码
Status pop-sqdustack(sqdustack &tws,int i,selemtype &x){
    if(i==0){
        if(tws.top1==0) return error;
        x=tws.base[--tws.top1];
    }else{
        if(tes.top2==maxsize-1) return error;
        x=tws.base[++tws.top2];
    }
    return ok;
}

三、链栈

1.准备工作

cpp 复制代码
typedef struct StackNode{
    ElemType data;
    struct SNode*next;
}SNode,*LinkStack;
LinkStack S;

2.初始化

cpp 复制代码
void InitStack(LinkStack &S){
    S=NULL;
    return ok;
}

3.判断链栈是否为空

cpp 复制代码
Status StackEmpty(LinkStack S){
    if(S==NULL) return true;
    else return false;
}

4.链栈的入栈

cpp 复制代码
Status Push(LinkStack &S,SElemType e){
    p=(SElemType*)malloc(MAXSIZE*sizeof(SElemType));
    p->data=e;
    p->next=S;
    S=p;
    return OK;
}

5.链栈的出栈

cpp 复制代码
Status Pop(LinkStack &S,SElemType e){
    if(S==NULL) return error;
    e=S->data;
    p=S;
    S=S->next;
    free(p);
    return ok;
}

6.取栈顶元素

cpp 复制代码
SElemType GetTop(LinkStack S){
    if(S!=NULL)
        return S->data;
}
相关推荐
Darling噜啦啦12 小时前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
小小工匠2 天前
Redis - 事务机制:能实现 ACID 属性吗
数据结构·redis·性能优化·并发·持久化
玖玥拾2 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
Qres8212 天前
算法复键——树状数组
数据结构·算法
牛油果子哥q2 天前
并查集(DSU)超精讲,路径压缩、按秩合并、万能模板、连通性判定、最小生成树与刷题实战全解
数据结构·c++·最小生成树·并查集
凌波粒2 天前
LeetCode--491.递增子序列(回溯算法)
数据结构·算法·leetcode
WL学习笔记2 天前
单项不带头不循环链表
数据结构·链表
小糯米6012 天前
JS 数组
数据结构·算法·排序算法
小欣加油2 天前
leetcode3612 用特殊操作处理字符串I
数据结构·c++·算法·leetcode·职场和发展
凌波粒2 天前
LeetCode--90.子集II(回溯算法)
数据结构·算法·leetcode