Problem creating search function in C

Question:

The search function receives the address of the first element of a list (p) and a string of characters (subname). Assuming that subname Francisco of the person's name matheus francisco will return it to me instead of NULL . I'm having difficulties to make it work.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_NOME 50

typedef struct pessoa
{
     char nome[MAX_NOME];
     int idade;
     char subnome[MAX_NOME];
}Pessoa;

/*celula lista*/
typedef struct Lista{
    Pessoa *elemento;
    struct Lista *prox;
}celula;

typedef celula *Lista;
void inserir(celula *ptr, Pessoa *p){
    if( ptr == NULL){
             printf("Lista vazia ");
        return 0;
    }
  celula *novo;
  novo = malloc(sizeof(celula));
  novo->elemento= p;
  novo->prox = ptr->prox;
  ptr->prox = novo;

}
celula *inserir_inicio(celula **topo, Pessoa *p ){
     celula *novo;
  novo = malloc(sizeof(celula));
   novo->elemento= p;

  if(*topo == NULL){
    novo->prox = NULL;
    *topo = novo;
    return novo;
  }
  else{
  novo->prox =*topo;
  *topo=novo;
  return novo;
}
}
celula *remover_ini(celula *topo)
{
    celula *lixo = topo;
        if(lixo == NULL)
        {
            printf("Lista vazia");
        }
        else
        {
            topo = topo->prox;
            printf("Elemento a ser excluido: %c", 10);
            printf("Nome: %s | Idade: %d \n",lixo->elemento->nome, lixo->elemento->idade);
            free(lixo);
        }
    return topo;

}
void remover(celula *topo){
    if(topo->prox == NULL){
        celula *aux;
        aux = topo->prox;
        free(topo);

    }
    celula *lixo;
    lixo = topo->prox;
    topo->prox = lixo->prox;
    free(lixo);
}
void Busca(celula *topo, char sobnome[]) {
     celula *aux;
      aux = topo;
      while(aux != NULL)
        {
            if(aux->elemento->subnome)
             printf("%s", aux->elemento->subnome);
      aux = aux->prox;
      }

}
void printar(celula *topo){
 celula *aux = topo;
  if(aux == NULL)
  {
    printf("vazio");
  }
  else{
      do{

         printf("Nome: %s | Idade: %d \n",aux->elemento->nome, aux->elemento->idade);
             printf("-------------------------- \n");
         aux= aux->prox;

      }while(aux != NULL);
  }
}



main()
{
  Lista topo = NULL;
  Pessoa p1,p2,p3,p4,p;
  Pessoa *info_removida;
  int menu = 1;

    p1.idade = 30;
    strcpy(p1.nome, "matheus " );


    p2.idade = 18;
    strcpy(p2.nome, "mayara");

    p3.idade = 19;
    strcpy(p3.nome, "juca");

while ( menu != 0) {
        printf(
        "\n-----------------------------------------------------\n"
        "Selecione opcao que deseja, veja nosso menu:\n"
        "-----------------\n"
        "0 - Sair \n"
        "1 - Inserir no Inicio\n"
        "3 - exibir \n"
        "2 - Inserir \n"
        "4 - Remover \n"
        "5 - Remover fi\n"
        "6 - Para buscar pessoa sobnome\n"
        "-----------------\n"
        "0 - SAIR DO PROGRAMA.\n"
        "-----------------\n"
        );
        scanf("%d", &menu);
        switch (menu){

            case 0:
                printf("Voce fechou.");
            break;
            case 1:

                inserir_inicio(&topo, &p1);
                inserir_inicio(&topo, &p2);
                inserir_inicio(&topo, &p3);

            break;

             case 2:
                 inserir(topo, &p1);

            break;
            case 3:

                printar(topo);
            break;
            case 4:
              topo = remover_ini(topo);
           break;
            case 5:
                remover(topo);
                break;
            case 6:
                Busca(topo);
             break;
            default:
                printf("Opcao inexistente.");
                break;
            }
    }

}

Answer:

The busca function needs to receive, in addition to the list, a sobnome that needs to be passed as a parameter when calling it.

And if your intention is for it to return NULL have to change the return type to a pointer and receive it and treat it in main :

Search:

celula *Busca(celula *topo, char sobnome[])
{
        celula *aux = topo;
        celula *achado == NULL;
        while(aux != NULL)
        {
                if(strcmp(aux->elemento->subnome, sobnome) == 0)
                        achado = aux;
                aux = aux->prox;
        }
        return achado;
}

Statement in main :

main()
    {
      Lista topo = NULL;
      Lista encontrado = NULL;
      Pessoa p1,p2,p3,p4,p;
      Pessoa *info_removida;
      int menu = 1;

Calling the busca function:

 case 6:
       encontrado = Busca(topo, "Francisco");
 break;

This way, the search function will return the celula that contains the last name or NULL if it doesn't exist, you just have to do the treatment that suits you.

Scroll to Top