Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在头部添加新节点_C++_Nodes - Fatal编程技术网

C++ 在头部添加新节点

C++ 在头部添加新节点,c++,nodes,C++,Nodes,对不起。我是C++新手。我试图添加第三个节点作为head节点,并为数据初始化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 *

对不起。我是C++新手。我试图添加第三个节点作为head节点,并为数据初始化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;
}
#包括
结构节点{
int数据;
节点*下一步;
};
int main(int argc,const char*argv[]
{
节点*n;
节点*头;
节点*tmp;
//创建头部节点。
n=新节点;
n->data=1;
tmp=n;
水头=n;
//在头节点之后创建一个新节点,并将其与头节点链接
n=新节点;
n->data=2;
tmp->next=n;
tmp=tmp->next;
//头节点前插入
n=新节点;
水头=n;
n->data=5;
n->next=头部;
tmp=头部;
//链表结束
n->next=NULL;
//印刷品
while(head!=NULL){
std::coutdata next;
}
返回0;
}

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

//end of linked list
n->next=NULL;
此外,您将
n
下一个
分配给自身:

 head = n;
 n->next = head;
在重新分配
头之前,您应该设置
n的
下一个

如果要设置最后一个节点的
下一步
,请使用以下命令:

 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;
}
#包括
结构节点{
int数据;
节点*下一步;
};
int main(int argc,const char*argv[]
{
节点*n;
节点*头;
节点*tmp;
//创建头部节点。
n=新节点;
n->data=1;
tmp=n;
水头=n;
//在头节点之后创建一个新节点,并将其与头节点链接
n=新节点;
n->data=2;
tmp->next=n;
tmp=tmp->next;
//在头部节点之前插入(以下我已更改!)
n=新节点;
n->data=5;
n->next=头部;
水头=n;
//链表结束
tmp=NULL;
//印刷品
while(head!=NULL){
std::coutdata next;
}
返回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;
没有道理

你给head分配了n

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

    n->next = head;
之后,您重新分配了n->next

   n->next=NULL;
因此,现在节点头的数据成员next等于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;
}
#包括
结构节点{
int数据;
节点*下一步;
};
int main(int argc,const char*argv[]
{
节点*n;
node*head=NULL;
node*tail=NULL;
//创建头部节点。
n=新节点;
n->data=1;
n->next=NULL;
尾=n;
水头=n;
//在头节点之后创建一个新节点,并将其与头节点链接
n=新节点;
n->data=2;
n->next=NULL;
tail->next=n;
尾=n;
//头节点前插入
n=新节点;
n->data=5;
n->next=头部;
水头=n;
//印刷品
对于(n=head;n!=NULL;n=n->next){

std::coutdata创建
InsertNode()
函数,这样您就不必重复自己的操作。您的开场白不正确。OP正在创建新节点并将它们正确插入tmp。OP的“在头插入”代码在将现有列表链接到新节点之前覆盖头指针,从而破坏整个列表。快速解释为什么将n指定给头,只需重写en即可尝试将节点放入列表并丢失列表,将大大增强此答案。