回 帖 发 新 帖 刷新版面

主题:谁能改下这个程序?

insert_list(Node **list, int val)
{
    Node *current;
    Node *new;

    while ((current = *list) != NULL &&
        current->data < val) {
        if (current->next == NULL)
            break;
        list = &current->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]

回复列表 (共1个回复)

沙发

我晕
不懂
你搞一半内容出来干嘛?

我来回复

您尚未登录,请登录后再回复。点此登录或注册