Writing a LinkedList destructor?

后端 未结 6 1883
我在风中等你
我在风中等你 2020-12-30 05:36

Is this a valid LinkedList destructor? I\'m still sort of confused by them.

I want to make sure I\'m understanding this correctly.

 LinkedList::~Link         


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-30 05:50

    The condition "head;" should check that it is not null, but I'm not sure if it will work.

    Yes, "head" by itself is the same as "head != null" -- but why use a meaningless typing shortcut if even you find it confusing? It's only 6 more keystrokes (and generates identical machine code), so go for the long form.

    Additionally, your code is a bit more complicated than necessary because you are using a for() construct. Why not use a while()? Your code will be much cleaner.

    Finally, I realize you are doing this as a learning exercise, but keep in mind that list<> is in the standard library --- Linked lists are official a "Solved Problem".

提交回复
热议问题