PDA

Visualizza la versione completa : [C] Lista cotenente sempre NULL


Il-predestinato
15-06-2010, 10:33
Allora, ho fatto questo programma in C :


#include <stdio.h>
#include <stdlib.h>

typedef struct Structure {
int Data;
struct Structure *Next;
}Element;

Element *AddElements ();

Element *ViewElements (Element *List);

int main ()
{
Element *List;
AddElements ();
system ("CLS");
ViewElements (List);
system ("PAUSE");
return 0;
}

Element *AddElements ()
{
Element *List = NULL;
Element *Pointer;
int Elements;
int i;
printf ("Elementi della lista : ");
scanf ("%i", & Elements);
if (Elements == 0)
{
List = NULL;
}else{
List = (Element *)malloc(sizeof(Element));
printf ("Inserisci il 1 elemento : ");
scanf ("%i", & List -> Data);
Pointer = List;
for (i = 0 ; i < Elements - 1 ; i++)
{
Pointer -> Next = (Element *)malloc(sizeof(Element));
Pointer = Pointer -> Next;
printf ("Inserisci il %i elemento : ", i + 2);
scanf ("%i", & Pointer -> Data);
}
Pointer -> Next = NULL;
}
return List;
}

Element *ViewElements (Element *List)
{
printf ("Lista :\n\n");
while (List != NULL)
{
printf ("%i -----> ", List -> Data);
}
printf ("NULL\n\n");
}

Solo che ogni volta che ho finito di immettere gli n elementi mi stampa NULL...

Perchè ???

YuYevon
15-06-2010, 11:38
#include <stdio.h>
#include <stdlib.h>

typedef struct Structure {
int Data;
struct Structure *Next;
}Element;

Element *AddElements ();

Element *ViewElements (Element *List);

int main ()
{
Element *List;
List = AddElements ();
system ("CLS");
ViewElements (List);
system ("PAUSE");
return 0;
}

Element *AddElements ()
{
Element *List = NULL;
Element *Pointer;
int Elements;
int i;
printf ("Elementi della lista : ");
scanf ("%i", & Elements);
if (Elements == 0)
{
List = NULL;
}else{
List = (Element *)malloc(sizeof(Element));
printf ("Inserisci il 1 elemento : ");
scanf ("%i", & List -> Data);
Pointer = List;
for (i = 0 ; i < Elements - 1 ; i++)
{
Pointer -> Next = (Element *)malloc(sizeof(Element));
Pointer = Pointer -> Next;
printf ("Inserisci il %i elemento : ", i + 2);
scanf ("%i", & Pointer -> Data);
}
Pointer -> Next = NULL;
}
return List;
}

Element *ViewElements (Element *List)
{
printf ("Lista :\n\n");
while (List != NULL)
{
printf ("%i -----> ", List -> Data);
List = List -> Next;
}
printf ("NULL\n\n");
}

Il-predestinato
15-06-2010, 12:52
Grazie :zizi: :zizi: :zizi:

Loading