Skip to content

Instantly share code, notes, and snippets.

@cp
Created December 9, 2014 22:44
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 cp/6f50fb52d3dfbb388060 to your computer and use it in GitHub Desktop.
Save cp/6f50fb52d3dfbb388060 to your computer and use it in GitHub Desktop.
java stack implimentation
class Stack {
private Object[] array;
private int tos = -1;
private int size;
Stack(int size) {
this.size = size;
array = new Object[size];
}
public void push(Object element) {
tos++;
array[tos] = element;
}
public Object pop() {
Object obj = array[tos];
tos--;
return obj;
}
public boolean is_full() {
return tos == size-1;
}
public boolean is_empty() {
return tos == -1;
}
public void printStack() {
for(int i=0; i<size; i++) {
System.out.println(array[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment