c - Pointers to pointers - linked list mess -
i'm writing simple c program manage linked list defined follow:
typedef struct node { int value; struct node *next; } *list;
i reviewed code , seems okay when printing results not working well.
my main, problems on comments:
int main(void) { list n = list_create(1); insert(n, 2); insert(n, 3); insert(n, 5); insert(n, 4); //something here not work properly. produces following output: //value: 1 //value: 2 //value: 3 //value: 4 //where value 5? print_list(n); delete(n, 3); print_list(n); return 0; }
i don't know destroying list structure. these functions, debug, if kind.
list list_create(int value) { list new = malloc(sizeof(struct node)); new->value = value; new->next = null; return new; } list new_node(int value, list next_node) { list new = malloc(sizeof(struct node)); new->value = value; new->next = next_node; return new; } void print_list(list l) { list *aux; (aux = &l; (*aux) != null; aux = &((*aux)->next)) printf("valor: %d\n", (*aux)->value); } void insert(list l, int value) { list *p; (p = &l; (*p) != null; p = &((*p)->next)) if ((*p)->value > value) { list tmp = *p; list new = new_node(value, tmp); *p = new; break; } *p = new_node(value, null); } void delete(list l, int value) { list *p; (p = &l; (*p) != null; p = &((*p)->next)) if ((*p)->value == value) { list del = (*p); (*p) = ((*p)->next); free(del); break; } }
this code has (at least) 2 bugs:
the line
if ((*p)->value > value){
means if start list 1 first value , try insert 2,3,4..., body of 'if' statement never runs, nothing ever gets inserted.
- if insert value below starting value, have modify list pointer itself. however, @eof alluded, trying modify value passed function taking address. won't work. &l not give address of list passed, gives address of local copy on insert()'s stack. better off modifying values of first element of list 'in place'. if want make list parameter mutable, you'll need pass list *, , call function address of list (e.g. insert(&n,2); ) delete() function suffers same problem - try deleting first element of list.
try insert function:
void insert(list l, int value) { list p; // find end of list or highest item less value for(p = l; p->next != null && p->next->value < value; p = p->next); if (p->value >= value) { // over-write p new value, , insert p new 1 after. // saves having modify l itself. int tmpval = p->value; p->value = value; p->next = new_node(tmpval, p->next); } else { // insert new item after p p->next = new_node(value, p->next); } }
a comment: possible way using pointers not helping debugging process.
for example, print_list() re-written this:
void print_list(list l){ list aux; for(aux = l; aux != null; aux = aux->next) printf("valor: %d\n", aux->value); }
and still behave same. practice not 'hide' pointer-like nature of pointer including '*' in typedef. example, if define list this:
typedef struct node{ int value; struct node *next; } list
and pass functions this:
my_func(list *l, ...)
then it'll make of these issues more apparent. hope helps.
Comments
Post a Comment