Localhost Posted June 13, 2010 at 09:53 PM Report Share #333882 Posted June 13, 2010 at 09:53 PM Que inicio? here since 2009 Link to comment Share on other sites More sharing options...
casdio Posted June 13, 2010 at 10:37 PM Report Share #333890 Posted June 13, 2010 at 10:37 PM Por exemplo: Fiz este exemplo: #include <stdio.h> #include <stdlib.h> typedef struct sTeste { int elem; struct sTeste *next; } *Teste, NTeste; Teste add(Teste t, int x){ Teste *head; head=&t; if(t==NULL){ t=malloc(sizeof(struct sTeste)); t->elem=x; t->next=NULL; } else{ while(t->next!=NULL){ t=t->next; } t->next=malloc(sizeof(struct sTeste)); t->next->elem=x; t->next->next=NULL; } return *head; } int main(){ Teste t = NULL; t=add(t,1); t=add(t,2); t=add(t,3); t=add(t,4); while(t!=NULL){ printf("%d\n",t->elem); t=t->next; } return 0; } Supostamente, ao fazer print, ele devia imprimir o 1, 2, 3 e 4. Mas so imprime o 3 e o 4. Que e que esta mal? Link to comment Share on other sites More sharing options...
Localhost Posted June 13, 2010 at 10:42 PM Report Share #333891 Posted June 13, 2010 at 10:42 PM Na main declara mais um ponteiro chamado head, por exemplo, quando fazes a primeira adição de dados atribuis a head o conteúdo de teste, depois trabalhas com head para percorreres a lista. here since 2009 Link to comment Share on other sites More sharing options...
casdio Posted June 13, 2010 at 10:46 PM Report Share #333892 Posted June 13, 2010 at 10:46 PM Na main declara mais um ponteiro chamado head, por exemplo, quando fazes a primeira adição de dados atribuis a head o conteúdo de teste, depois trabalhas com head para percorreres a lista. Nao tou a perceber :$ Edit: ja percebi. Ty 😉 Link to comment Share on other sites More sharing options...
Localhost Posted June 13, 2010 at 10:48 PM Report Share #333893 Posted June 13, 2010 at 10:48 PM Na função main declaras outro ponteiro, quando chamares pela primeira vez a função que adiciona dados atribuis o conteudo do ponteiro que recebe o retorno dessa mesma função ao ponteiro que declaraste, ou seja, vais receber a cabeça da lista. here since 2009 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now