在头部添加新节点

adding new nodes at head

本文关键字:节点 新节点 头部 添加      更新时间:2023-10-16

赦免我。我是C 的新手。我试图将第三个节点添加为head节点并初始化数据,但看来它使其命令是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;
}

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

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

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

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

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

 head = n;
 n->next = head;

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

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

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

但是,最好使用构造函数来初始化数据和写作成员功能来操纵数据而不是手动进行数据。

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

尝试:

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 = n;

然后分配了节点本身地址旁边的数据成员,因为头已经等于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;
}