Luke Grech
Luke Grech

Reputation: 185

Searching a string in a linked list C

I am having issues searching a linked list in C. I manage to search and find an integer entry but am having problems with strings (first and last names). Basically, there are three functions that search a telephone directory for entries by first, last name and number. Would it be possible to display the entry when a search is found too? Please find the code below. Thanks for your help.

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");
        } 
    }
}

Upvotes: 0

Views: 597

Answers (2)

wojand
wojand

Reputation: 190

2nd EDIT: added Pretty functions (last section) - examples of how printing messages could be modified.

EDIT: added free(l) in the searchByNumber2(), fixing a memory leak

For the thing you was asking for:

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 */


        } 
    }
}

And two versions of fixed searching by numbers (the second uses int *l, as an example; the first uses int l and is cleaner)

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);
}

Last edit

Note: this version of searchByLastNamePretty prints Found message the number of times the last name appears, and prints Finished message after a search. On th other hand, searchByNumberPretty prints a Found message or a Not-Found message (only one message, always; doesn't matter if there is a lot of occurences of the number).

Use them as examples of what you could do. Analyze usage of 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");
    }
}

Upvotes: 0

Tristan Clare
Tristan Clare

Reputation: 46

In C, you cannot just compare two strings (aka char *) using operator ==. When you work with strings in C, you can use the standard functions (#include <string.h>), like:

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

Upvotes: 2

Related Questions