Use the Stack interface to implement the LinkedStack.java class. Stack.java public interface Stack { /** * Push an element onto the stack. * * @param element, a value to be pushed on the stack */ public void push(E element); /** * Pop the top element off the stack. */ public E pop(); /** * Return the top element on the stack. */ public E top(); /** * Return True if the stack contains no elements. * * @return true if there are no elements in the stack */ public boolean isEmpty(); } ***In LinkedStack.java, change only The methods with empty bodies Do not use a sentinel node. In this implementation you use an inner class That is, a class that is declared inside of another class. The Node class isn't needed by any other class, so it is declared as a private class inside the LinkedStack class. The Node class has two fields: element and next. Things to note in the class LinkedStack: The EmptyStackExceptionis being used, same as in the ArrayStack. The class StringJoineris imported for use in the toString() LinkedStack.java import java.util.EmptyStackException; import java.util.StringJoiner; public class LinkedStack implements Stack { private class Node { public E element; public Node next; public Node(E element) { this(element, null); } public Node(E element, Node next) { this.element = element; this.next = next; } } private Node top; /** * Constructor for objects of class LinkedStack */ public LinkedStack() { } @Override public void push(E element) { // Complete this method } @Override public E pop() { // Complete this method } @Override public E top() { // Complete this method } @Override public boolean isEmpty() { // Complete this method } @Override public int size() { // Complete this method }   @Override public String toString() { StringJoiner joiner = new StringJoiner(", ", "[", "]"); for (Node current = top; current != null; current = current.next) { joiner.add(current.element.toString()); } return joiner.toString

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 3PE
icon
Related questions
Question

**Use the Stack interface to implement the LinkedStack.java class.

Stack.java

public interface Stack<E> {
/**
* Push an element onto the stack.
*
* @param element, a value to be pushed on the stack
*/
public void push(E element);

/**
* Pop the top element off the stack.
*/
public E pop();

/**
* Return the top element on the stack.
*/
public E top();

/**
* Return True if the stack contains no elements.
*
* @return true if there are no elements in the stack
*/
public boolean isEmpty();
}

***In LinkedStack.java, change only The methods with empty bodies Do not use a sentinel node. In this implementation you use an inner class That is, a class that is declared inside of another class. The Node class isn't needed by any other class, so it is declared as a private class inside the LinkedStack class. The Node class has two fields: element and next. Things to note in the class LinkedStack: The EmptyStackExceptionis being used, same as in the ArrayStack. The class StringJoineris imported for use in the toString()

LinkedStack.java

import java.util.EmptyStackException;
import java.util.StringJoiner;
public class LinkedStack<E> implements Stack<E> {
private class Node {
public E element;
public Node next;

public Node(E element) {
this(element, null);
}

public Node(E element, Node next) {
this.element = element;
this.next = next;
}
}

private Node top;
/**
* Constructor for objects of class LinkedStack
*/
public LinkedStack() {
}
@Override
public void push(E element) {
// Complete this method
}

@Override
public E pop() {
// Complete this method
}

@Override
public E top() {
// Complete this method
}

@Override
public boolean isEmpty() {
// Complete this method
}

@Override
public int size() {
// Complete this method
}

 


@Override
public String toString() {
StringJoiner joiner = new StringJoiner(", ", "[", "]");
for (Node current = top; current != null; current = current.next) {
joiner.add(current.element.toString());
}
return joiner.toString();
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Concept of Threads
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning