c - Listing values from Linked list -
after inserting elements linked list, retrieve them.
this struct:
typedef struct node { int data; struct node *next; } element; typedef element* row; i start list :
void startrow(row *row){ *row = null; } after that, use function insert @ end:
void insertend(row *row, int value){ if(*row==null){ *row = (row) (malloc(sizeof(element))); if(*row==null){ return; }else{ (*row)->data = value; (**row).next = null; } } } and list values, use function (i plan on removing recursion, playing arround):
void listvalues(row row){ if(row==null){ return; } printf("%d\n", row->data); listvalues(row->next); } i call this, only outputs first element!
int main(){ row row; startrow(&row); insertend(&row, 10); insertend(&row, 20); listvalues(row); return 0; } why? shouldn't output complete list?
*row not null after first insert. next insert never happens.
void insertend(row *row, int value){ if(*row==null){ printf("inserting %d\n", value); *row = (row) (malloc(sizeof(element))); if(*row==null){ return; }else{ (*row)->data = value; (**row).next = null; } } } add debug printf , it'll become obvious what's happening.
i think there more fundamental problems though design!
you might want keep pointer start of list around otherwise it'll mighty difficult find again. , fast insert @ end might want keep pointer end of list well.
i'm assuming trying figure out yourself, won't give answer :)
Comments
Post a Comment