急急急!请帮忙调试我的栈啊

来源:百度知道 编辑:UC知道 时间:2024/07/03 11:15:46
#include <iostream>
using namespace std;
struct Node{
int a;
int b;
int way;
};
struct stack{
Node *base;
Node *top;
};
void Push(stack &s,int a,int b,int way){
s.top->a=a;s.top->b=b;s.top->way=way;
s.top++;
}
void Pop(stack &s){
--s.top;
}
void main(){
stack s;
s.base=new Node;s.top=new Node;
s.top=s.base;
Push(s,2,3,4);
Push(s,4,5,2);
Push(s,32,6,2);
Pop(s);
cout<<s.top->a<<s.top->b<<s.top->way<<endl;
while(s.top!=s.base){
--s.top;
if(!s.top)break;
cout<<"("<<s.top->a<<","<<s.top->b<<")"<<s.top->way<<endl;
}
}
调试正确,运行错误是怎么回事啊?

不知道你为什么这样写栈,栈顶用int就可以了,不用两个节点指针,
而且既然你用C++就可以用类来实现,而你这完全是C的写法。
我改写了一下你的代码,VC6编译通过。
#include <iostream>
using namespace std;
struct Node
{
int a;
int b;
int way;
};
struct stack
{
Node *base;
int top;
};
void Push(stack &s,int a,int b,int way)
{
// cout<<"in top:"<<s.top<<" "<<a<<b<<way<<endl;
s.base[s.top].a=a;
s.base[s.top].b=b;
s.base[s.top].way=way;
s.top++;
}
void Pop(stack &s)
{
s.top--;
cout<<s.base[s.top].a<<","<<s.base[s.top].b<<"->"<<s.base[s.top].way<<endl;

}
void main()
{
stack s;
s.base=new Node[10];
s.top=0;
Push(s,2,3,4);
Push(s,4,5,2);
Push(s,32,60,2);
Push(s,1,3,4);//new test
Pop(s);
while(s.top!=0