This is java FixedSizedStack . Frankly, Deques are more likely to be valuable as
ID: 3592803 • Letter: T
Question
This is java
FixedSizedStack. Frankly, Deques are more likely to be valuable as an instrument of instruction (teaching you about linked structures, so you're prepared to understand graphs and trees) than in code you'll write in the future. I know of only a couple of real-world applications:
the A-Steal job scheduling algorithm (read about it here, if interested)
A fixed sized stack, in which you may push and pop off the front of the stack as usual, but you also remove items off the back of the stack once capacity has been reached.
Today, you will implement the second one in FixedSizedStack.java, which will be a client of the Deque.
This class should primarily involve pass-through code, making liberal use of the Deque API. For example, a call to push will simply call addFirst, then possibly removeLast.
Explanation / Answer
public class FixedStack { private T[] stack; private int size; private int top; public FixedStack(int size) { this.stack = (T[]) new Object[size]; this.top = -1; this.size = size; } public void push(T obj) { if (top >= size) throw new IndexOutOfBoundsException("Stack size = " + size); stack[++top] = obj; } public T pop() { if (top < 0) throw new IndexOutOfBoundsException(); T obj = stack[top--]; stack[top + 1] = null; return obj; } public int size() { return size; } public int elements() { return top + 1; } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.