Question

Implement the EasyStack interface with the MyStack class. You can use either a linked list or...

Implement the EasyStack interface with the MyStack class. You can use either a linked list or a dynamic array to implement the data structure.

A stack is a specialised form of list in which you can only get and remove the element most recently added to the stack.

The class should be able to work with the following code:

EasyStack stack = new MyStack();

NB: You cannot import anything from the standard library for this task. The data structure must be of your own creation, not a modified form of a pre-existing class.

public interface EasyStack {
  
   /**
   * Returns true if the stack is empty, else false.
   *
   * @return   whether the stack is empty
   */
   public boolean empty();
  
   /**
   * Returns the most recently added array, null if empty.
   *
   * @return   the most recently added array
   */
   public int[] peek();
  
   /**
   * Returns and removes the most recently added array, null if empty.
   *
   * @return   the most recently added array
   */
   public int[] pop();
  
   /**
   * Appends the array to the end of the stack. Does nothing if the array is empty or null.
   *
   * @param arr   the array to be added
   */
   public void push(int[] arr);
  
   /**
   * Finds the distance from the end of the stack the closest array with equal contents.
   * Returns -1 if the array is not present.
   *
   * @return   the distance from end
   */
   public int distance(int[] arr);
  
   /**
   * Returns the size of the stack
   *
   * @return   the size of the stack
   */
   public int size();
  
   /**
   * Returns true if the contents of the stack equal the contents of another stack, else false
   *
   * @return   whether the two stacks are equal
   */
   public boolean equals(EasyStack stack);
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public interface EasyStack {
/**
* Returns true if the stack is empty, else false.
*
* @return whether the stack is empty
*/
public boolean empty();
  
/**
* Returns the most recently added array, null if empty.
*
* @return the most recently added array
*/
public int[] peek();
  
/**
* Returns and removes the most recently added array, null if empty.
*
* @return the most recently added array
*/
public int[] pop();
  
/**
* Appends the array to the end of the stack. Does nothing if the array is empty or null.
*
* @param arr the array to be added
*/
public void push(int[] arr);
  
/**
* Finds the distance from the end of the stack the closest array with equal contents.
* Returns -1 if the array is not present.
*
* @return the distance from end
*/
public int distance(int[] arr);
  
/**
* Returns the size of the stack
*
* @return the size of the stack
*/
public int size();
  
/**
* Returns true if the contents of the stack equal the contents of another stack, else false
*
* @return whether the two stacks are equal
*/
public boolean equals(EasyStack stack);
}


class MyStack implements EasyStack
{
   private class Node
   {
       int array[];
       Node next;
       Node prev;
      
       Node(int arr[])
       {
           array = new int[arr.length];
           for(int i=0; i<array.length; i++)
           {
               array[i] = arr[i];
           }
           next = null;
           prev = null;
       }
      
       public boolean equals(Node node)
       {
           if(array.length!=node.array.length)
               return false;
           for(int i=0; i<array.length; i++)
           {
               if(array[i]!=node.array[i])
                   return false;
           }
           return true;
       }
   }
  
   private Node first;
   private int size;
  
   public MyStack()
   {
       first = null;
       size = 0;
   }
     
   /**
* Returns true if the stack is empty, else false.
*
* @return whether the stack is empty
*/
public boolean empty()
{
        return first==null;
}
  
/**
* Returns the most recently added array, null if empty.
*
* @return the most recently added array
*/
public int[] peek()
{
        if(empty()) return null;
        return first.array;
}
  
/**
* Returns and removes the most recently added array, null if empty.
*
* @return the most recently added array
*/
public int[] pop()
{
        if(empty()) return null;
        int arr[] = first.array;
        first = first.next;
        first.prev = null;
        size--;
       return arr;
}
  
/**
* Appends the array to the end of the stack. Does nothing if the array is empty or null.
*
* @param arr the array to be added
*/
public void push(int[] arr)
{
        Node newnode = new Node(arr);
        newnode.next = first;
        newnode.prev = null;
        first = newnode;
        size++;
}
  
/**
* Finds the distance from the end of the stack the closest array with equal contents.
* Returns -1 if the array is not present.
*
* @return the distance from end
*/
public int distance(int[] arr)
{
        Node node = first;
        int len = node.array.length;
        int dis = 0;
        while(node!=null)
        {
            node = node.next;
            dis++;
            if(len==node.array.length)
                break;
        }
        if(node==null)
            return -1;
        return dis;
}
  
/**
* Returns the size of the stack
*
* @return the size of the stack
*/
public int size()
{
        return size;
}
  
/**
* Returns true if the contents of the stack equal the contents of another stack, else false
*
* @return whether the two stacks are equal
*/
public boolean equals(EasyStack stack)
{
        if(stack instanceof MyStack){
       
        Node node1 = first;
        MyStack stk = (MyStack)stack;
        Node node2 = stk.first;
        while(node1!=null && node2!=null)
        {
            if(!node1.equals(node2))
                break;
            node1 = node1.next;
            node2 = node2.next;
        }
        if(node1==null && node2==null)
            return true;
        return false;
        }
        return false;
}
}

class Driver
{
   public static void main (String[] args)
   {
       EasyStack stack = new MyStack();
   }
}

Add a comment
Know the answer?
Add Answer to:
Implement the EasyStack interface with the MyStack class. You can use either a linked list or...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an ...

    Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an array. Implement all methods in ArrayStack class using resizable array strategy, i.e. usedoubleArray() Must throw StackException during exception events in methods:    peek(), pop(), ArrayStack(int initialCapacity) Do not change or add data fields Do not add new methods */ import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private static...

  • 1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class...

    1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class for integer stacks. 2. (20’) In stackTest.cpp, complete the implementation of function postfixTest(), which use an integer stack to evaluate post-fix expressions. For simplicity, you can assume the post-fix expression is input character by character (i.e., not an entire string), and each operand is a non-negative, single-digit integer (i.e., 0,1,…,9). However, you are supposed to detect invalid/ illegal post-fix expression input, e.g., “4 5...

  • Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored...

    Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an array. Implement all methods in ArrayStack class using resizable array strategy, i.e. usedoubleArray() Must throw StackException during exception events in methods:    peek(), pop(), ArrayStack(int initialCapacity) Do not change or add data fields Do not add new methods */ import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> {...

    In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> { /** Adds one element to the top of this stack. * @param element element to be pushed onto stack */ public void push (T element);    /** Removes and returns the top element from this stack. * @return T element removed from the top of the stack */ public T pop(); /** Returns without removing the top element of this stack. * @return T...

  • I need to implement a stack array but the top of the stack has to be...

    I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array.    //Array implementation of stacks.    import java.util.Arrays;       public class ArrayStack implements Stack {        //Declare a class constant called DEFAULT_STACK_SIZE with the value 10.           private static final int DEFAULT_STACK_SIZE = 10;           /* Declare two instance variables:            1. An integer called...

  • I have added a little Code but I need help with the rest. /** A class...

    I have added a little Code but I need help with the rest. /** A class of stacks whose entries are stored in a chain of nodes. Implement all methods in MyStack class Main Reference : text book or class notes Do not change or add data fields */ package PJ2; public class MyStack<T> implements StackInterface<T> {    // Data fields    private Node<T> topNode; // references the first node in the chain    private int numberOfEntries;       public...

  • Can someone implement this using singly linked list. Please and thank you public interface Priori...

    Can someone implement this using singly linked list. Please and thank you public interface PriorityQueueInterface<T extends Comparable<T>> { /** * Adds a new entry to this priority queue * @param newEntry An object to be added. */ void add(T newEntry); /** Removes and returns the entry having the highest priority. * @return Either the object having the highest priority or, if the priority * queue is empty before the operation, null. */ T remove(); /** Retrieves the entry having the...

  • You will write the following files: mystack.h - contains the class definition for the mystack class....

    You will write the following files: mystack.h - contains the class definition for the mystack class. mystack.cpp - contains the definitions for member functions of the mystack class. inpost.cpp - contains your convert() function. inpost.h - contains the function prototype for convert() so that the main() can call it. Each of the files (with the exception of inpost.h) is described in more detail below. All header files should contain header guards to prevent them from being included multiple times in...

  • Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns...

    Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns a value of primitive type int equal to the number of items on the stack. The method signature for sizeIS is public int sizeIs() a.) Write the code for sizeIs for the ArrayStack class b.) Write the code for sizeIs for the LinkedStack class (do not add any instance variables to the class; each time sizeIs is called you must "walk" through the stack...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT