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");
}
What bug??? Build error, run... (at first glance I notice the return type of
Buscar
iscelula*
but you’re trying to return achar[]
; did you not want to returnlista
instead ofnome
?)– mgibsonbr
does not look for anything more not of the error in the code and in my code not of Warning or anything, very strange!
– Matheus Francisco
@Francis has compiled with
-Wall
to really see if there are no warnings?– krystalgamer
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 usingstrcmp
(in practice) or compare string characters one to one (for learning). But fix the return type problem first.– mgibsonbr