在头部添加新节点

时间:2015-05-07 14:17:31

标签: c++ nodes

请原谅我。我是C ++的新手。我试图将第三个节点添加为头节点,并为数据初始化5,但它似乎会破坏前面的1-> 2的链接列表。

仅在输出上输出电流

5

我的预期输出将是

5
1
2

我的尝试。

#include <iostream>

struct node {
   int data;
   node *next;
};

int main(int argc, const char * argv[])
{
    node* n;
    node * head;
    node * tmp;

    //create head node.
    n = new node;
    n->data=1;
    tmp = n;
    head = n;

    //create a new node after head node and link it with head node
    n = new node;
    n->data=2;
    tmp->next=n;
    tmp=tmp->next;

    //inserting before head node
    n = new node;
    head = n;
    n->data=5;
    n->next = head;
    tmp = head;

   //end of linked list
   n->next=NULL;

   //print
   while ( head != NULL ) {
        std::cout<< head->data << std::endl;
        head = head->next;
   }
   return 0;
}

4 个答案:

答案 0 :(得分:0)

此行将第一个节点的next设置为NULL,而不是最后一个:

//end of linked list
n->next=NULL;

此外,您确实将next n分配给自己:

 head = n;
 n->next = head;

在重新分配next之前,您应该设置n的{​​{1}}。

如果要设置最后一个节点的head,请使用以下内容:

next

但是,最好使用构造函数初始化数据并编写成员函数来操作数据而不是手动操作。

答案 1 :(得分:0)

以下代码将解决您的问题,但这不是编写列表的好方法。

#include <iostream>

struct node {
   int data;
   node *next;
};

int main(int argc, const char * argv[])
{
    node* n;
    node * head;
    node * tmp;

    //create head node.
    n = new node;
    n->data=1;
    tmp = n;
    head = n;

    //create a new node after head node and link it with head node
    n = new node;
    n->data=2;
    tmp->next=n;
    tmp=tmp->next;

    //inserting before head node(the following i have changed!)
    n = new node;
    n->data=5;
    n->next = head;
    head = n;

   //end of linked list
   tmp=NULL;

   //print
   while ( head != NULL ) {
        std::cout<< head->data << std::endl;
        head = head->next;
   }
   return 0;
}

答案 2 :(得分:0)

您的代码未创建链接列表。您正在插入头部,在将现有列表链接到新节点之前覆盖头指针,从而破坏整个列表。

试试:

struct node {
  int x;
  node *next;
};

int main()
{
  node *root;       // This won't change, or we would lose the list in memory
  node *conductor;  // This will point to each node as it traverses the list

  root = new node;  // Sets it to actually point to something
  root->next = 0;   //  Otherwise it would not work well
  root->x = 12;
  conductor = root; // The conductor points to the first node
  if ( conductor != 0 ) {
    while ( conductor->next != 0)
      conductor = conductor->next;
  }
  conductor->next = new node;  // Creates a node at the end of the list
  conductor = conductor->next; // Points to that node
  conductor->next = 0;         // Prevents it from going any further
  conductor->x = 42;
}

答案 3 :(得分:0)

此代码段

    //inserting before head node
    n = new node;
    head = n;
    n->data=5;
    n->next = head;
    tmp = head;

   //end of linked list
   n->next=NULL;

没有意义。

你指定了n去头。

    head = n;

然后在节点本身的地址旁边分配数据成员,因为head已经等于n。

    n->next = head;

之后你重新分配了n-&gt; next

   n->next=NULL;

因此现在节点头的数据成员接下来等于NULL,实际上你的列表只包含头部。

程序可以按以下方式编写

#include <iostream>

struct node {
   int data;
   node *next;
};

int main(int argc, const char * argv[])
{
    node * n;
    node * head = NULL;
    node * tail = NULL;

    //create head node.
    n = new node;
    n->data = 1;
    n->next = NULL;
    tail = n;
    head = n;

    //create a new node after head node and link it with head node
    n = new node;
    n->data = 2;
    n->next = NULL;

    tail->next = n;
    tail = n;

    //inserting before head node
    n = new node;
    n->data = 5;
    n->next = head;
    head = n;

   //print
   for ( n = head; n != NULL; n = n->next ) {
        std::cout<< n->data << std::endl;
   }

   return 0;
}