C++链栈基本操作——出栈时不能输出出栈元素
在输入 D 时希望输出出栈的 元素,不知道哪里出错了,不要改动别的地方,只改Pop()里的或后面调用Pop的语句#include<iostream>using namespace std;
class Node
{
public:
char data;
Node *next;
};
class LinkStack
{
public:
LinkStack(){top=NULL;}
~LinkStack();
void Push(char x);
char Pop();
void Delete();
char GetTop()
{
//ToDo
return top->data ;
}
int Empty()
{
//ToDo
if (top->next = NULL)
{
return 0;
}
else
{
return 1;
}
}
private:
Node *top;
};
LinkStack::~LinkStack()
{
//ToDo
}
void LinkStack::Push(char x)
{
//ToDo
Node *s = new Node;
s->data = x;
s->next = top;
top = s;
}
char LinkStack::Pop()
{
//ToDo
Node *p = NULL;
char x;
if(top == NULL) throw "None";
x = top->data ;
p = top;
top = top->next ;
delete p;
return x;
}
void LinkStack::Delete()
{
//ToDo
LinkStack lin;
while(top != NULL)
{
lin.Pop() ;
}
}
int main()
{
LinkStack Seq;
char str,x;
while(1)
{
cin>>str;
//ToDo
if(str == 'P')
{
Seq.Push(x);
}
else if(str == 'D')
{
x = Seq.Pop();
cout<<x <<endl;
}
else if(str == 'T')
{
Seq.Delete();
}
else if(str == 'Y')
{
int flag = Seq.Empty();
if(flag)
{
cout<<"No"<<endl;
}
else
{
cout<<"Yes"<<endl;
}
}
else if(str == 'E')
{
break;
}
}
return 0;
}谢谢啦{:5_110:} x没有初始化,而且x入栈前没有赋值啊!
if(str == 'P')
{
cin >> x
Seq.Push(x);
} 看看这个帖子
页:
[1]