我正在使用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->左側應爲空,因爲它是現在的第一個節點 –