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

一、顺序栈的操作

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;
}
相关推荐
Fanxt_Ja2 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
今后1232 天前
【数据结构】二叉树的概念
数据结构·二叉树
散1122 天前
01数据结构-01背包问题
数据结构
消失的旧时光-19432 天前
Kotlinx.serialization 使用讲解
android·数据结构·android jetpack
Gu_shiwww2 天前
数据结构8——双向链表
c语言·数据结构·python·链表·小白初步
苏小瀚2 天前
[数据结构] 排序
数据结构
睡不醒的kun2 天前
leetcode算法刷题的第三十四天
数据结构·c++·算法·leetcode·职场和发展·贪心算法·动态规划
吃着火锅x唱着歌2 天前
LeetCode 978.最长湍流子数组
数据结构·算法·leetcode
Whisper_long2 天前
【数据结构】深入理解堆:概念、应用与实现
数据结构
IAtlantiscsdn2 天前
Redis7底层数据结构解析
前端·数据结构·bootstrap