#include<bits/stdc++.h>
#include<malloc.h>
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<<pop(&p)<<endl;
    }
}

棧2

#include<bits/stdc++.h>
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
/*
Status InitStack(SqStack &S);//構造空棧
Status DestroyStack(SqStack &S);// 銷燬棧
Status ClearStack(SqStack &S);// 情空棧
Status StackEmpty(SqStack S);// 判斷是否爲空棧
int StackLength(SqStack S);// 棧的長度
Status GetTop(SqStack S);// 棧頂元素
Status Push(SqStack &S,int &e);// 入棧
Status Pop(SqStack &S,int &e);// 出棧
Status Stacktravel(SqStack S);// 遍歷
*/
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<StackLength(S);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<iostream>
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<n;i++)std::cin>>a[i];
     for(int i=0;i<n;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;
     }
}
 /*         for(int i=0;i<n;i++)
          {
               scanf("%lld%lld",w+i,h+i);
               sum[i+1]=sum[i]+w[i];
               while(!d1.empty()&&h[d1.top()]>=h[i])d1.pop();
               if(d1.empty())L[i]=0;else L[i]=d1.top()+1;
               d1.push(i);
          }
          for(int i=n-1;i>=0;i--)
          {
               while(!d2.empty()&&h[d2.top()]>=h[i])d2.pop();
               if(d2.empty())R[i]=n;else R[i]=d2.top();
               d2.push(i);
          }
*/


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章