PDA

View Full Version : C e liste


Luc@s
26-05-2004, 11:16
#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;
}



Perche nn mi visualizza nulla anche se nn mi da errore???

Tnk

ilsensine
26-05-2004, 11:27
Perché head punta sempre al primo elemento, quindi head->prev è sempre nullo.

Luc@s
26-05-2004, 11:35
#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;
}



Cosi va + o - bene.
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

ilsensine
26-05-2004, 11:42
Perché nel primo while salti l'elemento con next==NULL, nel secondo salti quello con prev==NULL.

Mixmar
26-05-2004, 21:11
Scusate se entro nella vostra discussione (peraltro seria) con questo intervento irriverente ma... sembrate l'allievo Jedi e il suo maestro! :asd:

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... :burp:

kk3z
27-05-2004, 12:49
Originariamente inviato da Mixmar
Scusate se entro nella vostra discussione (peraltro seria) con questo intervento irriverente ma... sembrate l'allievo Jedi e il suo maestro! :asd:

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... :burp:

beh post lungo perchè è lungo il codice ;)

comunque :asd: