Java Program. Please read all and only answer if you understand the problem. (It
ID: 3715599 • Letter: J
Question
Java Program. Please read all and only answer if you understand the problem.
(It is also in the book: Object-oriented Data Structures Using Java By Nell B. Dale, Daniel T. Joyce, Chip Weems. )
The Stack ADT.
We decide to add a new operation to our Stack ADT called popTop.
We add the following code to our StackInterface interface:
public T popTop() throws StackUnderflowException:
//Throws StackUnderflowException if this stack is empty.
//otherwise removes and returns top element from this stack.
An operation like this is often included for stacks.
Implement the popTop method for the LinkedStack class.
Explanation / Answer
// Effect: Adds item to the top of this stack.
// Postcondition: If (this stack is full)
// an unchecked exception that communicates
// 'push on stack full' is thrown
// else
// item is at the top of this stack.
public T pop() throws StackUnderflowException;
================================================
public T pop() {
T x = null;
if (!isEmpty()) {
x = top->data;
top = top->next;
} else
throw new StackUnderflowException("Pop attempted on an empty stack.");
return x;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.