Java programming task: Provide a complete implementation of the LinkedStack clas
ID: 671976 • Letter: J
Question
Java programming task:
Provide a complete implementation of the LinkedStack class prodived and test it (add appropriate methods) on the following applications: Palindrome, Infix-to-Postfix, Parenthesis evaluator and Postfix evaluator. Include the ability to reinitialize the stack to empty and test for empty and full stack.
Provide a complete solution that includes a Unit test with the test data. Allow for presenting a different test data set (using scanner), explaining how to feed it into your class. The use of a switch statement may be hellpfull.
import java.util.*;
public class LinkedStack<E> implements StackInt<E> {
private static class Node<E> {
// Data Fields
/** The reference to the data. */
private E data;
/** The reference to the next node. */
private Node<E> next;
// Constructors
/**
* Creates a new node with a null next field.
* @param dataItem The data stored
*/
private Node(E dataItem) {
data = dataItem;
next = null;
}
/**
* Creates a new node that references another node.
* @param dataItem The data stored
* @param nodeRef The node referenced by new node
*/
private Node(E dataItem, Node<E> nodeRef) {
data = dataItem;
next = nodeRef;
}
}
/** The reference to the first stack node. */
private Node<E> topOfStackRef = null;
/**
* Insert a new item on top of the stack.
* @post The new item is the top item on the stack.
* All other items are one position lower.
* @param obj The item to be inserted
* @return The item that was inserted
*/
public E push(E obj) {
topOfStackRef = new Node<E>(obj, topOfStackRef);
return obj;
}
/**
* Remove and return the top item on the stack.
* @pre The stack is not empty.
* @post The top item on the stack has been
* removed and the stack is one item smaller.
* @return The top item on the stack
* @throws EmptyStackException if the stack is empty
*/
public E pop() {
if (empty()) {
throw new EmptyStackException();
} else {
E result = topOfStackRef.data;
topOfStackRef = topOfStackRef.next;
return result;
}
}
/**
* Return the top item on the stack.
* @pre The stack is not empty.
* @post The stack remains unchanged.
* @return The top item on the stack
* @throws EmptyStackException if the stack is empty
*/
public E peek() {
if (empty()) {
throw new EmptyStackException();
}
else {
return topOfStackRef.data;
}
}
/**
* See whether the stack is empty.
* @return true if the stack is empty
*/
public boolean empty() {
return topOfStackRef == null;
}
public static void main(String[] args){
}
}
Explanation / Answer
***Here is the code for stack interface***
public interface Stack <Item>
{
Item pop(); // return the top item and removes it from stack
void push(Item item); // adds an item to the stack
boolean isEmpty(); // returns true if stack is empty, false otherwise
int size(); // returns the number of items in stack right now
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.