C++ 对字符串的链表排序
我正试图找到一种方法来按字母顺序排列一个字符串链表。这就是我目前所拥有的,它可以编译,但在执行时会崩溃。。我已经做了好几个小时了,但似乎还没弄明白。while循环中的cout命令会打印出来,所以我想它比较好。我觉得这是我搞砸的联系。。顺便说一句,这是作业,所以欢迎任何输入!它的奥德瑞法应该对列表进行排序C++ 对字符串的链表排序,c++,linked-list,C++,Linked List,我正试图找到一种方法来按字母顺序排列一个字符串链表。这就是我目前所拥有的,它可以编译,但在执行时会崩溃。。我已经做了好几个小时了,但似乎还没弄明白。while循环中的cout命令会打印出来,所以我想它比较好。我觉得这是我搞砸的联系。。顺便说一句,这是作业,所以欢迎任何输入!它的奥德瑞法应该对列表进行排序 #include <iostream> #include <fstream> #include <cstdlib> #include <cstring&
#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;
}
第二次在你做的时候。。在循环时,prec仍将其先前的值保留在列表的末尾,因此当您发现必须再次交换时,它会将列表的末尾与节奏链接起来。要解决此问题,请在do的顶部将prec重置为NULL。。while循环 当调用ordreAlpha时,您还应该检查ptr是否为NULL,因为您当前正在立即检查cour->suivant,此时cour可能为NULL 编辑:调试后,另一个问题是,当您交换节点时,您也会执行cur=cur->next,这可能会导致cur为NULL,这将导致您的外观条件cur->next出现故障无效的在这种情况下,交换后,您不想将cur移动到下一个节点。相反,您只需要确保prev设置为正确的值,并让循环继续当前节点 对你来说,只要换衣服就行了
prev = cur;
cur = cur->next;
delete tempo;
到
sortAlpha函数不分配任何内容,因此其中的任何delete语句都是可疑的,可能会导致以后的崩溃您可以通过使用指向指针的指针来避免那些检查prev==NULL的if语句:
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
您应该使用调试器逐步检查代码,找出问题的根源,并在此处提出问题之前将其缩小到一个范围。普通代码转储不被接受。另外,请将您的程序翻译成英语。不太清楚什么是afficherListe和trouverNomListe。与其使用ptr->nom.compareNomMacher==0,不如使用ptr->non==nonAChercher。为什么不通过const引用传递nonacher以避免复制它呢?而不是char nomFichier[50]=espion.txt;为什么不char nomFichier[]=espion.txt;“你从不删除节点,所以会有内存泄漏。@安东这是一个很棒的评论,也是给所有人的一堂选择有意义的变量名的课。”。不再使用i、ii、iii,然后在功能中间重用它们!所以我删除了do…上面的节点,然后我检查了一下,ptr不为NULL。。但是仍然崩溃..不,不要删除任何节点。您正在排序,因此需要保持相同数量的节点。只需在循环的顶部或底部添加prec=NULL,就在cour=prem之后。但现在调试器给了我更多的东西:while=not found in current context。我不知道你的意思是什么-调试器说while=not found,还是编译器说while=not found?是的!我终于明白我的错误是什么了。。现在运行良好,非常感谢您抽出时间
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