Stack Interface
import java.util.EmptyStackException;
/*
* Interface for a stack : collection of objects over principle of LIFO , including main
* method from java.util.Stack
* @see EmptyStackException
*/
public interface Stack<E> {
//@return number of elements in stack
public int size();
//@return true if the stack is empty , false otherwise
public boolean isEmpty();
/*
* @return top element in the stack
* @exception EmptyStackException if the stack is empty.
*/
public E top() throws EmptyStackException;
//@param element to be inserted
public void push(E element);
/*
* @return element removed
* @exception EmptyStackException if the stack is empty
*/
public E pop();
}
Stack Implementation
import java.util.EmptyStackException;
/*
* Implementation of the stack Abstract Data Type (ADT) using fixed length array
* An exception is thrown if a push operation is attempted when the size of the
* stack is equal to the length of the array
*/
public class ArrayStack<E> implements Stack<E> {
protected int capacity; // actual capacity of the stack array
public static final int CAPACITY = 1000; // default array capacity
protected E S[]; // generic array used to implement the stack
protected int top = -1; // index of the top of the stack
// constructor
public ArrayStack() {
this(CAPACITY); // default capacity
}
public ArrayStack(int cap) {
capacity = cap;
S = (E[]) new Object[capacity]; // compiler may generate warning , but
// chillax , its ok
}
@Override
public int size() {
return (top + 1);
}
@Override
public boolean isEmpty() {
return (top < 0);
}
@Override
public E top() throws EmptyStackException {
if (isEmpty())
throw new EmptyStackException() ; //"Stack Is Empty";
return S[top];
}
@Override
public void push(E element) throws FullStackException {
if (size() == capacity)
throw new FullStackException("Stack is full");
S[++top] = element;
}
@Override
public E pop() throws EmptyStackException {
E element;
if (isEmpty())
throw new EmptyStackException("Stack Is Empty");
element = S[top];
S[top--] = null; // dereference S[top] for garabage collection
return element;
}
public String toString() {
String s;
s = "[";
if (size() > 0)
s += S[0];
if (size() > 1)
for (int i = 1; i <= (size() - 1); ++i) {
s += ", " + S[i];
}
return s + "] ";
}
// prints status info about recent operation on stack
public void status(String op, Object element) {
System.out.println("--------> " + op); // print this operation
System.out.println(" , returns " + element); // what was returned
System.out.println("result: size = " + size() + " , isEmpty = "
+ isEmpty());
System.out.println(" , stack: " + this);// contents of the stack i.e a
// call to toString(
}
public static void main(String[] args ){
Object o;
ArrayStack<Integer> A = new ArrayStack<>();
A.status("new ArrayStack<Integer> A ", null);
A.push(1);
A.status("A.push(1) ", null);
o = A.pop();
A.status(" A.pop() ", o);
A.push(7);
A.status(" A.push(7) ", null);
o = A.pop();
A.status("A.pop() ", o);
ArrayStack<String> B = new ArrayStack<>();
B.status("new ArrayStack<String> B ", null);
B.push("dont");
B.status("B.push(\"dont\") ", null);
B.push("stop");
B.status("B.push(\"stop\") ", null);
B.push("till");
B.status("B.push(\"till\") ", null);
B.push("u");
B.status("B.push(\"u\") ", null);
B.push("get");
B.status("B.push(\"get\") ", null);
B.push("enough");
B.status("B.push(\"enough\") ", null);
o = B.pop();
B.status("B.pop() ", o);
}
}
Result -
--------> new ArrayStack<Integer> A
, returns null
result: size = 0 , isEmpty = true
, stack: []
--------> A.push(1)
, returns null
result: size = 1 , isEmpty = false
, stack: [1]
--------> A.pop()
, returns 1
result: size = 0 , isEmpty = true
, stack: []
--------> A.push(7)
, returns null
result: size = 1 , isEmpty = false
, stack: [7]
--------> A.pop()
, returns 7
result: size = 0 , isEmpty = true
, stack: []
--------> new ArrayStack<String> B
, returns null
result: size = 0 , isEmpty = true
, stack: []
--------> B.push("dont")
, returns null
result: size = 1 , isEmpty = false
, stack: [dont]
--------> B.push("stop")
, returns null
result: size = 2 , isEmpty = false
, stack: [dont, stop]
--------> B.push("till")
, returns null
result: size = 3 , isEmpty = false
, stack: [dont, stop, till]
--------> B.push("u")
, returns null
result: size = 4 , isEmpty = false
, stack: [dont, stop, till, u]
--------> B.push("get")
, returns null
result: size = 5 , isEmpty = false
, stack: [dont, stop, till, u, get]
--------> B.push("enough")
, returns null
result: size = 6 , isEmpty = false
, stack: [dont, stop, till, u, get, enough]
--------> B.pop()
, returns enough
result: size = 5 , isEmpty = false
, stack: [dont, stop, till, u, get]
No comments:
Post a Comment