栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

栈


#include

#include

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

struct Stack

{

    int len;

    int *base,*top;

};

bool empty(Stack *s){return s->base==s->top;}

int size(Stack *s)

{

    return s->top-s->base;

}

void build(Stack *s,int n)

{

    s->base=(int *)malloc(sizeof(int)*n);

    s->len=n,s->top=s->base;

}

void push(Stack *s,int val)

{

    if(s->top-s->base==s->len)return;

    *s->top++=val;

}

int pop(Stack *s)

{

    if(empty(s))return *s->top;

    return *--s->top;

}

int front(Stack *s)

{

    if(empty(s))return -1;

    return *(s->top-1);

}

void clear(Stack *s)

{

    if(s->base)free(s->base);

}

int main()

{

    Stack p;

    build(&p,5);

    for(int i=0;i<5;i++)

    {

        int x;cin>>x;

        push(&p,x);

    }

    while(!empty(&p))

    {

        cout<

    }

}

栈2

#include

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

typedef struct

{

    int *base;

    int *top;

    int stacksize;  

}SqStack;

#define Status int

#define STACK_INIT_SIZE 100

#define STACKINCREMENT 10

#define OK 1

#define ERROR 0

#define INFEASIBLE  -1

#define OVERFLOW  -2

struct STACK

{

    Status InitStack(SqStack &S)

    {

        S.base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));

        if(!S.base)exit(OVERFLOW);

        S.top=S.base;

        S.stacksize=STACK_INIT_SIZE;

        return OK;

    }

    Status GetTop(SqStack S,int &e)

    {

        if(S.top==S.base)return ERROR;

        e=*(S.top-1);return OK;

    }   

    Status Push(SqStack &S,int e)

    {

        if(S.top-S.base>=S.stacksize)

        {

            S.base=(int *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(int));

            if(!S.base)exit(OVERFLOW);

            S.top=S.base+S.stacksize;

        }

        *S.top++=e;

        return OK;

    }

    Status Pop(SqStack &S,int &e)

    {

        if(S.top==S.base)return ERROR;

        e=*--S.top;

        return OK;

    }

    int StackLength(SqStack S){return S.top-S.base;}

    Status StackEmpty(SqStack S){return S.top==S.base;}

    Status Stacktravel(SqStack S)

    {

        for(int i=0;i

        {

            printf("%d ",S.base[i]);

        }

    }

    Status DestroyStack(SqStack &S)

    {

        S.base=S.top;

    }

};

int main()

{

    SqStack node;

    STACK Stack;

    Stack.InitStack(node);

    for(int i=0;i^5;i++)

    {

        int e;cin>>e;

        Stack.Push(node,e);

    }

}

单调栈

#include

const int N=105;

int main()

{

     int d[N];int n;std::cin>>n;

     int top=0;int a[N];

     int L[N],R[N];

     for(int i=0;i>a[i];

     for(int i=0;i

     {

          while(top>0&&a[d[top]]>=a[i])top--;

          if(top=0)L[i]=0;else L[i]=top+1;

          d[top]=i;

     }

     top=0;

     for(int i=n-1;i>=0;i--)

     {

          while(top>0&&a[d[top]]>=a[i])top--;

          if(top==0)R[i]=n;else R[i]=top;

          d[top]=i;

     }

}

 

©著作权归作者所有:来自51CTO博客作者qinXpeng的原创作品,如需转载,请注明出处,否则将追究法律责任


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/232735.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号