将元素从列表更改为结尾

时间:2018-12-22 13:23:52

标签: c pointers nullpointerexception

我在C中有一个列表,想要对其进行排序。一切正常,但是当我想更改列表中的最后一个元素时。我想结束的元素消失了。 你们有人知道出什么问题吗?

struPerson* sortListWithSelectSort(struPerson* pStart) {

    struPerson* pLastElement = NULL;
    struPerson* pLastElementToCompare = NULL;

    for (struPerson* pElement = pStart; pElement->pNext != NULL; pElement = pElement->pNext) {

        pLastElementToCompare = pElement;
        struPerson* pElementToCompare = pElement->pNext;

        do {

            //boolean 0 = false, 1 true
            short isGreater = 0;

            //compare if change must be
            if (strcmp(pElement->nachname, pElementToCompare->nachname) > 0) {
                isGreater = 1;
            }
            else if (strcmp(pElement->nachname, pElementToCompare->nachname) == 0) {
                if (strcmp(pElement->vorname, pElementToCompare->vorname) > 0) {
                    isGreater = 1;
                }
            }

            //change elements
            struPerson* pTemp = pElement;

            if (isGreater > 0) {
                struPerson* pTempElementToCompareNext = pElementToCompare->pNext;

                //change position
                //check if the element is pStart
                if (pStart == pElement) {
                    pStart = pElementToCompare;
                }
                else {
                    pLastElement->pNext = pElementToCompare;
                }

                //check if they are behind
                if (pTemp->pNext = pElementToCompare) {
                    pTempElementToCompareNext = pElementToCompare->pNext;
                    pElementToCompare->pNext = pElement;
                    pElement->pNext = pTempElementToCompareNext;
                    pElement = pElementToCompare;
                    pElementToCompare = pTemp;
                    pLastElementToCompare = pElement;
                }
                else {
                    pTempElementToCompareNext = pElementToCompare->pNext;
                    pElementToCompare->pNext = pTemp->pNext;
                    pElement->pNext = pTempElementToCompareNext;
                    pElementToCompare->pNext->pNext = pElement;
                    pElement = pElementToCompare;
                    pElementToCompare = pTemp;
                }
            }
            else {
                //set Pointer for next comparison
                pLastElement = pElement;
                pLastElementToCompare = pLastElementToCompare;
            }

            if (pElementToCompare->pNext == NULL) {
            }
            else {
                pElementToCompare = pElementToCompare->pNext;
            }

        } while (pElementToCompare->pNext != NULL);
    }
    return pStart;
}

2 个答案:

答案 0 :(得分:1)

恰好位于//check if they are behind下:if (pTemp->pNext = pElementToCompare) {必须为if (pTemp->pNext == pElementToCompare) {

我鼓励您编译要求所有警告的内容,例如gcc -Wall ...,它有助于查看此类错误以及更多信息

//set Pointer for next comparison注释的区块中,赋值pLastElementToCompare = pLastElementToCompare;什么都不做,必须是另一个吗?

答案 1 :(得分:0)

一个可行的提案,对不起,我懒得搜索更多您版本中的问题

#if 0
void exchange(struPerson* p1, struPerson* p2)
{
  char * s;

  s = p1->nachname;
  p1->nachname = p2->nachname;
  p2->nachname = s;

  s = p1->vorname;
  p1->vorname = p2->vorname;
  p2->vorname = s;

  /* exchange other fields except pNext of course
     this is the disadvantage of that solution */
}
#else
/* other way but you have to check this works with your definition of struPerson */
void exchange(struPerson* p1, struPerson* p2)
{
  struPerson p = *p1;
  struPerson * p1next = p1->pNext;
  struPerson * p2next = p2->pNext;

  *p1 = *p2;
  *p2 = p;
  p1->pNext = p1next;
  p2->pNext = p2next;
}
#endif

void sortListWithBubbleSort(struPerson* pStart) {
    int modified;

    do {
        modified = 0;

        struPerson* pElement;

        for (pElement = pStart; pElement->pNext != NULL; pElement = pElement->pNext) {
          int cmp = strcmp(pElement->nachname, pElement->pNext->nachname);

          if ((cmp > 0) ||
              ((cmp == 0) && (strcmp(pElement->vorname, pElement->pNext->vorname) > 0))) {
            exchange(pElement, pElement->pNext);
            modified = 1;
          }
        }
    } while (modified != 0);
}

与气泡排序一样,这不是始终检查最后一个元素的正确方法,因为至少一个回合后已经对最后一个元素进行了排序,我可以通过练习来改进它: -)