在头部添加新节点

adding new nodes at head

对不起。我是 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;
}

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

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

此外,您确实将 nnext 分配给了它自己:

 head = n;
 n->next = head;

您应该在重新分配 head 之前设置 nnext

如果要设置最后一个节点的 next,请使用类似:

 n->next->next->next = NULL;

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

下面的代码可以解决你的问题,但不是写列表的好方法。

#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;
}

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

试试看:

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;
}

这段代码

    //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。

    head = n;

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

    n->next = head;

之后你重新分配了 n->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;
}