2015-09-25 46 views
-1

我想了解链接列表并且遇到了困难时间。我想将三个元素放在一个节点中,然后打印出多个节点。但是,我只能打印节点的第一个元素。 例如: 输入:1,2,3 输出:1个NULL链接列表的一个节点中的三个元素

struct node 
{ 
    int Intx, Inty, Intz; 
    struct node *p; 
} 

class linked 
{ 
public: 
    node* create_node(int first, int second, int third); 
    int Intx, Inty, Intz; 
    void insert(); 
    void display(); 
} 

main() 
{ 
    linked sl; 
    sl.insert(); 
    sl.display(); 
} 

node *linked::create_node(int first, int second, int third) 
{ 
    Intx = first; 
    Inty = second; 
    Intz = third; 
    struct node *temp, *p; 
    temp = new (struct node); 
    if (temp == NULL) 
    { 
     cout << "Not able to complete"; 
    } 
    else 
    { 
     temp->Intx = first, Inty = second, Intz = third; 
     temp->next = NULL; 
     return temp; 
    } 
} 

void linked::insert() 
{ 
    int Intx, Inty, Intz; 
    cout << "Enter First Element for node: "; 
    cin >> Intx; 
    cout << "Enter Second Element for node: "; 
    cin >> Inty; 
    cout << "Enter Third Element for node: "; 
    cin >> Intz; 
    struct node *temp, *s; 
    temp = create_node(Intx, Inty, Intz); 
    if (start == NULL) 
    { 
     start = temp; 
     start->next = NULL; 
    } 
    else 
    { 
     s = start; 
     start = temp; 
     start->next = s; 
    } 
    cout << "Element Inserted." << endl; 
} 

void linked::display() 
{ 
    struct node *temp; 
    cout << "Elements of list are: " << endl; 
    while (temp != NULL) 
    { 
     cout << temp->Intx, Inty, Intz; 
     temp = temp->next; 
    } 
    cout << "NULL" << endl; 
} 
+1

需要分号';'在结构和类定义之后。主要必须返回int。带有非void返回类型的函数(例如create_node)必须返回函数中所有路径的值。逗号操作符的滥用已经被覆盖在答案中。 Injblue,你需要重新回到教科书中,并在有效帮助之前完成一些基本的程序构建。谨防采取答案,您可以简单地剪切和粘贴,因为这会导致您学习成为[Cargo Cult Programmer](https://en.wikipedia.org/wiki/Cargo_cult_programming)。 – user4581301

回答

0
temp-> Intx = first, Inty = second, Intz = third; 

用逗号分隔的事情没有做什么,你认为它在这里。你应该用三句话,你必须包括在每个temp->

temp->Intx = first; 
temp->Inty = second; 
temp->Intz = third; 

如果你真的想用逗号,你可以,但你仍然需要在所有三个任务temp->

同样,您使用的是display逗号并不做你想做

cout<< temp->Intx, Inty, Intz; 

应该

cout<< temp->Intx << "," << temp->Inty << "," << temp->Intz; 

或者类似的东西,这取决于你想要的格式

什么
0

不去谈论什么是错在你的代码。我宁愿建议你理解链表列表算法背后的逻辑。这会帮助你成长。

链接Link1: Youtube tutLink 2将为您提供链接列表算法的工作原理。

  1. 由于程序是用C++编写的。链接1是一个YouTube视频教程,通过使用C++编程逐步提供Visual Studio中的每个链接列表操作。这可能有助于您理解->运营商的正确使用方法。此外,它还可以帮助您了解对象与其成员之间的关系。

  2. 尽管链接2仅有助于链接列表作为数据结构如何增长以及如何维护的理论方面。

+0

尽管我同意评估和意图,但您应该总结链接以使其成为真正的答案。目前这是一条评论,而不是回答 – user4581301

+0

好的,会这样做 –

+0

@ user4581301对不起,我会提供它作为评论,但没有足够的声望给评论。我试图解决我的答案。但是,如果你建议我会很高兴标记我的答案,以保持堆栈清洁。 –

相关问题