栈
#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 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的原创作品,如需转载,请注明出处,否则将追究法律责任



