我想用c ++创建基本的链接列表应用程序,但是我收到此错误:
这是我的节点类:
class node {
friend class graph;
int n;
node *next;
};
这是我的图表类:
class graph{
private:
node *first;
node *last;
public:
void input();
void show();
graph();
};
graph::graph(){
first = NULL;
last = NULL;
}
我想从用户插入的数字
创建节点void graph::input(){
node *n1 = new node();
std::cout << "Please Enter Number \n";
std::cin >> n1->n;
n1->next = NULL;
if (first==NULL){
first = last = n1;
}
else{
last->next = n1;
last = n1;
}
}
最后我想显示数字,但我收到错误!
void graph::show(){
node *current = first;
do {
std::cout << "///////////////\n" << current->n;
current = current->next;
}while (current->next == NULL);
}
int main() {
graph g = *new graph();
g.input();
g.show();
}
请告诉我如何解决此错误以及我收到此错误的原因?
答案 0 :(得分:0)
我已经测试了你编写的代码。正确的方法是:
void graph::show()
{
node *current = first;
do
{
std::cout << "///////////////\n" << current->n;
current = current->next;
}while (current != NULL);
}
如果while条件类似
current->next != NULL
然后它将转储只提供列表中的一个元素或者其他一些场景