比较c++中链表中的两个字符串

comparing two strings from linked list in c++

本文关键字:两个 字符串 c++ 链表 比较      更新时间:2023-10-16

所以我在需要的地方有这个赋值1-从341行的文本文件中读取2-列出341人的姓名(前30个字符)3-一些名称是重复的

因此,我想做的是在创建它时,在将其链接到列表之前,检查名称是否已经包括在内。

在做了一些测试后,我意识到我正在比较的两个字符串都作为列表的第一个名称打印出来,并且似乎忽略了列表的其余部分。当我删除对布尔函数的调用时,列表会打印出来。

我是一个学生,又是一个不祥的开始,所以我担心我可能会错过几步?感谢任何帮助或指导!!谢谢

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cerrno>
using namespace std;
struct Pays
{
    char nom[20+1]; /* nom du pays */
    Pays *suivant; /* pointeur sur le prochain pays */
} ;
struct Emploi
{
    char nom[29+1]; /* nom de l’emploi */
    Emploi *suivant; /* pointeur sur le prochain emploi */
} ;
struct Espion
{
    std:: string nom; /* nom de l’espion(ne) */
    Pays *listePays; /* tête de liste des pays visités */
    Emploi *listeEmploi; /* tête de liste des emplois occupés */
    Espion *suivant; /* pointeur sur le prochain espion */
} ;

bool trouverNomListe(Espion* ptr, std ::string nomAChercher)// returns true if the name is already on the list
{
     if(ptr->suivant == NULL)
            return false;
            else
                if(ptr->nom.compare(nomAChercher) == 0){
                                    std:: cout<< ptr->nom << std::endl;// checking if the function is comparing the strings
                                    return true;
                                    }
                                  else
                                      trouverNomListe(ptr->suivant, nomAChercher); //keeps looking thru the list


}

int main()
{
    const int MAX_NOM = 30, MAX_PAYS = 20, MAX_EMPLOI = 29;
    char nomFichier[50] = "espion.txt";
    int n = 0;
    std:: string infoEspion;

    Espion *actu = NULL;
    Espion *debut = NULL;
    Espion *fin = NULL;
    Espion *nouveau = NULL;
    std :: ifstream aLire;
    aLire.open(nomFichier, std::ios::in);
    if(!aLire.is_open()){
        exit(EXIT_FAILURE);
    }


    while(std::getline(aLire, infoEspion))
    {
                if(debut == NULL){// if the list is empty
                      nouveau = new Espion;
                      nouveau -> nom = infoEspion.substr(0,30);
                      nouveau -> suivant = NULL;
                      debut = nouveau;
                      actu = nouveau;
                      fin = nouveau;
                      std::cout << actu -> nom<< std::endl;  
                      }
                      else
                      if(trouverNomListe(debut, infoEspion.substr(0,30)) == false )
                      {
                          nouveau = new Espion;
                          nouveau -> nom = infoEspion.substr(0,30);
                          nouveau -> suivant = NULL;
                          actu -> suivant = nouveau;
                          fin = nouveau;
                          actu = nouveau;
                          std::cout << actu -> nom<< std::endl;  
                          }


    }


    aLire.close();

    system("pause");
    return 0;
}

您的trouverNomListe函数是递归的,这是不正确的。它应该简单地按顺序浏览列表,例如

bool trouverNomListe(Espion* ptr, std ::string nomAChercher)// returns true if the name is already on the list
{
    while (ptr)
    {
         if(ptr->nom.compare(nomAChercher) == 0)
              return true;
         ptr = ptr->suivant;
    }
    return false;
}

您的代码还有其他几个问题,但这似乎是主要问题。