3332 数据结构实验之栈五:下一较大值(一)

来源:互联网 发布:运20和伊尔76数据对比 编辑:程序博客网 时间:2024/05/19 16:27

数据结构实验之栈五:下一较大值(一)

#include<cstdio>#include<cstdlib>#include<iostream>#include<cstring>#define stackmax 110000#define stacknum 110000int x,a[100001];typedef int  Elemtype;typedef struct{    Elemtype *top;    Elemtype  *base;    int stacksize;} qstack;int Initstack(qstack &s){    s.base=(Elemtype*)malloc(stackmax*sizeof(Elemtype));    if(!s.base)        exit(0);    s.top=s.base;    s.stacksize= stackmax;    return 0;}int Pushstack(qstack &s,int n){    if(s.top-s.base>=s.stacksize)    {        s.base=(Elemtype *)realloc(s.base,(s.stacksize+stacknum)*sizeof(Elemtype));        if(!s.base)            exit(0);        s.top=s.base+s.stacksize;        s.stacksize+=stacknum;    }    *(++s.top)=n;    return 0;}/*int empty(qstack &s){    if(s.base == s.top)        return 1;    else        return 0;}*/int change(qstack &s,int n){    int i,j;    for(j=0;j<n;j++)        {            scanf("%d",&a[j]);            Pushstack(s,a[j]);        }    return 0;}int compare(qstack &s){}int Putstack(qstack &s,int n){    int m,b,f;   while(s.top>s.base)      {    b=1;x=0;          s.base++;          f=(s.top-s.base);          while(f--)          {           m=*(s.base+b);b++;              if(*(s.base)<m)             {              x=1;              break;             }          }         if(x==1) printf("%d-->%d\n",*s.base,m);        if(x==0)  printf("%d-->-1\n",*s.base);      }      printf("\n");    return 0;}int main(){    qstack s;    int n,r,t;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);          Initstack(s);        change(s,n);        Putstack(s,n);    }    return 0;}
0 0
原创粉丝点击