主题:谁能改下这个程序?
insert_list(Node **list, int val)
{
Node *current;
Node *new;
while ((current = *list) != NULL &&
current->data < val) {
if (current->next == NULL)
break;
list = ¤t->next;
}
//if there is already an number equal val, return 0
if(current->data == val){
puts("There is the same number already.");
return 0;
}
new = (Node*)malloc(sizeof(Node));
if( new == NULL){ //also if(!new)
perror("malloc failed!");
return 1;
}
new->data = val;
new->next = !current->next ? NULL : current;
if(current->next == NULL)
current->next = new;
else
*list = new;
return 0;
这个是《c和指针》里的一个例程,不过那个我怎么也没给弄出来,后来改写成了这个样子,算是能工作了,不过感觉太累赘,有谁能帮忙改下啊?
这个是没头结点的!
谢谢了,
回了有分加啊[em5]
{
Node *current;
Node *new;
while ((current = *list) != NULL &&
current->data < val) {
if (current->next == NULL)
break;
list = ¤t->next;
}
//if there is already an number equal val, return 0
if(current->data == val){
puts("There is the same number already.");
return 0;
}
new = (Node*)malloc(sizeof(Node));
if( new == NULL){ //also if(!new)
perror("malloc failed!");
return 1;
}
new->data = val;
new->next = !current->next ? NULL : current;
if(current->next == NULL)
current->next = new;
else
*list = new;
return 0;
这个是《c和指针》里的一个例程,不过那个我怎么也没给弄出来,后来改写成了这个样子,算是能工作了,不过感觉太累赘,有谁能帮忙改下啊?
这个是没头结点的!
谢谢了,
回了有分加啊[em5]