【问题标题】:Searching a string in a linked list C在链表中搜索字符串 C
【发布时间】:2021-11-01 04:16:09
【问题描述】:

我在 C 中搜索链表时遇到问题。我设法搜索并找到一个整数条目,但在字符串(名字和姓氏)方面遇到问题。基本上,有三个功能可以按名字、姓氏和号码在电话簿中搜索条目。是否也可以在找到搜索时显示该条目?请在下面找到代码。感谢您的帮助。

struct node { 
    char firstname[32]; 
    char lastname[32]; 
    int *number; 
    struct node *next; 
}*head; 

struct node *start=NULL; 

struct node *getnode() { 
    return((struct node *)malloc(sizeof(struct node)));
}

void insert() { 
    struct node *temp,*nn; 
    nn=getnode(); 
    temp=start; 
    while(temp->next!=NULL) 
    { 
        temp=temp->next; 
    } 
    printf("Enter First name:\n"); 
    scanf("%s",&nn->firstname); 
    printf("Enter Last name:\n"); 
    scanf("%s",&nn->lastname); 
    printf("Enter number:\n"); 
    scanf("%d",&nn->number); 
    temp->next=nn; 
    nn->next=NULL; 
    display(start);
} 


struct node *create() {
    struct node *temp,*nn; 
    if(start!=NULL) insert(); 
    else { 
        nn=getnode(); 
        start=nn; 
        temp=start; 
        printf("Enter First name:\n"); 
        scanf("%s",&nn->firstname); 
        printf("Enter Last name:\n"); 
        scanf("%s",&nn->lastname); 
        printf("Enter number:\n"); 
        scanf("%d",&nn->number); 
        nn->next=NULL;
    }
} 

void searchByFirstName() { 
    char *f;
    struct node* temp, *nn;
    temp = start;
    while (temp != NULL){
        printf("Enter First Name to be searched:\n");  scanf("%s",&f);
        printf("%s", &f);
        if (temp -> firstname == f){
            printf ("\n Record Found!\n");
            temp = temp -> next;
        }else{
            printf ("\n Record not found\n");
        } 
    }
} 

void searchByLastName() { 
    char *f;
    struct node* temp, *nn;
    temp = start;
    if (temp != NULL){
        printf("Enter Last Name to be searched:\n");  scanf("%s",&f);
        printf("%s", &f);
        if (temp -> lastname == f){
            printf ("\n Record Found!\n");
            temp = temp -> next;
        }else{
            printf ("\n Record not found\n");
        } 
    }
}

void searchByNumber() { 
    int *l;
    struct node* temp, *nn;
    temp = start;
    if (temp != NULL){
        printf("Enter Number to be searched:\n");  scanf("%d",&l);
        if (temp -> number == l){
            printf ("\n Record Found!\n");
            temp = temp -> next;
        }else{
            printf ("\n Record not found\n");
        } 
    }
}

【问题讨论】:

  • 为什么number 是一个指针?你从来没有分配内存让它指向。'
  • 扫描成字符串时不要使用&
  • 你从未为f分配内存。
  • 这段代码有很多问题,你需要复习你对字符串和指针的注释。
  • 按号码搜索有用吗?看起来您应该只使用 int l,而不是指针类型(在 scanf 中,您使用指向 int 的指针,而不是指向 int 的指针)

标签: c string search linked-list


【解决方案1】:

在 C 中,您不能只使用运算符 == 比较两个字符串(也称为 char *)。在 C 中处理字符串时,可以使用标准函数 (#include ),例如:

strcmp(temp->lastname, f) // returns 0 in case of a perfect match

【讨论】:

    【解决方案2】:

    第 2 次编辑:添加了 Pretty 函数(最后一节)- 可以修改打印消息的示例。

    编辑:在 searchByNumber2() 中添加了free(l),修复了内存泄漏

    对于你要的东西:

    void searchByLastName() { 
        char f = char[32];
        struct node* temp, *nn;
        temp = start;
    
        printf("Enter Last Name to be searched:\n");  scanf("%s",f);
        printf("%s", f);
        while (temp != NULL){//!we search through whole list
            
            if (strcmp(temp -> lastname, f) == 0) {
                printf ("\n Record Found!\n");
                temp = temp -> next;
            }else{
                printf ("\n Record not found\n");
    /*!you probably wanna pull this printf for after the loop;
    I would either add bool Found and add if after the loop
    or after the loop always print "search finished" or sth */
    
    
            } 
        }
    }
    

    还有两个版本的数字固定搜索(第二个以int *l为例;第一个使用int l,更简洁)

    void searchByNumber() { 
        int l;
        struct node* temp, *nn;
        temp = start;
        printf("Enter Number to be searched:\n");  scanf("%d",&l);//&l is address of the int l
        while (temp != NULL){
            if (*(temp -> number) == l){//notice: temp->number is a pointer to int, not an int itself, l is an int here
                printf ("\n Record Found!\n");
                temp = temp -> next;
            }else{
                printf ("\n Record not found\n");
            } 
        }
    }
    
    
    void searchByNumber2() { 
        int *l = malloc(sizeof int);
        struct node* temp, *nn;
        temp = start;
        printf("Enter Number to be searched:\n");  scanf("%d",l);
        while (temp != NULL){
            if (*(temp -> number) == *l){//we want to compare int values, not their adresses, both temp->number and l are pointers here
                printf ("\n Record Found!\n");
                temp = temp -> next;
            }else{
                printf ("\n Record not found\n");
            } 
        }
    free(l);
    }
    

    上次编辑

    注意:此版本的 searchByLastNamePretty 会打印姓氏出现次数的 Found 消息,并在搜索后打印 Finished 消息。 另一方面,searchByNumberPretty 打印一条 Found 消息或一条 Not-Found 消息(始终只有一条消息;如果该数字出现很多则无关紧要)。

    将它们用作您可以做什么的示例。分析bool Found的使用情况。

    void searchByLastNamePretty() { 
        char f = char[32];
        struct node* temp, *nn;
        temp = start;
    
        printf("Enter Last Name to be searched:\n");  scanf("%s",f);
        printf("%s\n", f);  //better practice, to end printf with \n to flush it
        while (temp != NULL){//!we search through whole list
            
            if (strcmp(temp -> lastname, f) == 0) {
                printf ("Record Found!\n");
                temp = temp -> next;
            }
            printf("Search finished\n");
        }
    }
    
    
    void searchByNumberPretty() { 
        int l;
        struct node* temp, *nn;
        temp = start;
        printf("Enter Number to be searched:\n");
        scanf("%d",&l);//&l is address of the int l
        bool found = false;
        while (temp != NULL && !found){//!we search till the first occurence
            if (*(temp -> number) == l){//note: (temp->number) is a pointer to int, not an int
                found = true;
                temp = temp -> next;
            }
        }
    
        if (found) {
            printf("Record Found!\n");
        }
        else {
            printf("Record not found\n");
        }
    }
    

    【讨论】:

    • 您可以在找到第一个条目后打破循环,或者继续搜索所有条目。如果您想要特定的行为,我可以将代码插入到我的答案中。如果你不想玩,我也可以将printf("not found") 拉出循环,但我想我会让你玩得开心(我认为这也是一个很好的练习)
    • 如果您也可以将其插入代码中会很棒,因为我无法弄清楚:)
    猜你喜欢
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多