|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Apr 2002
Città: Vigevano(PV)
Messaggi: 2124
|
C e liste
Codice:
#include <stdio.h> #include <string.h> #include <assert.h> #include <malloc.h> struct List { int el; struct List *next; struct List *prev; }; void init(struct List *aux, int el); void add(struct List *aux, int el); void init(struct List *aux, int el) { assert(aux != NULL); aux->el = el; aux->next = NULL; aux->prev = NULL; } void add(struct List *aux, int el) { assert(aux != NULL); while(aux->next != NULL) aux = aux->next; struct List *tmp = (struct List *)malloc(sizeof(struct List)); tmp->el = el; tmp->prev = aux; tmp->next = NULL; aux->next = tmp; } int main(int argc, char *argv[]) { struct List *head = (struct List *)malloc (sizeof(struct List)); init(head, 10); add(head, 9); add(head, 8); while(head->prev != NULL) { printf("%d\n", head->el); head = head->prev; } char c; scanf("%s", &c); return 0; } Tnk
__________________
Gnu/Linux User ![]() |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Perché head punta sempre al primo elemento, quindi head->prev è sempre nullo.
__________________
0: or %edi, %ecx; adc %eax, (%edx); popf; je 0b-22; pop %ebx; fadds 0x56(%ecx); lds 0x56(%ebx), %esp; mov %al, %al andeqs pc, r1, #147456; blpl 0xff8dd280; ldrgtb r4, [r6, #-472]; addgt r5, r8, r3, ror #12 |
![]() |
![]() |
![]() |
#3 |
Senior Member
Iscritto dal: Apr 2002
Città: Vigevano(PV)
Messaggi: 2124
|
Codice:
#include <stdio.h> #include <string.h> #include <assert.h> #include <malloc.h> struct List { int el; struct List *next; struct List *prev; }; void init(struct List *aux, int el); void add(struct List *aux, int el); void init(struct List *aux, int el) { assert(aux != NULL); aux->el = el; aux->next = NULL; aux->prev = NULL; } void add(struct List *aux, int el) { assert(aux != NULL); while(aux->next != NULL) aux = aux->next; struct List *tmp = (struct List *)malloc(sizeof(struct List)); tmp->el = el; tmp->prev = aux; tmp->next = NULL; aux->next = tmp; } int main(int argc, char *argv[]) { struct List *head = (struct List *)malloc (sizeof(struct List)); init(head, 10); add(head, 9); add(head, 8); add(head, 7); while(head->next != NULL) { printf("%d\n", head->el); head = head->next; } puts(""); while(head->prev != NULL) { printf("%d\n", head->el); head = head->prev; } char c; scanf("%s", &c); return 0; } Ma perche mi stampa: 10 9 8 7 8 9 Il 7 nella prima parte e il 10 nella seconda perche nn me li stampa?? So che sono dumande stupide cmq tnk ilsensine x la pazienza
__________________
Gnu/Linux User ![]() |
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Perché nel primo while salti l'elemento con next==NULL, nel secondo salti quello con prev==NULL.
__________________
0: or %edi, %ecx; adc %eax, (%edx); popf; je 0b-22; pop %ebx; fadds 0x56(%ecx); lds 0x56(%ebx), %esp; mov %al, %al andeqs pc, r1, #147456; blpl 0xff8dd280; ldrgtb r4, [r6, #-472]; addgt r5, r8, r3, ror #12 |
![]() |
![]() |
![]() |
#5 |
Senior Member
Iscritto dal: Feb 2002
Città: Trento
Messaggi: 962
|
Scusate se entro nella vostra discussione (peraltro seria) con questo intervento irriverente ma... sembrate l'allievo Jedi e il suo maestro!
![]() Luc@s : (Domanda lunghissima) Perchè? ilsensine: Risposta stringata e concisa Luc@s: (Domanda lunghissima, cambia poco rispetto a prima) Perchè? ilsensine: Risposta stringata e concisa ... e potreste continuare a lungo... ![]()
__________________
"Et Eärallo Endorenna utúlien. Sinome maruvan ar Hildinyar tenn' Ambar-metta!" -- Aragorn Elessar, Heir of Isildur Mixmar -- OpenSuSE 11.1 on AMD 64 3000+ on DFI LanParty nF4-D | GeForce 6600 GT + Thermaltake Schooner on Samsung 710N Storage -- ( 2 x Hitachi Deskstar 80 Gb + 1 x Hitachi 250 Gb ) = 1 RAID 5 + 1 Storage space LaCie Ethernet Disk Mini 250 Gb | HP - DV2150 EL MILAN CLAN |
![]() |
![]() |
![]() |
#6 | |
Senior Member
Iscritto dal: Nov 2003
Messaggi: 980
|
Quote:
![]() comunque ![]() |
|
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 22:21.