栈的实现,急啊,求大虾帮忙!
时间: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那两个地方编译不了,急需帮忙!感激涕零!
# 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那两个地方!
但是我检查过没有什么错!就是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
C/C++ code
# define STACK_INIT_SIZE 100; # define STACKINCREMENT 10;
作者: xxyxxb 发布时间: 2011-11-28
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28