Jump to content

[Dúvida] Listas Ligadas


rgcouto
 Share

Recommended Posts

Boas,

Estou aqui com uma dúvida, alguém me pode dar o código de inserção à cabeça de Listas Ligadas? É que tinha o código, mas entretanto nao sei onde o pûs :/

Abraço..

inserir á cabeça?

Ora bem, deixa ver se ainda me lembro disso:

typedef struct coisasInuteis
{
int b;
char nome[10];
struct coisasInuteis *next;
} *MesmoInuteis;



int insere(MesmoInuteis realmenteInuteis, int bla, char uuuu[10])
{
MesmoInuteis * aux;
aux=(MesmoInuteis*)malloc(sizeof(MesmoInuteis));
aux->b=bla;
strcpy(aux->nome, uuuu);
aux->next=realmenteInuteis;
return 0;
}

Acho que é assim, não testei, mas pelo que me lembro, truclas =D

Abraços BTIII

Órale MI RAZA, MY BLOOD HOLMES BELONGS TO ME ÉSE...

EENNNGGGGEEEENNNNNNNHHHHAAAARRRRRIIIIIIIIIIAAAAAAAINFORMATICASISTEMAS....

Link to comment
Share on other sites

inserir á cabeça?

Ora bem, deixa ver se ainda me lembro disso:

typedef struct coisasInuteis
{
int b;
char nome[10];
struct coisasInuteis *next;
} *MesmoInuteis;



int insere(MesmoInuteis realmenteInuteis, int bla, char uuuu[10])
{
MesmoInuteis * aux;
aux=(MesmoInuteis*)malloc(sizeof(MesmoInuteis));
aux->b=bla;
strcpy(aux->nome, uuuu);
aux->next=realmenteInuteis;
return 0;
}

Acho que é assim, não testei, mas pelo que me lembro, truclas =D

Abraços BTIII

acho que tem aí alguns erros... o MesmoInuteis já é um apontador pa estrutura, logo o aux vai ser um apontador para apontador e n tás a modificar a lista inicial, acho que o melhor seria a função retornar MesmoInuteis, algo tipo:

MesmoInuteis insere(MesmoInuteis realmenteInuteis, int bla, char uuuu[10])
{
MesmoInuteis aux;
aux=(MesmoInuteis)malloc(sizeof(MesmoInuteis));
aux->b=bla;
strcpy(aux->nome, uuuu);
aux->next=realmenteInuteis;
return aux;
}
Link to comment
Share on other sites

é pá é provavel, nada que um compilador não resmunga-se, tirando não devlver a lista.

Ele disse que queria inserir á cabeça, não disse que queria devolver 😉

fiu

😛

Nata79, por acaso voce não ser o pastel...

Órale MI RAZA, MY BLOOD HOLMES BELONGS TO ME ÉSE...

EENNNGGGGEEEENNNNNNNHHHHAAAARRRRRIIIIIIIIIIAAAAAAAINFORMATICASISTEMAS....

Link to comment
Share on other sites

Nata79, por acaso voce não ser o pastel...

Meninos, PM ftw 😉

Nata79, ali na linha

aux=(MesmoInuteis)malloc(sizeof(MesmoInuteis));

estás a reservar espaço para um apontador. É preciso adicionar outra definição no typedef para alocar espaço correctamente:

typedef struct _coisasInuteis
{
/* (...) */
} CoisasInuteis, *MesmoInuteisP;

/* (...) */
aux=(MesmoInuteisP)malloc(sizeof(CoisasInuteis));
/* (...) */

Desaparecido.

Link to comment
Share on other sites

sim... s o resultado d strcmp for positivo a segunda string é "maior" que a primeira, s for negativo é o contrário e se for 0 são iguais... axo eu que é mais ou menos isto... faço sempre uma confusão dos diabos com o strcmp...

Nata, realmente acho que fizeste confusão:

strcmp( s, v) > 0 ----> s é "maior" que v

strcmp(s, v) == 0 -----> s é igual a v

strcmp(s,v) <0 ------> s é "menor" que v

Link to comment
Share on other sites

Eu já sei inserir à cabeça e no fim da lista.

E se quisermos fazer a inserção no meio da lista,

Por exemplo:

typedef struct celula *LInt;
struct celula{
  int valor;
  LInt next;
};

LInt insert_at (LInt, int, int)

Em que LInt é a lista, o primeiro int é o inteiro que quero inserir e o segundo int é a posição. Se a posição for maior que o tamanho da lista o valor é inserido no fim desta.

Link to comment
Share on other sites

Já tenho aqui algo que acho que funciona. Fica aqui pra quem quiser. Agradecimentos a quem me fez isto e me aturou pra me explicar.  😛

typedef struct listaligada{

int valor;
struct listaligada *prox;

} * LInt;



LInt insert_at (LInt l, int v, int p){

int i;
LInt k, ik, novo;

if(l==NULL){
	l=(LInt) malloc(sizeof(struct listaligada));
	l->valor=v;
	l->prox=NULL;

}else if (p==0){
	LInt aux;
	aux=(LInt) malloc (sizeof(struct listaligada));
	aux->valor=v;
	aux->prox=l;
	l=aux;
        }else{
	i=0;
	k=l;

	while(i<p || k!=NULL){
	  ik=k; //guarda o anterior
	  i++;  //aumenta o contador
		  k=k->prox; //avança na lista
	}

	novo=(LInt) malloc(sizeof(struct listaligada));
	novo->valor=v;
	novo->prox=k;
	ik->prox=novo;
}
   return l;

}
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
 Share

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