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

一、顺序栈的操作

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;
}
相关推荐
思捻如枫4 小时前
C++数据结构和算法代码模板总结——算法部分
数据结构·c++
小猫咪怎么会有坏心思呢5 小时前
华为OD机考 - 水仙花数 Ⅰ(2025B卷 100分)
数据结构·链表·华为od
hn小菜鸡5 小时前
LeetCode 1356.根据数字二进制下1的数目排序
数据结构·算法·leetcode
SuperCandyXu8 小时前
leetcode2368. 受限条件下可到达节点的数目-medium
数据结构·c++·算法·leetcode
lyh13449 小时前
【SpringBoot自动化部署方法】
数据结构
MSTcheng.9 小时前
【数据结构】顺序表和链表详解(下)
数据结构·链表
慢半拍iii10 小时前
数据结构——F/图
c语言·开发语言·数据结构·c++
iceslime11 小时前
旅行商问题(TSP)的 C++ 动态规划解法教学攻略
数据结构·c++·算法·算法设计与分析
witton13 小时前
美化显示LLDB调试的数据结构
数据结构·python·lldb·美化·debugger·mupdf·pretty printer
chao_78913 小时前
链表题解——环形链表 II【LeetCode】
数据结构·leetcode·链表