简体   繁体   English

在链表中插入一个元素

[英]insert an element in linked list

I have now known why the linked list was not updated from the previous questions. 我现在知道为什么以前的问题没有更新链表。 It turns out that I have to iterate the coordinates of x but that's not important in this question of mine. 事实证明,我必须迭代x的坐标,但这在我的问题中并不重要。

When I insert an element in the linked list the elements before where I want to insert a value disappears. 当我在链接列表中插入元素时,要插入值之前的元素消失。 For example I have elements that would print out "helo" and I want to insert another 'l' after e, the output would be "(space)ello. Here is my insertion code and structure: 例如,我有一些元素会打印出“ helo”,我想在e后面插入另一个“ l”,输出将是“(space)ello。这是我的插入代码和结构:

struct node {
struct node *previous;
int c;
int x;
int y;
struct node *next;
}*head;

void checker(int ch, int xpos, int ypos)
{
    int flag=0;
    struct node *temp,*temp1,*var,*insert_node;
    var=(struct node *)malloc(sizeof(struct node));
    temp=(struct node *)malloc(sizeof(struct node));
    insert_node=(struct node*)malloc(sizeof(struct node));
    temp=head;
    while(temp!=NULL)
    {
        if(temp->x==xpos && temp->y==ypos)
        {
            insert_node->c=ch;
            insert_node->x=xpos;
            insert_node->y=ypos;
            insert_node->next=NULL;
            temp1=temp;
                while(temp1!=NULL)
                {
                    if(temp1->y==ypos)
                    temp1->x++;
                    temp1=temp1->next;
                }
                var->next=insert_node;
                insert_node->next=temp;
                head=var;

            flag=1;
            break;
        }
            var=temp;
            temp=temp->next;
    }
    if(flag==0)
        characters(ch,xpos,ypos);
}

It seems like var has only one element inside instead of two, it takes "h" from helo forgranted. 似乎var内部只有一个元素,而不是两个,它从被允许的helo中取了“ h”。

You throw away the list from the original head up to the point you find the matching x and y in the list when you assign head = var . 分配head = var时,会将列表从原始head丢弃到找到匹配的x和y的点。 Sit down and draw a couple of pictures to convince yourself that is wrong. 坐下来画几张画,以使自己确信这是错误的。

To insert a new node before the matching node in the list: keep track of the current node in the list and the previous node visited. 要在列表中的匹配节点之前插入新节点:跟踪列表中的当前节点和之前访问的节点。 Then when you are ready to insert a new node in front of current_node, do this: 然后,当您准备在current_node前面插入新节点时,请执行以下操作:

insert_node->next = current_node;
if (previous_node == NULL)
    head = insert_node;
else
    previous_node->next = insert_node;

In your code, temp plays the role of current_node (the one you are examining). 在您的代码中, temp扮演current_node (您正在检查的角色)的角色。 You don't have a pointer to the previous node, so declare one. 您没有指向上一个节点的指针,因此声明一个。 Set current_node to head, previous_node to NULL, then start running the list, and when you find the node in the list you want to put insert_node in front of, use the code above. 将current_node设置为head,将previous_node设置为NULL,然后开始运行该列表,然后在列表中找到要放置insert_node的节点时,请使用上面的代码。 Note the special case when you want to insert at the front of the list. 要在列表的前面插入时,请注意特殊情况。 I leave it as an exercise to figure out what to do if you want to insert the new node after current_node . 我将其保留为练习,以弄清楚如果要在current_node之后插入新节点,该怎么办。

        var->next=insert_node;
        insert_node->next=temp;

should be: 应该:

        insert_node->next=temp->next;
        temp->next=insert_node;

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

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