Double chained list in C

Asked

Viewed 6,989 times

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.

  • 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)

  • 1

    @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 choosing p wrong, even the above code being right the function as a whole will not work)

  • 1

    P.S. In her inserir_inicio, you forgot to do nova->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 wrote nova->seg = (*lst) and soon after redefined, is where you intended to assign the ant, right?

  • agr the problem is in the remove function I’m trying to fix

1 answer

5


To delete a node you first need to get to it (by the way, this is something you would also need to do in a "search" function). Assuming that its function deletar also receive a Pessoa as a parameter:

void deletar( cel **lst, Pessoa *pessoa){
   cel *p;
   p = (*lst);
   if((*lst)->seg ==NULL){
    printf("lista vazia");
   }
   else
    {
        /**** Acha a pessoa que quer deletar ****/
        while ( p != NULL && p->conteudo != pessoa )
            p = p->seg;
        if ( p == NULL ) {
            printf("a pessoa nao esta na lista");
            return;
        }

        /**** O resto do seu código está quase ok ****/
        if ( p->seg != NULL )
            p->seg->ant = p->ant;
        p->ant->seg = p->seg;
        free(p);
   }
}

Warning: I don’t have much experience with C, this comparison of conteudo with pessoa may be incorrect (but the general idea is this), check operations with pointer.

For this to work, however, it is necessary to fix also a problem with its function inserir_inicio. Where do you do:

nova->seg = (*lst) ;

Probably what you want is:

nova->ant = (*lst) ;

Otherwise, the ant of the new cell would be null, violating the double-chained list structure.

  • 1

    Because p->seg->ant = lst;? That way the pointer ant of the element following the one being excluded will point to the lst, that should be *lst because this is pointer to pointer. I think it would be more interesting this way: p->seg->ant = p->ant;. This way the previous pointer of the next will point to the previous of the current.

  • @Daviaragao That’s right, thanks! When I adapted the AP code, I didn’t realize that this part was referring to the head of the list (and not to the current node).

  • Thanks I managed to fix your code worked perfectly!

Browser other questions tagged

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