首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

栈的小异常初学者级别

2012-10-14 
栈的小错误菜鸟级别#include stdio.h#include stdlib.h#define STACK_INIT_SIZE 100#define STACKINC

栈的小错误菜鸟级别
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 100;
#define STACKINCRESMENT 10;
#define TRUE 1;
#define FALSE 0;
#define OK 1;
#define ERROR -1;
#define OVERFLOW -2;
typedef int Status;
typedef int SElemType;
typedef struct SqStack
{
  SElemType * base;
  SElemType * top;
  int stacksize;
}SqStack;
void InitStack(SqStack *L)
{
  (*L).base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));//20行
  if(!(*L).base)//只针对式子,因此每一个数
  exit(OVERFLOW);//22行
  (*L).top=(*L).base;
  (*L).stacksize=STACK_INIT_SIZE;
}
void DestroyStack(SqStack *L)
{
  free((*L).base);
  (*L).base=NULL;
  (*L).stacksize=0;
  (*L).top=NULL;
}
void ClearStack(SqStack *L)
{
  (*L).top=(*L).base;//记住已知的是base,因此base赋值给top
}
Status StackEmpty (SqStack *L)
{
  if((*L).base==(*L).top)
  {
  return TRUE;
  }
  else{
  return FALSE;
  }
}
int StackLength(SqStack L)
{
  return (L).top-(L).base;
}
Status GetTop(SqStack L,SElemType * e)
{
  if(L.top > L.base)//防止为空,因此要判断
  {
  *e=*(L.top-1);
  return OK;
  }else{
  return ERROR;
  }
}
void push(SqStack *L,SElemType *e)
{
  if((*L).top-(*L).base>=(*L).stacksize)
  {
  (*L).base=(SElemType *)realloc(((*L).base),((*L).stacksize+STACKINCRESMENT)*sizeof(SElemType));//65行
  if(!(*L).base)
  exit(OVERFLOW);//67行
  (*L).top=(*L).stacksize+(*L).base;
  (*L).stacksize+=STACKINCRESMENT;
  }
  *(((*L).top)++)=*e;
}
Status pop(SqStack * L,SElemType * e)
{//gettop与pop都是看是否为空
  if((*L).base==(*L).top)//如果栈不为空,要看是否为空
  return ERROR;
  *(((*L).top)--)=*e;
  return OK;
}
void StackTraverse(SqStack L,void(*visit)(SElemType))
{
  while(L.top>L.base)
  visit(*L.base++);
  printf("\n");
}
int main()
{
  SqStack L;
  SElemType e;
  unsigned N;
  InitStack(&L);
  scanf("%d",&N);
  while(N)
  {
  Push(L,(N%8));
  N=N/8;
  }
  while(!StackEmpty(L))//98行
  {
  pop(&L,&e);
  printf("%d",e);
  }
  return 0;
}
Compiling: main.c
D:\数据结构\Stack\main.c: In function 'InitStack':
D:\数据结构\Stack\main.c:20: error: expected ')' before ';' token
D:\数据结构\Stack\main.c:22: error: expected ')' before ';' token
D:\数据结构\Stack\main.c: In function 'push':
D:\数据结构\Stack\main.c:65: error: expected ')' before ';' token
D:\数据结构\Stack\main.c:67: error: expected ')' before ';' token
D:\数据结构\Stack\main.c: In function 'main':
D:\数据结构\Stack\main.c:95: warning: implicit declaration of function 'Push'
D:\数据结构\Stack\main.c:98: error: incompatible type for argument 1 of 'StackEmpty'
D:\数据结构\Stack\main.c:37: note: expected 'struct SqStack *' but argument is of type 'SqStack'
Process terminated with status 1 (0 minutes, 0 seconds)
5 errors, 1 warnings
跪求大神帮忙,拜谢,这几个错误都一样,基本都一样谢谢啦
 


[解决办法]
帮你改好了,错误还真不少。
多用引用,少用指针。
Pop出栈时,应先减小top指针,再取元素

C/C++ code
#include <stdio.h>#include <stdlib.h>#define STACK_INIT_SIZE 100#define STACKINCRESMENT 10#define TRUE 1#define FALSE 0#define OK 1#define ERROR -1#define OVERFLOW -2typedef int Status;typedef int SElemType;typedef struct SqStack{    SElemType * base;    SElemType * top;    int stacksize;}SqStack;void InitStack(SqStack &L){    L.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));//20行    if(!L.base)                //只针对式子,因此每一个数        exit(OVERFLOW);        //22行    L.top=L.base;    L.stacksize=STACK_INIT_SIZE;}void DestroyStack(SqStack *L){    free((*L).base);    (*L).base=NULL;    (*L).stacksize=0;    (*L).top=NULL;}void ClearStack(SqStack *L){    (*L).top=(*L).base;//记住已知的是base,因此base赋值给top}Status StackEmpty (const SqStack &L){    if(L.base==L.top)    {        return TRUE;    }    else{        return FALSE;    }}int StackLength(SqStack L){    return (L).top-(L).base;}Status GetTop(SqStack L,SElemType e){    if(L.top > L.base)//防止为空,因此要判断    {        e=*(L.top-1);        return OK;    }else{        return ERROR;    }}void Push(SqStack &L,SElemType e){    if(L.top-L.base>=L.stacksize)    {        L.base=(SElemType *)realloc((L.base),(L.stacksize+STACKINCRESMENT)*sizeof(SElemType));//65行        if(!L.base)            exit(OVERFLOW);       //67行        L.top=L.stacksize+L.base;        L.stacksize+=STACKINCRESMENT;    }    *(L.top++)=e;}Status Pop(SqStack &L,SElemType &e){//gettop与pop都是看是否为空    if(L.base==L.top)//如果栈不为空,要看是否为空        return ERROR;    e=*(--L.top);    return OK;}void StackTraverse(SqStack L,void(*visit)(SElemType)){    while(L.top>L.base)        visit(*L.base++);    printf("\n");}int main(){    SqStack L;    SElemType e;    unsigned int N=0;    InitStack(L);    scanf("%d",&N);    while(N)    {        Push(L,(N%8));        N=N/8;    }    while(!StackEmpty(L))//98行    {        Pop(L,e);        printf("%d",e);    }    return 0;} 

热点排行