4
I wonder how I create a function to remove at the top of the list headless.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_NOME 50
typedef struct pessoa{
char nome[MAX_NOME];
int idade;
}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){
if((*topo) == NULL){
return printf("Lista vazia");
}
else{
celula *lixo = *topo;
(*topo)->prox = lixo->prox;
free(lixo);
return 1;
}
}
void remover(celula *topo){
if(topo->prox == NULL){
celula *aux;
aux = topo->prox;
free(topo);
return 0;
}
celula *lixo;
lixo = topo->prox;
topo->prox = lixo->prox;
free(lixo);
return 1;
}
void printar(celula *topo){
celula *aux;
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;
p1.idade = 30;
strcpy(p1.nome, "matheus");
p2.idade = 18;
strcpy(p2.nome, "mayara");
p3.idade = 19;
strcpy(p3.nome, "juca");
int menu;
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"
"-----------------\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:
remover_ini(topo);
break;
case 5:
remover(topo);
break;
default:
printf("Opcao inexistente.");
break;
}
}
}
welcome!! Try to indent your code, so we can help you.
– Pena Pintada
I didn’t read the whole code because I soon found a problem that could cause many problems. Why two structures? One for the person and one for the list? My suggestion: http://pastebin.com/AmgTufxD In addition to the code and post only necessary pieces.
– krystalgamer
When posting a question it would be interesting to define well what you would like and what mistakes you are encountering.
– DaviAragao
@krystalgamer If you only use one structure, AP will only have a list of people (i.e. you cannot have lists of other things, nor people outside lists). This separation is beneficial if done correctly of course (i.e. without leaving any unintentional coupling). P.S. Okay, I realize that in this case the list is only of people, because the pointer is for
Pessoa
, but still separate becomes easier to generalize in the future.– mgibsonbr
@Matthew For you to indent the code, it has an easy way : if you use the Notepad++ or another editor paste your code into it , select everything and a tab (this creates four blank spaces for the line of code) then copy and paste into your question.
– Pena Pintada
@Pen painted ...or simply select all the code and click on the "Code Sample" button (Ctrl+K shortcut). :)
– mgibsonbr
Matheus, if you use the same structure to represent a list and to represent a cell on the same list, then there is no way: either you have a head, or you have to replace the original list (everywhere in the program that refers to it) each time you remove the first element. A "gambiarra" that you could do is remove the second element from the list by copying its value to the first node. But I’d still have trouble removing the last item from the list, so I wouldn’t recommend...
– mgibsonbr
@mgibsonbr Thanks man, now I will indent the fastest codes around here!
– Pena Pintada