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

一、顺序栈的操作

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;
}
相关推荐
Zachery Pole5 小时前
CCF-CSP备战NO.5链表(1)
数据结构·链表
wabs66612 小时前
关于图论【卡码网110.字符串迁移的思考】
数据结构·算法·图论
oier_Asad.Chen16 小时前
【洛谷题解/AcWing题解】洛谷P4011 孤岛营救问题/AcWing1131拯救大兵瑞恩
数据结构·笔记·学习·算法·动态规划·图论·宽度优先
冻柠檬飞冰走茶16 小时前
PTA基础编程题目集 7-19 支票面额(C语言实现)
c语言·开发语言·数据结构·算法
青山木18 小时前
Hot 100 --- 组合总和
java·数据结构·算法·leetcode
不正经学生19 小时前
二叉树递归遍历:先根与后根,递归思维的最佳试炼场
c语言·开发语言·数据结构·算法·蓝桥杯
冻柠檬飞冰走茶20 小时前
PTA基础编程题目集 7-17 爬动的蠕虫(C语言实现)
c语言·开发语言·数据结构·算法
東隅已逝,桑榆非晚21 小时前
数据结构常见英文术语速查手册
数据结构
小poop1 天前
轮转数组:从暴力到最优,一题掌握算法复杂度分析
数据结构·算法·leetcode
wabs6662 天前
关于图论【卡码网104.建造最大岛屿的思考】
数据结构·算法·图论