+ -
当前位置:首页 → 问答吧 → 栈的实现,急啊,求大虾帮忙!

栈的实现,急啊,求大虾帮忙!

时间:2011-11-28

来源:互联网

# define TRUE 1
# define FALSE 0
# define OK 1
# define ERROR 0
# define INFEASIBLE -1
# define OVERFLOW -2
# define NULL 0
typedef int Status;

# include <stdio.h>
# include <stdlib.h>
# include <iostream>
using namespace std;

# define STACK_INIT_SIZE 100;
# define STACKINCREMENT 10;


typedef int SElemType;
typedef struct 
{
SElemType *base;
SElemType *top;
int stacksize;

}SqStack;


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 GetTop(SqStack &S,SElemType &e){
if (S.top=S.base) return ERROR;
e=*(S.top-1);
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.base ==S.top ) return ERROR;
e=*--S.top ;
return OK;
}


void main()
{SqStack S;
InitStack(S);
int n,i,e;
cout<<"please enter the number of the stack:"<<endl;
cin>>n;
cout<<"please enter the number in the stack:"<<endl;
GetTop(S,e);A
for(i=0;i<=n;++i) Push(S,e);
for(i=0;i<n;++i) Pop(S,e);
}

malloc和realloc那两个地方编译不了,急需帮忙!感激涕零!

作者: babypraver   发布时间: 2011-11-28

报什么错误信息?自己尝试分析下原因,实在不行,把错误信息贴到百度,google去查

作者: icechenbing   发布时间: 2011-11-28

用数组模拟吧.

作者: lthyxy   发布时间: 2011-11-28

试过了,报的是error C2143: syntax error : missing ')' before ';'
但是我检查过没有什么错!就是malloc和realloc那两个地方!

作者: babypraver   发布时间: 2011-11-28

C/C++ code

# define TRUE 1
# define FALSE 0
# define OK 1
# define ERROR 0
# define INFEASIBLE -1
# define OVERFLOW -2
# define NULL 0
typedef int Status;

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

#define STACK_INIT_SIZE 100     //;没有分号, # define 之间一般没有空格
#define STACKINCREMENT 10      //;


typedef int SElemType;
typedef struct  
{
    SElemType *base;
    SElemType *top;
    int stacksize;

}SqStack;


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 GetTop(SqStack &S,SElemType &e){
    if (S.top=S.base) return ERROR;
    e=*(S.top-1);
    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.base ==S.top ) return ERROR;
    e=*--S.top ;
    return OK;
}


void main()
{SqStack S;
InitStack(S);
int n,i,e;
cout<<"please enter the number of the stack:"<<endl;
cin>>n;
cout<<"please enter the number in the stack:"<<endl;
GetTop(S,e);                            //A 不知是什么
    for(i=0;i<=n;++i) Push(S,e);
for(i=0;i<n;++i) Pop(S,e);
}




编译通过,逻辑不知对不对

作者: AnYidan   发布时间: 2011-11-28

去掉下面两句后面的分号
C/C++ code

# define STACK_INIT_SIZE 100;
# define STACKINCREMENT 10;

作者: xxyxxb   发布时间: 2011-11-28