nodetype *f(nodetype *head,elemtype x)
{
nodetype *p=head->next,*q,*s;
q=(nodetype *)malloc(sizeof(nodetype));
q->data=x;
if(p==NULL)
{
head->next=q;
q->next=NULL;
return head;
}
while(1)
{
while(p!=NULL && p->data < x)
{
s=p;
p=p->next;
}
if(p==NULL)
{
s->next=q;
q->next=NULL;
return head;
}
else if(p->data==x)
{
s->next=p->next;
return head;
}
else if(p->data>x)
{
q->next=p;
s->next=q;
return head;
}
}
}
注意一定要將if(p==NULL)寫在最前面,否則的話p->data就會導致程序崩潰。
這應該算是我寫了這些天程序的一個新的收穫吧,看起來很簡單的程序,自以爲很容易就可以寫出來了,結果寫了半天,什麼都理清楚了,但是程序就是老崩潰,不知道問題出在哪裏了,所以說,程序要先寫在紙上,然後一定要進行調試,否則永遠都無法進步的。