Lets Create Our Own Custom Stack.
package sorting;
public class IntStack {
private Object[] stack;
private int top;
private int size;
public IntStack() {
top = -1;
size = 2;
stack = new Object[2];
}
public IntStack(int size) {
top = -1;
this.size = size;
stack = new Object[this.size];
}
public boolean push(Object item) {
if (!isFull()) {
top++;
stack[top] = item;
return true;
} else {
return false;
}
}
public Object pop() {
return stack[top--];
}
public boolean isEmpty() {
return (top == -1);
}
private boolean isFull() {
return (top == stack.length - 1);
}
}
Comments
Post a Comment