Jump to content

[Resolvido] Inserir ordenado lista ligada


Recommended Posts

Posted (edited)

Boa noite.

Tenho um código cujo objectivo é inserir de forma ordenada, por ano, numa lista ligada.

Contudo, quando compilo, dá erro e não estou a conseguir descobrir porquê.

Cá vai o código:


typedef struct listaEntradas {
int ano;
struct listaEntradas *seg;
} *LISTA_ENTRADAS, NODO_LISTA_ENTRADAS;

static LISTA_ENTRADAS *lent = NULL;


void inserir_ordenado_lista_entradas(int ano)
{
LISTA_ENTRADAS *lent_aux = NULL;
LISTA_ENTRADAS *pa = NULL;
LISTA_ENTRADAS *corr = *lent;
int count = 1;

lent_aux = (LISTA_ENTRADAS *) malloc(sizeof(NODO_LISTA_ENTRADAS));
lent_aux->ano = ano;
lent_aux->seg = NULL;

while((corr != NULL) && (count))
{
	if(ano < corr->ano) count = 0;
	else
	{
		pa = corr;
		corr = corr->seg;
	}
}
lent_aux->seg = corr;
if(pa == NULL) *lent = lent_aux;
else pa->seg = lent_aux;
}

Os erros que obtenho são:

listaEntradas.c: In function ‘inserir_ordenado_lista_entradas’:

listaEntradas.c:15: warning: initialization from incompatible pointer type

listaEntradas.c:19: error: request for member ‘ano’ in something not a structure or union

listaEntradas.c:20: error: request for member ‘seg’ in something not a structure or union

listaEntradas.c:24: error: request for member ‘ano’ in something not a structure or union

listaEntradas.c:28: error: request for member ‘seg’ in something not a structure or union

listaEntradas.c:31: error: request for member ‘seg’ in something not a structure or union

listaEntradas.c:32: warning: assignment from incompatible pointer type

listaEntradas.c:33: error: request for member ‘seg’ in something not a structure or union

Alguma ideia do que possa estar mal?

Obrigado.

Edited by Joao Miguel
Posted
typedef struct listaEntradas {
// ...
} *LISTA_ENTRADAS; // <-- é um ponteiro para a estrutura

// ...

void inserir_ordenado_lista_entradas(int ano)
{
       LISTA_ENTRADAS *lent_aux = NULL;  // <--- ponteiro para ponteiro para a estrutura
       LISTA_ENTRADAS *pa = NULL;        // <--- idem, aspas, aspas
       LISTA_ENTRADAS *corr = *lent;     // <--- ....
IRC : sim, é algo que ainda existe >> #p@p
Posted (edited)

Boa noite. Venho só dizer que consegui resolver o problema e já está a funcionar. Obrigado HappyHippyHippo, com os teus comentários consegui perceber uma coisa sobre apontadores que ainda não tinha percebido e por conseguinte, corrigir a função.

Deixo aqui o código corrigido:


typedef struct listaEntradas {
   int ano;
   struct listaEntradas *seg;
} *LISTA_ENTRADAS, NODO_LISTA_ENTRADAS;



static LISTA_ENTRADAS lent = NULL;

void inserir_ordenado_lista_entradas(int ano)
{
LISTA_ENTRADAS lent_aux = NULL;
LISTA_ENTRADAS pa = NULL;
LISTA_ENTRADAS corr = lent;
int count = 1;

lent_aux = (LISTA_ENTRADAS) malloc(sizeof(NODO_LISTA_ENTRADAS));
lent_aux->ano = ano;
lent_aux->seg = NULL;

while((corr != NULL) && (count))
{
	if(ano < corr->ano) count = 0;
	else
	{
		pa = corr;
		corr = corr->seg;
	}
}
lent_aux->seg = corr;
if(pa == NULL) lent = lent_aux;
else pa->seg = lent_aux;
}
Edited by Joao Miguel

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.