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

各位帮帮小弟我吧,小弟我的栈出有关问题了

2012-12-30 
各位帮帮我吧,我的栈出问题了#includestdio.h#includeiostream.h#define MAXSIZE 100typedef struct{i

各位帮帮我吧,我的栈出问题了
#include"stdio.h"
#include"iostream.h"
#define MAXSIZE 100
typedef struct
{
int top;
char data[MAXSIZE];
}SeqStack;
int Init(SeqStack *s)
{
s=new SeqStack;
if(s==NULL)
{
cout<<"error";
return 0;
}
else
{
cout<<"sucsess!";
s->top=-1;
cout<<s->top<<endl;
return 1;
}
}
int Empty(SeqStack *s)
{
if(s->top==-1)
return 1;
else
return 0;
}
int push(char w,SeqStack *s)
{
if(s->top==MAXSIZE-1)
return 0;
else
{
s->data[++s->top]=w;
return 1;
}

}
char pull(char x,SeqStack *s)
{
if(Empty(s))
return NULL;
else
{
x=s->data[s->top--];
return x;
}
}
char Top(SeqStack *s)
{
if(Empty(s))
return 0;
else return(s->data[s->top]);
}
void main()
{
char w='\0',a;
SeqStack *s;
Init(s);
//cout<<s->top;
cout<<"please enter a number";
cin>>a;
push(a,s);
pull(w,s);
cout<<w<<endl;
}
[解决办法]
调用Init(s)之后s本身的值没有变化,要用二级指针。

int Init(SeqStack **s)
{
*s=new SeqStack;
if(*s==NULL)
{
cout<<"error";
return 0;
}
else
{
cout<<"sucsess!";
(*s)->top=-1;
cout<<(*s)->top<<endl;
return 1;
}
}

热点排行