Skip to content

Instantly share code, notes, and snippets.

@jordancodurance
Created January 3, 2023 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordancodurance/6b5ef000864fe8a1d19729a397d7985e to your computer and use it in GitHub Desktop.
Save jordancodurance/6b5ef000864fe8a1d19729a397d7985e to your computer and use it in GitHub Desktop.
Special case null stack solution for https://www.codurance.com/katalyst/stack
interface Stack {
static Stack create(int size) {
if (size < 0) throw new IllegalStackSizeException();
if (size == 0) return new NullStack();
return new BoundedStack(size);
}
void push(int element);
boolean isEmpty();
// Other operations...
}
class NullStack implements Stack {
@Override public void push(int element) {
throw new StackOverFlowException(); // 0 stack always overflows
}
@Override public boolean isEmpty() {
return true; // 0 stack is always empty
}
}
class BoundedStack implements Stack {
@Override public void push(int element) {
// push code
}
@Override public boolean isEmpty() {
// empty check code
}
}
@jordancodurance
Copy link
Author

For the special case of a null stack, if you are using a language which supports polymorphism, consider creating a base abstract Stack, a BoundedStack with your current implementation, and a NullStack with only the code required for the 0 case. Then provide a way to construct either derivate of the stack based on the size provided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment