Created
December 9, 2014 22:44
-
-
Save cp/6f50fb52d3dfbb388060 to your computer and use it in GitHub Desktop.
java stack implimentation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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