首页 > 解决方案 > 在 C++ 中对链表进行排序在运行时失败

问题描述

    void head_insert(DomesticPtr& head, string firstNameD, string lastNameD, string province, float cgpaD, int researchScoreD, int idD)
{
  DomesticPtr temp_ptr;
  DomesticPtr temp2;
  temp_ptr= new DomesticStudent(head, firstNameD, lastNameD, province,cgpaD,researchScoreD,idD);
  temp2 = head->getLink();
      temp2==temp_ptr;
      head=temp_ptr;
      if (head->getLink() == NULL)
          return;
      else
      {
          bubblesort(head);
      }


}

void bubblesort(DomesticStudent* head)
{
 int  rsd;
 int cgpad;
 int p;
 DomesticPtr tempc, tempd, tempe;
 tempd=head;
 tempe= head->getLink();
    {
 while(tempd != NULL)
   {                                                   
      rsd=compareResearchScore(tempd, tempe);

                if (rsd==1)
                  {
                    tempc=head;
                    head->next=head;
                    head=tempc;
                  }// if
                else if (rsd==0)
                  {
                    cgpad= compareCGPA(tempe,tempd);
                    if (cgpad==1)
                      {
                    tempc=head;
                    head->next=head;
                    head=tempc;
                      }// if (cgpad[k]>cgpad[k+1])
                    else if(cgpad==0)
                      {

                        p=compareProvince(tempd,tempe);
                        if(p==1)
                          {
                        tempc=head;
                        head->next=head;
                        head=tempc;
                          }// if (p[k]>p[k+1])
                      }//
                      }// else if cgpad[k]
                      }// else if rsd[k]
                //  }
// }
                tempd = tempe;

}






  int compareResearchScore(DomesticPtr RSA, DomesticPtr RSB)
{
    if (RSB == NULL || RSA==NULL )
    {
        return 0;
    }
  if (RSA->researchScoreD==RSB->researchScoreD) //compares if is the same for domesetic students returns value for bubble sort
{
  return 0;
}
  if (RSA->researchScoreD > RSB->researchScoreD)
         {

           return 1;
}
  if (RSA->researchScoreD< RSB->researchScoreD)
         {
           return -1;
}
}

我试图在每次插入新节点时对我的链表进行排序。它可以编译,但每次我尝试运行程序时,它都会卡在我试图打印我的列表的点上。我有一个析构函数,但没有复制构造函数或赋值运算符。head_insert 调用 sort 函数, sort 函数调用 compare 函数来接收整数输出,以便进行交换。我想比较研究,cgpa,然后是省。任何输入将不胜感激,这是一个项目,所以我不喜欢任何代码块,但如果你能指出我正确的方向或多个方向。

标签: c++classsortinglinked-list

解决方案


推荐阅读