c - Create linked list from elements of a Binary tree at a certain depth -
i trying build linked list, elements @ depth
i came this:
void nivel(abin a, int k, slist *l, int level){ if (!a) return; if(k == level){ slist n = (slist)malloc(sizeof(struct slist)); n->value = a->value; n->next=(*l); (*l) = n; return; }else{ nivel(a->left, k, l, level+1); nivel(a->right, k, l, level+1); } }
it work
but exercise asks use header: slist nivel (abin a, int n)
i have tried void practice. can't figure out how make 1 returns linked lists.
data structure , binary tree:
typedef struct slist { int value; struct slist* next; } *slist; typedef struct arvbin* abin; typedef struct arvbin { int value; abin right; abin left; } arvb;
edit:
<---------------working header in exercise-----------> // thank politank-z
slist nivel_(abin a, int k){ slist *l; nivel(a, k, l, 1); return l; } void nivel(abin a, int k, slist *l, int level){ if (!a) return; if(k == level){ slist n = (slist)malloc(sizeof(struct slist)); n->value = a->value; n->next=(*l); (*l) = n; return; }else{ nivel(a->left, k, l, level+1); nivel(a->right, k, l, level+1); } }
regarding difficulty prototype: common restricted function prototype doesn't meet needs of implementation. in such cases, easier call function prototyped function shoehorn functionality prototype.
Comments
Post a Comment