简体   繁体   English

为什么在插入C ++链接列表后不能插入节点?

[英]Why can't I insert nodes after head into a C++ linked list?

Could someone please help me identify the problem with the code below. 有人可以帮助我确定以下代码的问题。

#include <iostream>

using namespace std;

struct node
{
    int a,b;
    struct node* next=NULL;
};

node* head=NULL;

void insert(int a,int b)
{
    if(head==NULL)
    {
        head=new node;
        head->a=a;
        head->b=b;
        return;
    }
    node* cur=head;
    while(cur!=NULL)
    {
        cur=cur->next;
    }
    cur=new node;
    cur->a=a;
    cur->b=b;
    return;
}

void display()
{
    node* cur=head;
    while(cur!=NULL)
    {
        cout<<cur->a<<"\t"<<cur->b<<"\n";
        cur=cur->next;
    }

}

int main()
{
    int i;
    for(i=0;i<3;++i)
    {
        insert(i,i+1);
    }
    display();
    //cout<<h->next->a;
    return 0;
}

This is the output that I get: 这是我得到的输出:

        0        1

It seems that I can only display the head node and none after gets inserted. 看来我只能显示头节点,插入后什么也不能显示。 If I try to access the next node after head, I get a segmentation fault. 如果我尝试访问头之后的下一个节点,则会出现分段错误。 Why is that? 这是为什么?

  • While inserting, update head->next to NULL (when head is NULL) and curr->next to NULL (when some elements are already in the list) respectively. 插入时,分别将head->next更新为NULL(当head为NULL时)和curr->next为NULL(当某些元素已在列表中时)。
  • You are not linking head to curr . 您没有将headcurr链接。 To link head and curr , you can create another pointer instead to hold the new element, say new_ptr . 要链接headcurr ,可以创建另一个指针来保存新元素,例如new_ptr Keep curr such that curr->next=NULL , and then write curr->next=new_ptr . 保持currcurr->next=NULL ,然后编写curr->next=new_ptr

     void insert(int a,int b) { if(head==NULL) { head=new node; head->a=a; head->b=b; head->next=NULL; return; } node* cur=head,*new_ptr; while(cur->next!=NULL) { cur=cur->next; } new_ptr=new node; new_ptr->a=a; new_ptr->b=b; new_ptr->next=NULL; curr->next=new_ptr; return; } 

Your search code is: 您的搜索代码是:

node* cur=head;
while(cur!=NULL)
{
    cur=cur->next;
}
cur=new node;

At the end of the loop, you've found the right place to add the new node, but you overwrite that with cur = new node; 在循环的最后,您找到了添加新节点的正确位置,但是使用cur = new node;覆盖了该位置cur = new node; — so you need to use something more like: -因此您需要使用更多类似的内容:

node *new_node = new node;
new_node->a = a;
new_node->b = b;
new_node->next = nullptr;
cur->next = new_node;

Or, equivalently: 或者,等效地:

cur->next = new node;
cur->next->a = a;
cur->next->b = b;
cur->next->next = nullptr;

Even better, you'd create a constructor for the struct node class, such as: 更好的是,您将为struct node类创建一个构造struct node ,例如:

node(int a_init = 0, int b_init = 0) : a(a_init), b(b_init), next(nullptr) { }

and then: 接着:

cur->next = new node(a, b);

would do the whole initialization job. 将完成整个初始化工作。

I found out the bug .While inserting , instead of sitting in a node and checking if it is null , look 1 node ahead and check if its null. 我发现了这个错误。虽然插入而不是坐在一个节点上并检查它是否为null,但要向前看1个节点并检查它是否为null。 Because if you don't , then the list will get broken and cpp allocates memory else where rather than to the pointer of the last list node's next branch. 因为如果不这样做,则列表将损坏,并且cpp将内存分配到其他位置,而不是指向最后一个列表节点的下一个分支的指针。

Modified insert function : 修改后的插入功能:

void insert(int a,int b)
{
    if(head==NULL)
    {
        head=new node;
        head->a=a;
        head->b=b;
        head->next=NULL;
        return;
    }
    node* cur=head;
    while(cur->next!=NULL)
    {
        //cout<<cur->a<<"\t"<<cur->b<<"\n";;
        cur=cur->next;
    }
    cur->next=new node;
    cur->next->a=a;
    cur->next->b=b;
    return;
}

At the time of creation of any node , the next pointer of that node becomes null as per the definition of your node. 创建任何节点时,根据您节点的定义,该节点的下一个指针将为null

struct node
{
    int a,b;
    struct node* next=NULL;
};

Now,after the creation of start node ,the next pointer of start node is NULL .And when u created your second node ,you didn't point the next node of your first node to the second node.Then how will you be able to reach to the second node if you do not have the pointer to the second node. 现在,在创建起始节点之后,起始节点的下一个指针为NULL 。当您创建第二个节点时,您没有将第一个节点的下一个节点指向第二个节点。那么您将如何如果您没有指向第二个节点的指针,请到达第二个节点。

So the solution will be -- 因此解决方案将是-

void insert(int a,int b)
{

node *temp;
if(head==NULL)
    {
        head=new node;
        head->a=a;
        head->b=b;
       temp=head;
        return;
    }
    node* cur=head;
    while(cur!=NULL)
    {
        cur=cur->next;
    }
    cur=new node;
   temp->next=cur;
    cur->a=a;
    cur->b=b;
    temp=cur;
        return;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM