Question

In: Computer Science

Complete the following program: LinkedStack.zip package jsjf; /** * Represents a node in a linked list....

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
   }
}

}
----------------------------------------------------------------------

Solutions

Expert Solution

##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;

}


}


Related Solutions

Complete the following program:LinkedQueue.zip package jsjf; /** * Represents a node in a linked list. */...
Complete the following program:LinkedQueue.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)...
C++ program Complete the following functions for linked list. You are not allowed to alter the...
C++ program Complete the following functions for linked list. You are not allowed to alter the names or the function prototypes. #ifndef _ULL #define _ULL #include <iostream> #include "nodeType.h" using namespace std; void initializeList(nodeType *&head, nodeType *&tail, int&count); //Initialize the list to an empty state. //Postcondition: head = NULL, tail = NULL, count = 0; bool isEmptyList(const nodeType *head) ; //Function to determine whether the list is empty. //Postcondition: Returns true if the list is empty, // otherwise it returns false. void print(const...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value x if...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and not the last node on the list. What is the effect of the following code fragment? x.next = x.next.next b. Singly Linked List has two private instance variables first and last as that point to the first and the last nodes in the list, respectively. Write a fragment of code that removes the last node in a linked list whose first node is first....
Linked List: Complete the following code to create a linked list from an Array. After creating...
Linked List: Complete the following code to create a linked list from an Array. After creating the list, display the elements of the linked list iteratively. Write two others function called as RDisplayTailRecursion(first) and RDisplayTailRecursion(first) which will print elements of the linked list using the tail and head recursions respectively. #include <stdio.h> #include <stdlib.h> struct Node { }*first=NULL; void create(int A[], int n) { for(i=1; i<n; i++) { } } void Display(struct Node*p) { while(p!=NULL) { } } void RDisplayTailRecursion...
I've provided a Node class that implements a node of a simple singly-linked list (with .value...
I've provided a Node class that implements a node of a simple singly-linked list (with .value and .next fields), and an empty LinkedList class. Your task is to implement LinkedList.sort(l), where given the node l as the head of a singly-linked list, LinkedList.sort(l) sorts the nodes in the list into ascending order according to the values in the .value field of each node. Your implementation should do an in-place update of the list. It is ok to use a simple...
The following is how we define a linear linked list node: (USE IT IN QUESTIONS 1...
The following is how we define a linear linked list node: (USE IT IN QUESTIONS 1 & 2)     struct node    { int item; node* next;            node(int x, node* t)          { item = x; next = t; }     };     typedef node* link; Write a C++ function void moveMaxToLast(link h) that takes as a parameter a link to the head of a linked list L. After this function performs, L still contains the same item values with...
The following is how we define a linear linked list node: (USE IT IN QUESTIONS 1...
The following is how we define a linear linked list node: (USE IT IN QUESTIONS 1 & 2) struct node { int item; node* next; node(int x, node* t) { item = x; next = t; } }; typedef node* link; Write a C++ function void moveMaxToLast(link h) that takes as a parameter a link to the head of a linked list L. After this function performs, L still contains the same item values with the following change in order...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int size; public CircularLinkedList() { tail= null; size = 0; } public int size(){ return size; } public boolean isEmpty() { return size==0; } //if list is not empty return the first element public E first() { if (isEmpty()) return null; //code here return 0; } //if list not empty return last element public E last() { if (isEmpty()) return null; return tail.getElement(); } /*...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT