Jump to content

Lista duplamente ligada, dúvidas


alphasil

Recommended Posts

Oi

Tenho uma lista duplamente ligada pedida num dos exames que diz que tenho de fazer a estrutura e a função local em main. (feito)

Depois pede para criar uma função que simula o registo de um cliente acrescentando-o na lista.

Fiz assim seguindo um exercício de listas mas dá-me dois warnings que não percebo porquê?:

#include <stdio.h>
#include <stdlib.h>
typedef struct churr{
   char mat[100];
   char pedido[100];
   float preco;
}INFO;
typedef struct elemento{
   INFO nodo;
   struct elem *Seguinte;
   struct elem *Anterior;
}ELEM;
int recebeCliente(INFO info, ELEM **initlista, ELEM **fimlista)
{
   ELEM *novo;
   novo=(ELEM*)malloc(sizeof(ELEM));
   novo->nodo=info;
   novo->Seguinte=NULL;
   novo->Anterior=NULL;
   if(*initlista==NULL)
   {
    novo=*initlista;
    novo=*fimlista;
   }
   novo->Anterior=*fimlista;
   (*fimlista)->Seguinte=novo;
   *fimlista=novo;
return 0;
}
int main()
{
   ELEM *initlista=NULL, *fimlista=NULL;
   INFO info;
   printf("Matricula: \n");
   gets(info.mat);
   printf("Descricao: \n");
   gets(info.pedido);
   printf("Preco: \n");
   scanf("%f",&info.preco);
   getchar();
   recebeCliente(info,&initlista,&fimlista);
   return 0;
}

D:\2012\c_exe\Pergunta 1 Modulo 2\main.c||In function 'recebeCliente':|

D:\2012\c_exe\Pergunta 1 Modulo 2\main.c|29|warning: assignment from incompatible pointer type [enabled by default]|

D:\2012\c_exe\Pergunta 1 Modulo 2\main.c|30|warning: assignment from incompatible pointer type [enabled by default]|

||=== Build finished: 0 errors, 2 warnings ===|

Alguém me consegue explicar o porquê?

gmc11

 

Link to comment
Share on other sites

typedef struct elemento{
   INFO nodo;
   struct elem *Seguinte;
   struct elem *Anterior;
}ELEM;

Nao consigo descobrir a definicao da struct elem em lado nenhum.

So vejo a definicao duma struct churr e doutra struct elemento.

What have you tried?

Não respondo a dúvidas por PM

A minha bola de cristal está para compor; deve ficar pronta para a semana.

Torna os teus tópicos mais atractivos e legíveis usando a tag CODE para colorir o código!

Link to comment
Share on other sites

dá-me um segmentation fault aqui:

novo->Anterior=*fimlista;

A logica do teu programa nao esta bem.

Da maneira que o escreveste, o valor da variavel novo, nessa linha do programa, é NULL (copiado de *fimlista; que veio do main a NULL).

What have you tried?

Não respondo a dúvidas por PM

A minha bola de cristal está para compor; deve ficar pronta para a semana.

Torna os teus tópicos mais atractivos e legíveis usando a tag CODE para colorir o código!

Link to comment
Share on other sites

Resolvi a situação dos warnings com chavetas, agora criei uma função para listar e nada aparece...

#include <stdio.h>
#include <stdlib.h>
typedef struct churr{
   char mat[100];
   char pedido[100];
   float preco;
}INFO;
typedef struct elem{
   INFO nodo;
   struct elem *Seguinte;
   struct elem *Anterior;
}ELEM;
int recebeCliente(INFO info, ELEM **initlista, ELEM **fimlista)
{
   ELEM *novo;
   novo=(ELEM*)malloc(sizeof(ELEM));
   novo->nodo=info;
   novo->Seguinte=NULL;
   novo->Anterior=NULL;
   if(novo==NULL)
   {
    printf("Erro na lista...");
    return -1;
   }
   if(*initlista==NULL)
   {
    novo=*initlista;
    novo=*fimlista;
   }
   else
   {
    novo->Anterior=*fimlista;
    (*fimlista)->Seguinte=novo;
    *fimlista=novo;
   }
return 0;
}
int listar(ELEM *initlista)
{
   ELEM *aux;
   aux=initlista;
   while(aux!=NULL)
   {
    printf("%s %s %.2f", aux->nodo.mat, aux->nodo.pedido, aux->nodo.preco);
    aux=aux->Seguinte;
   }
   return 0;
}
int main()
{
   ELEM *initlista=NULL, *fimlista=NULL;
   INFO info;
   printf("Matricula: \n");
   gets(info.mat);
   printf("Descricao: \n");
   gets(info.pedido);
   printf("Preco: \n");
   scanf("%f",&info.preco);
   getchar();
   recebeCliente(info,&initlista,&fimlista);
   listar(initlista);
   return 0;
}
gmc11

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.