lasq Temat założony przez niniejszego użytkownika |
[C] Niepoprawne wyświetlanie zawartości listy jednokierunkowej » 2014-01-03 13:24:01 Problem polega na tym, że wyświetlana jest tylko ostatnio dodana zawartość, a reszta, to jakieś krzaki... Bardzo prosiłbym o kompetentną pomoc :-) #include <stdio.h> #include <stdlib.h>
struct Element { char * Text; struct Element * next; };
void dodaj( struct Element * lista ) { struct Element * nowy; char buf[ BUFSIZ ]; printf( "No dawaj: " ); fgets( buf,( int ) sizeof( buf ), stdin ); nowy = malloc( sizeof( struct Element ) ); nowy->Text = buf; nowy->next = lista; }
void wypisz( struct Element * lista ) { while( lista ) { printf( "%s\n", lista->Text ); lista = lista->next; } }
int main( void ) { struct Element * lista; lista = malloc( sizeof( struct Element ) ); lista->next = NULL; dodaj( lista ); dodaj( lista ); wypisz( lista ); return 0; }
|
|
maly |
» 2014-01-03 14:01:59 char buf[ BUFSIZ ]; zmienna lokalna zniknie po wyjściu z funkcji. nowy->next = lista; ??? chyba miałobyć lista->next = nowy; , brakuje jeszcze nowy->next = NULL; . printf( "%s\n", lista->Text ); a co jeśli niema tekstu? W drugim wywołaniu funkcji dodaj() argumentem powinno być lista->next . Pewnie cośtam będzie jeszcze źle:) |
|
lasq Temat założony przez niniejszego użytkownika |
» 2014-01-03 14:26:00 char buf[ BUFSIZ ]; A to bardzo źle, że zniknie po wyjściu? Przecież później już jej nie potrzebuje :-) Zmieniłem już na lista->next = nowy; i dodałem nowy->next = NULL; , ale ogólnie, to chciałem, żeby nowy element był na początku, a nie na końcu listy. Sugerujesz, że wyskakują mi krzaki, bo przy printf( "%s\n", lista->Text ); nie ma tekstu? Zmieniłem już na dodaj( lista->next ); , ale kod po tych wszystkich zmianach, daje nadal taki sam rezultat ;/ Tak, czy siak, dziękuję za zainteresowanie i mam nadzieję na jeszcze jakąś pomoc :-) A kod teraz wygląda tak: #include <stdio.h> #include <stdlib.h>
struct Element { char * Text; struct Element * next; };
void dodaj( struct Element * lista ) { struct Element * nowy; char buf[ BUFSIZ ]; printf( "No dawaj: " ); fgets( buf,( int ) sizeof( buf ), stdin ); nowy = malloc( sizeof( struct Element ) ); nowy->Text = buf; nowy->next = NULL; lista->next = nowy; }
void wypisz( struct Element * lista ) { while( lista ) { printf( "%s\n", lista->Text ); lista = lista->next; } }
int main( void ) { struct Element * lista; lista = malloc( sizeof( struct Element ) ); dodaj( lista ); dodaj( lista->next ); wypisz( lista ); return 0; }
|
|
maly |
» 2014-01-03 14:36:41 buf twórz dynamicznie(malloc). ale ogólnie, to chciałem, żeby nowy element był na początku |
Będziesz musiał sam się pomęczyć;) |
|
lasq Temat założony przez niniejszego użytkownika |
» 2014-01-03 19:32:17 Męczyłem się, męczyłem, aż w końcu napisałem sobie inny program, łatwiejszy do rozgryzienia. Ale nadal mam problem, żeby zapisywało na początku listy... Kombinowałem ze zmienną tymczasową w procedurze "dodaj", ale co najwyżej wyświetlało mi tylko ostatnio dodaną wartość. #include <stdio.h> #include <stdlib.h>
struct Element { int x; struct Element * next; };
void dodaj( struct Element * el, int a ) { el->x = a; el->next = malloc( sizeof( struct Element ) ); } int main( int argc, char * argv[] ) { struct Element * el; el = malloc( sizeof( struct Element ) ); dodaj( el, 5 ); dodaj( el->next, 6 ); dodaj( el->next->next, 7 ); el->next->next->next = NULL; while( el != NULL ) { printf( "%d ", el->x ); el = el->next; } return 0; }
|
|
« 1 » |