2015-04-05 39 views
0

我想创建一个C++基本链表应用程序,但我得到这个错误: enter image description hereC++ EXC_BAD_ACCESS链表

这里是我的节点类:

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

我测试了你写的代码。正确的方法是:

void graph::show() 
{ 
    node *current = first; 
    do 
    { 
     std::cout << "///////////////\n" << current->n; 
     current = current->next; 
    }while (current != NULL); 
} 

如果while条件就像是

current->next != NULL 

那么它将转储提供在列表中或在其他一些情况下,以及

+0

感谢的只有一个元素你回答 – Hamid 2015-04-05 11:05:13