In: Computer Science
Complete the following program: LinkedStack.zip
package jsjf;
/**
* Represents a node in a linked list.
*/
public class LinearNode<T>
{
   private LinearNode<T> next;
   private T element;
   /**
   * Creates an empty node.
   */
   public LinearNode()
   {
       next = null;
       element = null;
   }
   /**
   * Creates a node storing the specified element.
   * @param elem element to be stored
   */
   public LinearNode(T elem)
   {
       next = null;
       element = elem;
   }
   /**
   * Returns the node that follows this one.
   * @return reference to next node
   */
   public LinearNode<T> getNext()
   {
       return next;
   }
   /**
   * Sets the node that follows this one.
   * @param node node to follow this one
   */
   public void setNext(LinearNode<T> node)
   {
       next = node;
   }
   /**
   * Returns the element stored in this node.
   * @return element stored at the node
   */
   public T getElement()
   {
       return element;
   }
   /**
   * Sets the element stored in this node.
   * @param elem element to be stored at this node
   */
   public void setElement(T elem)
   {
       element = elem;
   }
}
-----------------------------------------------------------------------
package jsjf;
/**
* Defines the interface to a stack collection.
*/
public interface StackADT<T>
{
   /**
   * Adds the specified element to the top of this
stack.
   * @param element element to be pushed onto the
stack
   */
   public void push(T element);
   /**
   * Removes and returns the top element from this
stack.
   * @return the element removed from the stack
   */
   public T pop();
   /**
   * Returns without removing the top element of this
stack.
   * @return the element on top of the stack
   */
   public T peek();
   /**
   * Returns true if this stack contains no
elements.
   * @return true if the stack is empty
   */
   public boolean isEmpty();
   /**
   * Returns the number of elements in this stack.
   * @return the number of elements in the stack
   */
   public int size();
   /**
   * Returns a string representation of this stack.
   * @return a string representation of the stack
   */
   public String toString();
---------------------------------------------------------------------
package jsjf;
import jsjf.exceptions.*;
/**
* Represents a linked implementation of a stack.
*/
public class LinkedStack<T> implements
StackADT<T>
{
   private int count;
   private LinearNode<T> top;
   /**
   * Creates an empty stack.
   */
   public LinkedStack()
   {
       count = 0;
       top = null;
   }
   /**
   * Adds the specified element to the top of this
stack.
   * @param element element to be pushed on stack
   */
   public void push(T element)
   {
       LinearNode<T> temp = new
LinearNode<T>(element);
       temp.setNext(top);
       top = temp;
       count++;
   }
   /**
   * Removes the element at the top of this stack and
returns a
   * reference to it.
   * @return element from top of stack
   * @throws EmptyCollectionException if the stack is
empty
   */
   public T pop() throws EmptyCollectionException
   {
       if (isEmpty())
           throw new
EmptyCollectionException("stack");
       T result =
top.getElement();
       top = top.getNext();
       count--;
       return result;
   }
   /**
   * Returns a reference to the element at the top of
this stack.
   * The element is not removed from the stack.
   * @return element on top of stack
   * @throws EmptyCollectionException if the stack is
empty
   */
   public T peek() throws EmptyCollectionException
   {
       // To be completed as a Programming
Project
      
       return null; // temp
   }
   /**
   * Returns true if this stack is empty and false
otherwise.
   * @return true if stack is empty
   */
   public boolean isEmpty()
   {
       // To be completed as a Programming
Project
      
       return true; // temp
   }
   /**
   * Returns the number of elements in this stack.
   * @return number of elements in the stack
   */
   public int size()
   {
       // To be completed as a Programming
Project
      
       return 0; // temp
   }
   /**
   * Returns a string representation of this stack.
   * @return string representation of the stack
   */
   public String toString()
   {
       // To be completed as a Programming
Project
      
       return ""; // temp
   }
}
}
----------------------------------------------------------------------
##LinkedStack Class With Complete solution
package Library;
import java.util.EmptyStackException;
public class LinkedStack<T> implements
StackADT<T>
{
private int count;
private LinearNode<T> top;
/**
* Creates an empty stack.
*/
public LinkedStack()
{
count = 0;
top = null;
}
/**
* Adds the specified element to the top of this stack.
* @param element element to be pushed on stack
*/
public void push(T element)
{
LinearNode<T> temp = new LinearNode<T>(element);
temp.setNext(top);
top = temp;
count++;
}
/**
* Removes the element at the top of this stack and returns a
* reference to it.
* @return element from top of stack
* @throws EmptyCollectionException if the stack is empty
*/
public T pop() throws EmptyCollectionException
{
   if (isEmpty())
throw new EmptyCollectionException("stack");
T result = top.getElement();
top = top.getNext();
count--;
return result;
}
/**
* Returns a reference to the element at the top of this
stack.
* The element is not removed from the stack.
* @return element on top of stack
* @throws EmptyCollectionException if the stack is empty
*/
public T peek() throws EmptyCollectionException
{
//complete solution as a Programming Project
   if (isEmpty())
       try {
           throw new
Exception("stack is empty");
       } catch (Exception e) {
          
e.printStackTrace();
       }
return top.getElement(); // Ans
}
/**
* Returns true if this stack is empty and false otherwise.
* @return true if stack is empty
*/
public boolean isEmpty()
{
//complete solution as a Programming Project
//if top is null means we do not have any elements in our
stack
return top==null; // Ans
}
/**
* Returns the number of elements in this stack.
* @return number of elements in the stack
*/
public int size()
{
//complete solutiond as a Programming Project
//In Push and pop we are updating count value so it is giving the
exact size of the stack
return count; // Ans
}
/**
* Returns a string representation of this stack.
* @return string representation of the stack
*/
public String toString()
{
//Complete solution as a Programming Project
//toString method defines the class meaning it represents the value
of member variable of the class
   String result = "";
LinearNode current = top;
while(current.getNext() != null){
result += current.getElement();
if(current.getNext() != null){
result += ", ";
}
current = current.getNext();
}
return "Total no of elements : "+count+"\nList: " + result;
}
}