Skip to content

Stack Considerations

There are a few edge cases that need to be considered dependant on the type of stack (linked list or array) that is being implemented. Below are a list of those considerations.

  • Underflow: throw exceptions if pop from an empty stack.
  • Overflow: use resizing array for array implementations.
  • Null Items: we allow null items to be inserted
  • Loitering: Holding reference to an object when it is no longer needed.

In Java for example the below code would holding a reference to a pointer longer then required (loitering).

public String pop() { return s[--N]; }

To avoid this we can implement a pop as such where the garbage collector can reclaim memory.

public String pop()
{
String item = s[--N];
s[N] = null;
return item;
}