Arrange search function in double chained circular list

Asked

Viewed 924 times

1

People have how to arrange my function to seek or give the idea of error ? I have to fetch the name of the person who is inserted in the list and remove this function with a bug

vlw hug.

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>


#define MAX_NOME 50

typedef struct pes
{
    char nome[MAX_NOME];
    int num;
} pessoa;

typedef struct cel
{
    pessoa *conteudo;
    struct cel *seg;
    struct cel *ant;
} celula;

pessoa *CriaPessoa(char *nome, int num)
{
    pessoa *nova = (pessoa*) malloc(sizeof(pessoa));
    strcpy(nova->nome, nome);
    nova->num = num;

    return nova;
}


void Inserir(celula* p, pessoa *x)
{

    celula* elemento = (celula*) malloc (sizeof(celula));
    elemento->conteudo = x;

        elemento->seg = p->seg;
        elemento->ant = p;
        if(p->seg == p)
            p->ant = elemento;
        if(p->seg)
            p->seg->ant = elemento;
        p->seg = elemento;
        printf("inseriu %s\n", p->seg->conteudo->nome);
}

pessoa * Remover(celula* p)
{
    pessoa *aux = NULL;
    celula *rem = p;
    aux = p->conteudo;

    if(p->seg)
        p->seg->ant = p->ant;
    p->ant->seg = p->seg;

    free(rem);
    return aux;
}

celula * Buscar(celula *lista, char nome[])
{
    for(;lista->seg!=NULL;lista = lista->seg);
        if(lista->seg->conteudo->nome == nome)
                return nome;

    return NULL;


    }


void Imprime(celula *lista)
{
    printf("\n\n");
    celula *aux = lista;

    for( ;lista->seg!=aux; lista = lista->seg){/* lista->seg*/
            if(lista->seg->conteudo->nome)
                printf("nome: %s -- numero: %d\n", lista->seg->conteudo->nome, lista->seg->conteudo->num);
            else
                printf("Cabeca\n");
                system("pause");
        }
}



int main()
{
    celula *lista = (celula*) malloc( sizeof(celula));
    lista->seg = lista->ant = lista;

    Inserir(lista ,CriaPessoa("eduardo",10));
    Inserir(lista ,CriaPessoa("andrei",12));
    Inserir(lista ,CriaPessoa("luiz",13));
    Inserir(lista ,CriaPessoa("caio",14));
    Inserir(lista ,CriaPessoa("daniel",15));
    Inserir(lista ,CriaPessoa("joao",16));
    Inserir(lista ,CriaPessoa("pedro",17));

    Imprime(lista);
    celula *x = Buscar(lista, "pedro");
    printf("Achou %s na lista\n", x->conteudo->nome);
    /*Remover(x);*/

    Imprime(lista);
system("pause");
}
  • 1

    What bug??? Build error, run... (at first glance I notice the return type of Buscar is celula* but you’re trying to return a char[]; did you not want to return lista instead of nome?)

  • does not look for anything more not of the error in the code and in my code not of Warning or anything, very strange!

  • @Francis has compiled with -Wall to really see if there are no warnings?

  • 1

    On a second reading, I see that you are also comparing strings with ==. Unless comparing by identity, i.e. if it is the same string, and not merely two equal strings, this will always turn out false (hence the search will never return anything, always null). I suggest using strcmp (in practice) or compare string characters one to one (for learning). But fix the return type problem first.

1 answer

2


As @mgibsonbr said, in the search function the return type and the name check were wrong.

celula *Busca(celula *lista, char nome[])
{
        celula *achado = NULL;
        celula *aux = lista;
        while(lista->seg != aux)
        {
                if(strcmp(lista->elemento->nome, nome) == 0)
                        achado = lista;
                lista = lista->seg;
        }
        return achado;
}

For removal it is important to check if the element to be removed is the first and update the list value in the main function.

celula *Remover(celula *lista, celula *p)
{
    celula *rem = p;

    if(lista == p)
        lista = p->seg;

    p->seg->ant = p->ant;
    p->ant->seg = p->seg;

    free(rem);
    return lista;
}

Main function.

celula *x = Buscar(lista, "pedro");
printf("Achou %s na lista\n", x->conteudo->nome);
lista = Remover(lista, x);

NOTE: The removal function will be right if the insertion function is following the concept of double-chained circular list correctly.

  • Thanks guy helped a lot!

  • I am trying to make a function to invert the list but this giving a warring in the first line of the if of this function talking q this with error in the most Pontiero can not see which pointer void to invert(cell**lst){ if((lst)->seg == lst){ printf("head "); }Else{ cellp; cellq; cellx; p = q = x = (*lst)->seg; while( x != lst ){ p = p->seg; x->seg = q; q = x; x = p; } (*lst)->seg->seg = lst; (*lst)->seg = q; } }

  • This is a double-chained circular list, circles have no beginning and no end. From the head of the list go through it using the pointer ant who will have his list "reversed".

Browser other questions tagged

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