r/learnprogramming • • Apr 05 '22

Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them

Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.

Someone else who reads yours and does understand it, try to explain it to them to help them learn

1.6k Upvotes

738 comments sorted by

View all comments

Show parent comments

1

u/Liambass Apr 07 '22 edited Apr 07 '22

That description seems more like an iterative approach to me?

"remove bricks until there aren't any more".

I think a better example would be trying to disassemble the tower from the bottom;

void breakTower(Tower tower) {
    breakTowerRecursive(tower, tower.base);
}

void breakTowerRecursive(Tower tower, Brick brick){
    if (brick.above == null) {
        brick.remove();
    } else {
        breakTower(tower, brick.above);
        brick.remove();

1

u/[deleted] Apr 07 '22

That’s actually a good suggestion. I agree. I would suggest the code however to be two separate functions rather than two overloaded ones, since some people might not be familiar with function overloading and get confused.

P.s I also like your use of a linked list. I was thinking of a vector at first, but linked list makes it clearer.

1

u/Liambass Apr 07 '22

Yeah, that's fair. Let's see if I can't do a wee edit.