30/01/16 16:41
sceva95
salve a tutti, ho un problema con questo programma e non riesco a trovare una soluzione: quando lo lancio dopo aver completato il ciclo for per la seconda scanf va in loop e non mi da più nulla, cosa ho sbagliato?
#include <stdio.h>
#include <stdlib.h>
struct list{
int value;
struct list *next;
struct list *prec;
};
void print (struct list *li){
while(li != NULL){
printf("%i ", li->value);
li = li->next;
}
}
void destroy (struct list *l){
if (l != NULL){
struct list *c = l;
struct list *p = NULL;
while(c!= NULL){
if ( p!= NULL)
free(p);
};
};
}
int main (void){
int n = 10;
int i;
struct list *p = (struct list * ) malloc(sizeof(struct list));
struct list *tail;
scanf("%d", &p->value);
tail = p;
while ( n != 1){
tail->next = (struct list *) malloc(sizeof(struct list));
tail = tail->next;
scanf("%d", &tail->value);
tail->prec = p;
p = p->next;
n--;
};
tail->next = NULL;
print (p);
destroy(p);
}
#include <stdio.h>
#include <stdlib.h>
struct list{
int value;
struct list *next;
struct list *prec;
};
void print (struct list *li){
while(li != NULL){
printf("%i ", li->value);
li = li->next;
}
}
void destroy (struct list *l){
if (l != NULL){
struct list *c = l;
struct list *p = NULL;
while(c!= NULL){
if ( p!= NULL)
free(p);
};
};
}
int main (void){
int n = 10;
int i;
struct list *p = (struct list * ) malloc(sizeof(struct list));
struct list *tail;
scanf("%d", &p->value);
tail = p;
while ( n != 1){
tail->next = (struct list *) malloc(sizeof(struct list));
tail = tail->next;
scanf("%d", &tail->value);
tail->prec = p;
p = p->next;
n--;
};
tail->next = NULL;
print (p);
destroy(p);
}
aaa