在链表C中插入

在链表C中插入,c,linked-list,C,Linked List,我正在用C编写一个简单的文本编辑器。在链表中插入元素时遇到了问题 以下是我的结构: 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,*insert_node=NULL

我正在用C编写一个简单的文本编辑器。在链表中插入元素时遇到了问题

以下是我的结构:

 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,*insert_node=NULL;
    temp=(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;

            if(temp->previous==NULL) //this is for inserting at the first
            {
                   insert_node->next=temp;
                   head=insert_node;
            }

            else                     //this is for inserting in the middle.
            {
                            temp1=temp;
                temp=insert_node;
                insert_node->next=temp1;
            }

                flag=1;
                            break;
            }
                temp=temp->next;
        }

//this one's for the normal insertion and the end of the linked list.
if(flag==0)
    characters(ch,xpos,ypos);
}

第一部和第二部作品中没有任何插页。我不知道哪里出了问题。请帮助我。

插入节点在您发布的代码中始终为空


此外,您可能希望进一步拆分代码;首先在
find()
函数中隔离它的一部分。

insert\u节点
在您发布的代码中始终为空


此外,您可能希望进一步拆分代码;首先在
find()
函数中隔离它的一部分。

问题在于
insert\u node
是函数checker()中的一个局部变量,它也初始化为NULL。执行
insert\u node->c
意味着
NULL->c
,我相信您会同意我的说法,这是错误的


在使用变量之前,请尝试为变量动态分配内存,这样应该不会有问题。

问题在于,
insert\u node
是函数checker()中的一个局部变量,它也初始化为NULL。执行
insert\u node->c
意味着
NULL->c
,我相信您会同意我的说法,这是错误的

temp=(struct node *)malloc(sizeof(struct node));
temp=head;
在使用变量之前,尝试为变量动态分配内存,这样应该可以

temp=(struct node *)malloc(sizeof(struct node));
temp=head;
您正在为一个新节点分配空间,但随后您失去了这个新节点的地址分配
temp=head


您正在为一个新节点分配空间,但随后您丢失了这个新节点的地址,分配
temp=head

您的
结构在哪里?\code>??对不起,我忘了,我将更新它。在您的代码中,首先插入..插入节点->左应该为空,因为它是第一个节点now@user2624491... 我建议您重新填写您的支票条件。1:如果head=NULL,2:检查最后一个元素,3:在中间插入…您的
结构在哪里?对不起,我忘了,我会更新它。在您的代码中,首先插入..插入节点->左应该为NULL,因为它是第一个节点now@user2624491... 我建议您重新填写您的支票条件。1:如果head=NULL,2:检查最后一个元素,3:在中间插入。。。