Java programming task: Provide a complete implementation of the LinkedStack clas
ID: 672027 • 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.
NOTE: In the Java API, class java.util.Stack<E> implements a stack; there is no stack interface.
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){
LinkedStack<String> s = new LinkedStack<>();
s.push("dave");
System.out.println(s.pop());
}
}
Explanation / Answer
class LinkedListEmptyException extends RuntimeException{
public LinkedListEmptyException(){
super();
}
public LinkedListEmptyException(String message){
super(message);
}
}
class StackEmptyException extends RuntimeException {
public StackEmptyException(){
super();
}
public StackEmptyException(String message){
super(message);
}
}
class Node {
public int data; // data in Node.
public Node next; // points to next Node in list.
public Node(int data){
this.data = data;
}
public void displayNode() {
System.out.print( data + " ");
}
}
class LinkedList {
private Node first; // ref to first link on list
public LinkedList(){
first = null;
}
public void insertFirst(int data) {
Node newNode = new Node(data); //Creation of New Node.
newNode.next = first; //newLink ---> old first
first = newNode; //first ---> newNode
}
public Node deleteFirst()
{
if(first==null){ //means LinkedList in empty, throw exception.
throw new LinkedListEmptyException("LinkedList doesn't contain any Nodes.");
}
Node tempNode = first; // save reference to first Node in tempNode- so that we could return saved reference.
first = first.next; // delete first Node (make first point to second node)
return tempNode; // return tempNode (i.e. deleted Node)
}
public void displayLinkedList() {
Node tempDisplay = first; // start at the beginning of linkedList
while (tempDisplay != null){ // Executes until we don't find end of list.
tempDisplay.displayNode();
tempDisplay = tempDisplay.next; // move to next Node
}
System.out.println();
}
}
class StackLinkedList{
LinkedList linkedList = new LinkedList(); // creation of Linked List
public void push(int value){
linkedList.insertFirst(value);
}
public void pop() throws StackEmptyException {
try{
linkedList.deleteFirst();
}catch(LinkedListEmptyException llee){
throw new StackEmptyException();
}
}
public void displayStack() {
System.out.print("Displaying Stack > Top to Bottom : ");
linkedList.displayLinkedList();
}
}
public class StackLinkedListApp {
public static void main(String[] args) {
StackLinkedList stackLinkedList=new StackLinkedList();
stackLinkedList.push(39); //push node.
stackLinkedList.push(71); //push node.
stackLinkedList.push(11); //push node.
stackLinkedList.push(76); //push node.
stackLinkedList.displayStack(); // display LinkedList
stackLinkedList.pop(); //pop Node
stackLinkedList.pop(); //pop Node
stackLinkedList.displayStack(); //Again display LinkedList
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.