C 需要插入单链表的帮助吗

C 需要插入单链表的帮助吗,c,C,它是正确地插入员工并将他们按排序,就像我所面临的唯一问题是列表中的第一个员工没有被排序一样 改变如下: void insert(SLL *list, Employee e){ NODE *temp, *current, *temp2; temp = (NODE *)malloc(sizeof(NODE)); assert(temp != NULL); temp -> anEmployee = (Employee *)malloc(sizeof(Empl

它是正确地插入员工并将他们按排序,就像我所面临的唯一问题是列表中的第一个员工没有被排序一样

改变如下:

    void insert(SLL *list, Employee e){

   NODE *temp, *current, *temp2;

   temp = (NODE *)malloc(sizeof(NODE));
   assert(temp != NULL);

   temp -> anEmployee = (Employee *)malloc(sizeof(Employee));
   assert(temp -> anEmployee != NULL);

   strcpy(temp -> anEmployee -> name, e.name); 
   temp -> anEmployee -> ID = e.ID;
   strcpy(temp -> anEmployee -> address, e.address); 
   strcpy(temp -> anEmployee -> city, e.city);
   temp -> anEmployee -> age = e.age;

   temp -> next = NULL;

   if (list -> head == NULL) {     /* list is empty */
      list -> head = list -> tail = temp;
      return;
   }
   else { // list is not empty
        current = list-> head;
        while(current -> next != NULL && strcmp(temp-> anEmployee -> name, current-> anEmployee -> name)>0){
            current = current -> next;
        }
        if(current -> next == NULL){
            list -> tail -> next = temp;
            list -> tail = temp;
        }
        else{
            temp -> next = current -> next;
            current -> next = temp;
        }
  }
}
与:

    if(current -> next == NULL){
        list -> tail -> next = temp;
        list -> tail = temp;
    }
    else{
        temp -> next = current -> next;
        current -> next = temp;
    }

好的,那么您是否尝试过使用调试器单步执行代码?
    if(current == list->head){
        list->head = temp;
        temp->next = current;
    } else if(current -> next == NULL){
        list -> tail -> next = temp;
        list -> tail = temp;
    } else{
        temp -> next = current -> next;
        current -> next = temp;
    }