Owk i have updated my code a little
Now i have no longer a infinite loop but my loop doesnt stop at the right time.
Wanted output : 1 2 3 4 5
output : 1 2 3 4 5 5 4 5 5 4 5 5 4 5 5 2 3 4 5 4 5 5 3 4 5 5 4 5 5 stop
I dont see the problem? Because my list is 1 2 3 4 5 NULL and it needs to stop at NULL
Code:
#include <stdio.h>
#include <stdlib.h>
typedef int element;
struct cell {
element e;
struct cell *p;
};
typedef struct cell* cel;
cel addInFront (cel l, element i);
void writeList (cel l);
int main (void){
cel list;
list = (cel) NULL;
list = addInFront (list, 5);
list = addInFront (list, 4);
list = addInFront (list, 3);
list = addInFront (list, 2);
list = addInFront (list, 1);
writeList (list);
return 0;
}
void writeList (cel l){
while (l != (cel)NULL){
printf("\t %d", l->e);
l = l->p;
writeList (l);
}
}
cel addInFront ( cel l, element i){
cel p;
p = (cel) malloc (sizeof(struct cell));
p->e = i;
p->p = l;
return p;
}