recursion - How to make recursive singly linked list (C++) -
my book asking me make recursive definition of singly linked list. have no idea @ how that. can please me out sample? thanks
it's normal linked list except iteration performed recursion rather loops.
first, little light reading: what recursion , when should use it?
for example, loop-based function find last node be:
node * getlast(node * current) { while (current->next == null) { // loop until no more nodes current = current.next; } return current; // return last node }
while recursive version checks if current node last , calls next node if there next node.
node * getlast(node * current) { if (current->next == null) { // found last node. return return current; } else { // see if next node last node return getlast(current->next); } }
Comments
Post a Comment