求教,C语言中的!在if语句中是什么作用。
wsfzk 2010-10-22 11:59:53 最近学习到了链表,大概意思弄了个差不多,在实际应用中碰到了问题。于是我就把书上的一道例题的源代码整个打了出来,发现一句代码无法理解,请各位老师指教。
LISTPTR add_to_list(int ch,LISTPTR first)
{
LISTPTR new_rec=NULL;//声明一个新指针,用来存储新节点
LISTPTR tmp_rec=NULL;
LISTPTR prev_rec=NULL;
new_rec=(LISTPTR)malloc(sizeof(LIST));//为新节点分配内存
if(!new_rec)//就是这一句,前面有一个!号,如果把它去掉程序就无法正常运行
{
printf("\nUnable to allocale memory!\n");
exit(1);
}
new_rec->ch=ch;
new_rec->next_rec=NULL;
if(first==NULL)
{
first=new_rec;
new_rec->next_rec=NULL;
}
else
{
if(new_rec->ch < first->ch)
{
new_rec->next_rec=first;
first=new_rec;
}
else
{
tmp_rec=first->next_rec;
prev_rec=first;
if(tmp_rec==NULL)
{
prev_rec->next_rec=new_rec;
}
else
{
while((tmp_rec->next_rec != NULL))
{
if(new_rec->ch < tmp_rec->ch)
{
new_rec->next_rec=tmp_rec;
if(new_rec->next_rec != prev_rec->next_rec)
{
printf("ERROR");
getc(stdin);
exit(0);
}
prev_rec->next_rec=new_rec;
break;
}
else
{
tmp_rec=tmp_rec->next_rec;
prev_rec=prev_rec->next_rec;
}
}
if(tmp_rec->next_rec==NULL)
{
if(new_rec->ch < tmp_rec->ch)
{
new_rec->next_rec=tmp_rec;
prev_rec->next_rec=new_rec;
}
else
{
tmp_rec->next_rec=new_rec;
new_rec->next_rec=NULL;
}
}
}
}
}
return(first);
}