排序字符串的链表

时间:2014-11-16 22:32:04

标签: c++ linked-list

我试图找到一种按字母顺序排列字符串链接列表的方法。这是我到目前为止,它编译但崩溃时执行..我已经工作了几个小时但似乎无法弄明白。 while循环命令中的cout打印出来,所以我猜它正在比较好。我觉得它是我正在弄乱的链接..这是学校作业顺便说一句,所以任何输入都是受欢迎的!它的ordreAlpha(应该)对列表进行排序。

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cerrno>


struct Espion
{
    std:: string name; /* nom de l’espion(ne) */
    Espion *next; /* pointeur sur le prochain espion */
} ;


Espion *sortAlpha(Espion*ptr)
{
   Espion *cur = ptr;
   Espion *tempo = NULL;
   Espion *prev  = NULL;
   Espion *first  = ptr;
   Espion *last  = NULL;
   bool flag;

   do{

    flag = false;
    while(cur->next != NULL){
                    tempo = cur->next;
                    if(cur->name.compare(tempo->name) > 0){

                           flag = true;// needs swap
                           cur->next = tempo->next;
                           tempo->next = cur;
                           if(prev != NULL)
                                   prev->next = tempo;


                           if(prev == NULL)
                                   first = tempo;

                           if(cur->next == NULL)
                                   last = cur; 

                           prev = cur; 
                           cur = cur->next;
                           delete tempo;       
                    }
                    else {

                         prev = cur;
                         cur = cur->next;
                    }

         }
      delete cur;
      delete prev;
      cur = first;              
    }while(flag == true);  

    return first;             
}







void printList(Espion* ptr)
{
     std::cout << "\n"<< std::endl;
     while(ptr){
                std::cout << ptr-> name << std::endl;
                ptr = ptr->next;
     }
     std::cout << "\n"<< std::endl;

}

int main()
{

    char fileName[] = "espion.txt";

    std:: string infoEspion;


    Espion *cur = NULL;
    Espion *first = NULL;
    Espion *last = NULL;
    Espion *add = NULL;


    std :: ifstream readFile;
    readFile.open(fileName, std::ios::in);

    if(!readFile.is_open()){
        exit(EXIT_FAILURE);
    }





    while(std::getline(readFile, infoEspion))
    {
                if(first == NULL){ //head
                      add = new Espion;
                      add -> name = infoEspion.substr(0,30);
                      add -> next = NULL;
                      first = add;
                      cur = add;
                      last = add;


                      }
                      else
                      if(trouverNomListe(first, infoEspion.substr(0,30)) == false ) // adding only if not on the list already

                            {

                                add = new Espion;
                                add -> name = infoEspion.substr(0,30);
                                add -> next = NULL;
                                cur -> next = add;
                                last = add;
                                cur = add;



                             }








    }
    printList(first);


    printList(sortAlpha(first));

    readFile.close();


    system("pause");
    return 0;

}

3 个答案:

答案 0 :(得分:0)

第二次绕do .. while循环,prec仍然会有其先前的值(列表的结尾),所以当你发现你必须再次交换时,它会链接结束您tempo的列表。要解决此问题,请在prec循环顶部将do .. while重置为NULL。

当调用ptr时,您还应检查ordreAlpha是否为NULL,因为当cour->suivant可能为NULL时,您当前正在立即检查cour

编辑:调试后,另一个问题是,当你交换节点时,你也会cur = cur->next,这可能导致cur NULL,这将导致你的外观出现故障条件cur->next != NULL。在这种情况下(当您已交换时),您不希望将cur移动到下一个节点。相反,您只需要确保prev设置为正确的值,并让循环继续当前节点。

在您的情况下,只需更改

prev = cur; 
cur = cur->next;
delete tempo;       

prev = tempo; 

答案 1 :(得分:0)

你的sortAlpha函数没有分配任何东西,所以那里的任何删除语句都很可疑,以后可能会导致崩溃

答案 2 :(得分:0)

您可以通过使用指向指针的方法来避免那些检查prev == NULL的语句:

Espion **pprev;              // pointer to cur or previous node's next pointer
    // ...
    cur = first;             // init current pointer (before the do while loop)
    // ...
    pprev = &cur;            // init pprev (before the inner while loop)
    // ...
    *pprev = tempo;          // update what was pointing to cur
    pprev = &(tempo->next);  // update what prev points to