Chained list without head in c

Asked

Viewed 2,274 times

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.

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

  • When posting a question it would be interesting to define well what you would like and what mistakes you are encountering.

  • @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.

  • @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.

  • 1

    @Pen painted ...or simply select all the code and click on the "Code Sample" button (Ctrl+K shortcut). :)

  • 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 Thanks man, now I will indent the fastest codes around here!

Show 3 more comments

1 answer

2


I see in your code that you already have two removal functions, but both are with the wrong return. The function remover is returning an entire, you declare the return of the same as void. Take out the return of function remover.

And in function remover_ini that should return a pointer to a cell you return an integer and then place a printf in return. Return the topo from the list already with the element removed.

Another problem in your code is that the variable menu has nothing in the first run and thus the program does nothing. Initializes menu, by declaring it at the top of the main.

That way the code at least compiles, executes and removes some elements:

#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)
{
    celula *lixo = topo;
    if(lixo == NULL)
    {
        printf("Lista vazia");
    }
    else
    {
        topo = topo->prox;
        printf("Elemento a ser excluido: %c", 10);
        printf("Nome: %s | Idade: %d \n",lixo->elemento->nome, lixo->elemento->idade);
        free(lixo);
    }
    return topo;

}

void remover(celula *topo)
{
    if(topo->prox == NULL)
    {
        celula *aux;
        aux = topo->prox;
        free(topo);
    }
    celula *lixo;
    lixo = topo->prox;
    topo->prox = lixo->prox;
    free(lixo);
}

void printar(celula *topo)
{
    celula *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;
    int menu = 1;

    p1.idade = 30;
    strcpy(p1.nome, "matheus");

    p2.idade = 18;
    strcpy(p2.nome, "mayara");

    p3.idade = 19;
    strcpy(p3.nome, "juca");

    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:
              topo = remover_ini(topo);
            break;
            case 5:
                remover(topo);
            break;
            default:
                printf("Opcao inexistente.");
             break;
            }
       }  
}
  • Hello still not removing at first

  • I was thinking with is a list without head , I wanted to create a head and make it point so I removed the first element but it’s not working

  • The remove function is working but the remove start function is not returning anything yet

  • @Matheus Francisco, corrected. The function remover_ini Menu option 4 makes the desired.

  • yes the function is doing what you want yes now I’m trying to create a function cell *Search(cell *top, Person p){ cellaux; aux = top->Prox; while(and I have q find type the sobnome but if Matheus has no surname it returns NULL understood ? } and one that will list all the elements but only the over name if not only will give the list size .. I think this is it. !

  • is a function that will fetch if the name has a surname .. type name Matheus has some surname if it has will return the surname ... http://i58.tinypic.com/ddpag8.png

  • Search (Person *p, cell top){ cellaux; aux = top->Prox; while(aux != NULL && aux->element->name != p) aux = aux->Prox; Return aux; } but I have to look up if the name has a subname char

  • Understood, can create an element in the structure pessoa to store the last name or you can check if there are spaces in the nome.

  • But Sopt is a Q&A that proposes a structure of questions and answers only. And each question should be subject to only one answer, so if the initial proposal of this question has been answered I suggest you accept it as right and open a second question by presenting this difficulty and the code you have already tried to create. So you’ll get more success!

  • I tried something so void Search(Person *p, cell top){ cellaux; aux = top->Prox; while(aux != NULL && aux->element->subname == p) aux = aux->Prox; Return aux; }

  • So it might work void Busca(celula *topo)&#xA;{ &#xA; celula *aux; &#xA; aux = topo; &#xA; while(aux != NULL)&#xA; {&#xA; if(aux->elemento->subnome)&#xA; printf("%s", aux->elemento->subnome);&#xA; aux = aux->prox;&#xA; }&#xA;}, but create another question with this subject you will get more success.

Show 6 more comments

Browser other questions tagged

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