我正在使用C语言编写一个简单的文本编辑器。我在插入元素时遇到了麻烦。插入链接列表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);
}
无。我不知道哪里出了问题。请帮帮我。
你的结构在哪里? – someone
opps对不起,我忘记了,我会更新它。 – buzzcarla
在您的代码插入第一..insert_node->左侧应为空,因为它是现在的第一个节点 –