Linked Lists Part II: More Push and Pop functions

Channel:
Subscribers:
2,640
Published on ● Video Link: https://www.youtube.com/watch?v=lmMLsEqLHyg



Duration: 1:03:06
316 views
6


Whenever working with pointers, there's a couple rules you should always follow:

1) Any time there is an arrow operator, ask yourself, "Am I confident that this pointer is not going to be null?" If you're not confident, then add a check, like this:
if (!ptr) die(); // or handle it however you want

!ptr is true when ptr is 0, aka, nullptr, aka NULL

2) Whenever you work with a linked list, always sketch it out on pen and paper, and see if any pointers need to be updated when nodes get added or deleted from the data structure.

3) Whenever working with classes, always think about your class invariants, the things that must always be true after one of your member functions returns. For linked lists, we can come up with a couple: "Head must always point at the front of the list, or NULL if the list is empty", "Tail must always point at the end of the list, or NULL if the list is empty", "SIZE must always have the current size of the list" and so forth. Every time you have a non-const method, you must super extra double check that none of the invariants can be violated by your code.







Tags:
csci 41
linked lists
c++
push
pop