4
Edited code I’m having trouble removing function
#include<stdio.h>
#include<stdlib.h>
#define MAX_NOME 50
typedef struct pessoa{
char nome[MAX_NOME];
int idade;
}Pessoa;
struct celula{
Pessoa *conteudo;
struct celula*ant;
struct celula*seg;
};
typedef struct celula cel;
/*inseri entre a seguinte e null ou seja na ultima antes de null*/
void inserir(cel **lst, Pessoa *p){
cel *nova;
cel *aux;
nova = (cel*)malloc(sizeof(cel*));
nova->conteudo = p;
nova->seg = NULL ;
nova->ant = (*lst);
if((*lst)->seg == NULL){
(*lst)->seg=nova;
}
else{
aux = (*lst)->seg;
while(aux->seg != NULL){
aux= aux->seg;
}
aux->seg=nova;
nova->ant= aux;
}
}
/* inseri entre lst e a seguinte ou seja no inicio dps da cabeça*/
void inserir_inicio(cel **lst, Pessoa *p){
cel *nova;
nova = (cel*)malloc(sizeof(cel*));
nova->conteudo = p;
nova->seg = (*lst) ;
nova->seg = (*lst)->seg;
(*lst)->seg = nova;
if(nova->seg !=NULL){
nova->seg->ant=nova;
}
}
void deletar( cel **lst){
cel *p;
p = (*lst);
if((*lst)->seg ==NULL){
printf("lista vazia");
}
else
{
p->seg->ant = lst;
p->ant->seg = p->seg;
free(p);
}
}
void imprimir(cel*lst){
cel*p;
p = lst->seg;
while(p != NULL){
printf("%s %d", p->conteudo->nome, p->conteudo->idade);
p = p->seg;
}
}
main(){
cel*lst;
lst = (cel*)malloc(sizeof(cel*));
lst->seg = NULL;
lst->ant = NULL;
lst->conteudo = 0;
Pessoa p1,p2,p3;
p1.idade = 30;
strcpy(p1.nome, "matheus");
p2.idade = 18;
strcpy(p2.nome, "mayara");
p3.idade = 19;
strcpy(p3.nome, "juca");
int menu =1;
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 ini\n"
"6 - Buscar \n"
"-----------------\n"
"0 - SAIR DO PROGRAMA.\n"
"-----------------\n"
);
scanf("%d", &menu);
switch (menu)
{
case 0:
printf("Voce fechou.");
break;
case 1:
inserir(&lst,&p1);
inserir(&lst,&p2);
break;
case 2:
inserir_inicio(&lst, &p3);
inserir_inicio(&lst, &p2);
break;
case 3:
imprimir(lst);
break;
case 4:
deletar(lst);
break;
case 5:
break;
case 6:
break;
default:
printf("Opcao inexistente.");
break;
}
}
}
Be more clear and specific, we ask specific questions do not do your college work.
– DaviAragao
sorry is that I’m stuck in a remove from error function always have this list with head and want to remove the first Element plus it is chained and already tried to declare p->ant->seg=p->seg if(p->seg) != NULL p->seg->ant = p->ant free(p)
– Matheus Francisco
@Matheusfrancisco Enter the code you tried, and tell clearly which error you got. At first glance, what you wrote above seems to me correct, the only thing I didn’t understand is who it is
p
... (in other words, if you are choosingp
wrong, even the above code being right the function as a whole will not work)– mgibsonbr
P.S. In her
inserir_inicio
, you forgot to donova->ant = (*lst)
, so that if you insert a node at the beginning and then try to remove it by its above method, it will give a null reference. I think where you wrotenova->seg = (*lst)
and soon after redefined, is where you intended to assign theant
, right?– mgibsonbr
agr the problem is in the remove function I’m trying to fix
– Matheus Francisco