Problem creating C search function

Asked

Viewed 877 times

3

The search function takes the address of the first element of a list (p) and a string (subname). Suppose subname Francisco of the person’s name matheus francisco will return it to me instead of NULL. I’m having trouble making 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;
            }
    }

}
  • I found two problems when you pass topo for function Busca you are copying the variable instead of its index. You should also take a look at the function strstr http://www.tutorialspoint.com/c_standard_library/c_function_strstr.htm

1 answer

2


The function of busca need to receive beyond the list one sobnome which remains to be passed as a parameter at the time it calls.

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

Seeking:

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;

Function call busca:

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

This way the search function will return to celula containing the last name or NULL if the same does not exist, just do the treatment that suits you.

  • if(strcmp(aux->element->subname, sobnome) == 0) find = aux; aux = aux->Prox; this tando an error in that code !! strcmp

  • Is it time to compile? What’s the mistake?

  • person has no Member named "subname" but I did this function cell *Search(list lst, Person x){ cell *p = lst->Prox; while ((p != NULL) && (p->element->sobnome != x.name)){ printf("%s",p->element->sobnome); p = p->Prox; } Return p; } and rotated printed only q dps does not print anymore and Buga the program

  • To pessoa really doesn’t have sobnome that you’re trying to access here (p != NULL) && (p->elemento->sobnome != x.nome). Pessoa has subnome. If it hasn’t changed the struct pessoa.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.