Implement queue ADT (interface) with stack for the following: public class Main{
ID: 671404 • Letter: I
Question
Implement queue ADT (interface) with stack for the following:
public class Main{
public static void main(String[] args){
ArrayStack<Integer> stack = new ArrayStack <Integer>(4);
//Stack <Integer> stack = new NodeStack<Integer>();
process(stack);
}
private static void process(ArrayStack<Integer> stack){
try{
stack.push(-1);
stack.push(-2);
stack.push(-3);
stack.push(-4);
System.out.println(stack.pop());
stack.push(-5);
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.toString());
}catch(StackException e){
System.out.println(e);
}
}
}
public class ArrayStack<E> implements Stack<E>{
public static final int DEFAULT_CAPACITY = 1000;
private int top;
private int capacity;
private E s[];
public ArrayStack(){
this(DEFAULT_CAPACITY);
}
public ArrayStack(int capacity){
this.capacity = capacity;
top = -1;
s = (E[]) new Object[capacity];
}
// Returns the number of elements in the stack
public int size(){
return top + 1;
}
// Returns true if stack is empty.
public boolean isEmpty(){
return top < 0;
}
// Returns true if stack is full.
public boolean isFull(){
return top == capacity - 1;
}
// Inserts item at the top of the stack.
public void push(E item) throws StackException{
if(isFull())
throw new StackException("Stack is full");
s[++top] = item;
}
public interface Stack <E>{
// Returns the number of elements in the stack
public int size();
// Returns true if stack is empty.
public boolean isEmpty();
//Returns the top item from top of the stack.
//But does not remove it.
public E top() throws StackException;
// Inserts item at the top of the stack.
public void push(E item) throws StackException;
// Pops the top an item from top of the stack and returns it.
public E pop() throws StackException;
// Returns true if stack is full.
public boolean isFull();
}
Explanation / Answer
public class Main{
public static void main(String[] args) {
ArrayQueue<Integer> queue = new ArrayQueue<Integer>(4);
process(queue);
//ArrayStack<Integer> stack = new ArrayStack<Integer>(4);
// Stack <Integer> stack = new NodeStack<Integer>();
//process(stack);
}
private static void process(ArrayQueue<Integer> queue) {
try {
queue.push(-1);
queue.push(-2);
queue.push(-3);
queue.push(-4);
queue.print();
System.out.println(queue.pop());
queue.push(-5);
System.out.println(queue.pop());
System.out.println(queue.pop());
queue.print();
System.out.println(queue.toString());
} catch (StackException e) {
System.out.println(e);
}
}
}
class ArrayQueue<E> implements StackADT<E> {
public static final int DEFAULT_CAPACITY = 1000;
private int rear;
private int front;
private int capacity;
private E s[];
public ArrayQueue() {
this(DEFAULT_CAPACITY);
rear = -1;
front = -1;
}
public ArrayQueue(int capacity) {
this.capacity = capacity;
front = -1;
rear = -1;
s = (E[]) new Object[capacity];
}
// Returns the number of elements in the stack
public int size() {
return rear + 1;
}
// Returns true if stack is empty.
public boolean isEmpty() {
return rear < 0;
}
// Returns true if stack is full.
public boolean isFull() {
return rear == capacity - 1;
}
@Override
public E pop() throws StackException {
E item = null;
// TODO Auto-generated method stub
if (!isEmpty() && front >= 0) {
item = s[front];
for (int i = front ; i <= rear - 1; i++) {
s[i] = s[i + 1];
}
//System.out.println("Front : "+front+" rear : "+rear+" item: "+item);
// remove the rearest node
s[rear] = null;
rear--;
if(rear < 0)
front = -1;
}
return item;
}
public void print() {
for (int i = front; i <= rear; i++)
System.out.print(s[i] + " ");
System.out.println();
}
@Override
public void push(E item) throws StackException {
if (isFull())
throw new StackException("Stack is full");
if (isEmpty())
front = 0;
s[++rear] = item;
}
@Override
public E top() throws StackException {
// TODO Auto-generated method stub
if (!isEmpty() && front >= 0)
return s[front];
return null;
}
}
interface StackADT<E> {
// Returns the number of elements in the stack
public int size();
// Returns true if stack is empty.
public boolean isEmpty();
// Returns the top item from top of the stack.
// But does not remove it.
public E top() throws StackException;
// Inserts item at the top of the stack.
public void push(E item) throws StackException;
// Pops the top an item from top of the stack and returns it.
public E pop() throws StackException;
// Returns true if stack is full.
public boolean isFull();
}
------------------------------ Queuue ouptu ------------------------------
-1 -2 -3 -4
-1
-2
-3
-4 -5