I'm currently studying Linked Stacks in Java.
One the things that caught my eyes is pop(), this is my prof's code:
Code:
public E pop() {
E savedInfo = top.info;
Elem<E> oldTop = top;
Elem<E> newTop = top.next;
top = newTop;
oldTop.info = null; // scrubbing the memory
oldTop.next = null; // scrubbing the memory
return savedInfo;
}
The way I understand it, "oldTop.info = null" and "oldTop.next = null" are unnecessary because the reference of top is being replaced by another reference in by top.next.
Correct me if I'm wrong.
Thanks.
Bookmarks