回 帖 发 新 帖 刷新版面

主题:[讨论]为什么啊

大家帮忙看下这个程序怎么不行啊,感觉没什么问题啊
#include<stdio.h>
#include<malloc.h>
#include<conio.h>

#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OK 1
#define EQUAL 1
#define OVERFLOW -1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

typedef int Status ;
typedef char SElemType;

struct STACK
{
  SElemType *base;
  SElemType *top;
  int stacksize;
};

typedef struct STACK SqStack;
typedef struct STACK *pSqstack;

Status InitStack(SqStack *S)
{
  S->base=(SElemType *)malloc(STACK_INIT_SIZE *sizeof(SElemType));
  if(!S->base)exit(OVERFLOW);
  S->top=S->base;
  S->stacksize=STACK_INIT_SIZE;
  return OK;
}

Status Push(SqStack S,SElemType e)
{
  if(S.top - S.base>=S.stacksize)
   {
     S.base=(SElemType *) realloc(S.base,
        (S.stacksize + STACKINCREMENT) * sizeof(SElemType));
     if(!S.base)exit(OVERFLOW);
     S.top=S.base+S.stacksize;
     S.stacksize += STACKINCREMENT;
   }
  *(S.top++)=e;
  return OK;
}

Status Pop(SqStack S,SElemType *e)
{
  if(S.top==S.base) return ERROR;
  *e=*(--S.top);
  return OK;
}

main()
{
    int i;
    char s[10];
    char c;
    SqStack S;
    InitStack(&S);
    for(i=0;s[i]=getchar()!='\n';i++)
    for(i=0;s[i]!='\0';i++)
    Push(S,s[i]);
    printf("\nStack:");
    while(S.top!=S.base)
    {
        Pop(S,&c);
        printf("%c",c);
    }
}

回复列表 (共3个回复)

沙发

大家帮忙看下啊,这么个简单程序怎么就不行呢,那个S.base应该是一直不变的啊,可我在调试时发现它一直在变,而且打印没结果,为什么啊

板凳

struct stack
{
    BiTree *base;
    BiTree *top;
    int stacksize;
};
//初始化栈
void initstack(stack & p)
{
    p.base=(BiTree *)malloc(sizeof(BiTree)*init_size);
    if(!p.base) exit(1);
    p.top=p.base;
    p.stacksize=init_size;
}

//判断栈空
bool emptystack(stack p)
{
    if(p.base==p.top)
        return(1);
    else
        return(0);
}

//返回栈的长度
int stack_length(stack p)
{
    return(p.top-p.base);
}




//若栈不空,返回栈的栈顶元素,并返回ok;否则返回error
bool gettop(stack p,BiTree & e)
{
    if(p.top!=p.base)
    {
        e=*(p.top-1);
        return(ok);
    }
    else
        return(error);
}







//插入元素e为栈顶元素
bool push(stack &p,BiTree e)
{
    if(stack_length(p)==p.stacksize)
    {
        p.base=(BiTree *)realloc(p.base,sizeof(BiTree)*(init_size+add_size));
        if(!p.base) exit(1);
        p.stacksize+=add_size;
        *(p.top)=e;
        p.top++;
    }
    else
    {
        *(p.top)=e;
        (p.top)++;
    }
    return(ok);
}
        




//若栈不空则删除栈顶元素,用e返回它的值,并返回ok;否则返error
bool pop(stack &p,BiTree & e)
{
    if(!emptystack(p))
    {
        e=*(p.top-1);
        p.top--;
        return(ok);
    }
    else
        return(error);
}

3 楼

你把BiTree改为elemtype即可

我来回复

您尚未登录,请登录后再回复。点此登录或注册