Linked list sorting in C

前端 未结 5 747
自闭症患者
自闭症患者 2021-01-25 06:48

I\'m writing a simple file for one of my classes that is a simple linked list activity and I need to sort a linked list.

This is my source code so far:

/         


        
5条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-25 07:31

    I figured it out after some stack traces with a friend. Heres the fixed code:

    struct node *sort_list(struct node *head) {
    
        struct node *tmpPtr = head;
        struct node *tmpNxt = head->next;
    
        int tmp;
    
        while(tmpNxt != NULL){
               while(tmpNxt != tmpPtr){
                        if(tmpNxt->value < tmpPtr->value){
                                tmp = tmpPtr->value;
                                tmpPtr->value = tmpNxt->value;
                                tmpNxt->value = tmp;
                        }
                        tmpPtr = tmpPtr->next;
                }
                tmpPtr = head;
                tmpNxt = tmpNxt->next;
        }
             return tmpPtr ; // Place holder
    }  
    

提交回复
热议问题