StackArrayBased and QueueArrayBased class
These are the codes for StackArrayBased and QueueArrayBased classes. Together with these are StackInterface, StackException, QueueInterface and QueueException. All these classes has to be compiled besides StackArrayBased and QueueArrayBased.
Code:
/*
* StackArrayBased.java
*
* Created on February 13, 2007, 1:05 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author AIGINI
*/
public class StackArrayBased implements StackInterface
{
final int MAX_STACK = 50; // max. size of stack
private Object items[];
private int top;
public StackArrayBased()
{
items = new Object[MAX_STACK];
top = -1;
}
public boolean isEmpty()
{
return top < 0;
}
public boolean isFull()
{
return top == MAX_STACK - 1;
}
public void push (Object newItem) throws StackException
{
if ( !isFull() )
items[++top] = newItem;
else
throw new StackException ("Stk.Expn => " + " push : stack full");
}
public void popAll()
{
items = new Object[MAX_STACK];
top = -1;
}
public Object pop() throws StackException
{
if ( !isEmpty() )
return items[top--];
else
throw new StackException ("Stk.Excpn. ->> " + " pop: stack empty");
}
public Object peek() throws StackException
{
if ( !isEmpty() )
return items[top];
else
throw new StackException ("Stk.exn. -=> " + " peek - stack empty");
}
}
Code:
/*
* StackInterface.java
*
* Created on February 13, 2007, 1:06 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author AIGINI
*/
public interface StackInterface {
public boolean isEmpty();
// Determine whether the stack is empty;
// Returns true if the stack is empty, otherwise returns false.
public boolean isFull();
// Determine whether the stack is full;
// Returns true if the stack is full, otherwise returns false.
public void popAll();
// Removes all the items from the stack.
public void push(Object newItem) throws StackException;
// Adds an item to the top of a stack.
// Exception : Some implementations may throw StackException // when newItem cannot be placed on the stack.
public Object pop() throws StackException;
// Removes the top of a stack.
// Exception : Throws StackException if the stack is empty.
public Object peek() throws StackException;
// Retrieves the top of a stack.
// Exception : Throws StackException if the stack is empty.
}
Code:
/*
* StackException.java
*
* Created on February 13, 2007, 1:06 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/*
*
* @author AIGINI
*/
public class StackException extends java.lang.RuntimeException
{
public StackException (String A) {
super(A);
}
}
/ Code:
*
* QueueArrayBased.java
*
* Created on February 13, 2007, 12:48 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author AIGINI
*/
public class QueueArrayBased {
private final int MAX_QUEUE = 5; // maximum size of queue
private Object[] items;
private int front, back, count;
public QueueArrayBased() {
items = new Object[MAX_QUEUE];
front = 0;
back = MAX_QUEUE-1;
count = 0;
} // end default constructor // queue operations:
public boolean isEmpty() {
return count == 0;
} // end isEmpty
public boolean isFull() {
return count == MAX_QUEUE;
} // end isFull
public void enqueue(Object newItem) {
if (!isFull()) {
back = (back+1) % (MAX_QUEUE);
items[back] = newItem;
++count; }
else {
throw new QueueException("QueueException on enqueue: " + "Queue full");
} // end if
} // end enqueue
public Object dequeue() throws QueueException {
if (!isEmpty()) { // queue is not empty;
//remove front
Object queueFront = items[front];
front = (front+1) % (MAX_QUEUE);
--count;
return queueFront;
}
else {
throw new QueueException("QueueException on dequeue: "+ "Queue empty");
} // end if
} // end dequeue
public void dequeueAll() {
items = new Object[MAX_QUEUE];
front = 0;
back = MAX_QUEUE-1;
count = 0;
} // end dequeueAll
public Object peek() throws QueueException {
if (!isEmpty()) { // queue is not empty;
//retrieve front
return items[front];
}
else {
throw new QueueException("Queue exception on peek: " + "Queue empty");
} // end if
} // end peek
} // end QueueArrayBased
Code:
/*
* QueueInterface.java
*
* Created on February 13, 2007, 12:58 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author AIGINI
*/
// QueueInterface.java
// interface for the Queue ADT
public interface QueueInterface{
// isEmpty
// pre: none
// post: returns true if this Queue is empty, false otherwise
public boolean isEmpty();
// length
// pre: none
// post: returns the length of this Queue.
public int length();
// enqueue
// adds newItem to back of this Queue
// pre: none
// post: !isEmpty()
public void enqueue(Object newItem);
// dequeue
// deletes and returns item from front of this Queue
// pre: !isEmpty()
// post: this Queue will have one fewer element
public Object dequeue() throws QueueException;
// peek
// pre: !isEmpty()
// post: returns item at front of Queue
public Object peek() throws QueueException;
// dequeueAll
// sets this Queue to the empty state
// pre: !isEmpty()
// post: isEmpty()
public void dequeueAll() throws QueueException;
// toString
// overrides Object's toString() method
public String toString();
}
Code:
/*
* QueueException.java
*
* Created on February 13, 2007, 12:57 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author AIGINI
*/
public class QueueException extends java.lang.RuntimeException {
public QueueException(String s) {
super(s);
}
}