Question

In: Computer Science

JAVA * This is a skeleton file. Complete the functions below. You * may also edit...

JAVA

* This is a skeleton file. Complete the functions below. You
* may also edit the function "main" to test your code.
*
* You should not use any loops or recursions. Your code needs to run in
* constant time. It is OK if your testing code has loops (like in checkInvariants).
*
* You must not add fields or static variables. As always, you must not change
* the declaration of any method nor the name of the class or of this file.
*/

public class Deque {

   private Node first;   // A reference to the first item in the Dequeue (or
                           // null if empty)
   private Node last;   // A reference to the last item in the Dequeue (or
                           // null if empty)
   private int N;            // The number of items currently in the Dequeue

   static class Node {
      
       public T item;           // The data stored at this node.
       public Node next;   // The node to the right (or null if there is no
                               // node to the right)
       public Node prev;   // The node to the lett (or null if there is no
                               // node to the left)
   }

   /**
   * Construct an empty Deque.
   */
   public Deque() {
       // TODO - Replace the line below with a correct solution.
       throw new RuntimeException("Not implemented");
   }

   /**
   * Tests if the Dequeue is empty.
   *
   * @return true if this Deque is empty and false
   * otherwise.
   */
   public boolean isEmpty() {
       // TODO - Replace the line below with a correct solution.
       throw new RuntimeException("Not implemented");
   }

   /**
   * Returns the number of items currently in this Deque.
   *
   * @return the number of items currently in this Deque
   */
   public int size() {
       // TODO - Replace the line below with a correct solution.
       throw new RuntimeException("Not implemented");
   }

   /**
   * Inserts an item into the front of this Deque.
   *
   * @param item
   * the item to be inserted
   */
   public void pushFront(T item) {
       // TODO - Replace the line below with a correct solution.
       throw new RuntimeException("Not implemented");
   }

   /**
   * Inserts an item into the back of this Deque.
   *
   * @param item
   * the item to be inserted
   */
   public void pushBack(T item) {
       // TODO - Replace the line below with a correct solution.
       throw new RuntimeException("Not implemented");
   }

   /**
   * Removes and returns the item at the front of this Deque.
   *
   * @return the item at the front of this Deque.
   * @throws NoSuchElementException if this Deque is empty.
   */
   public T popFront() {
       // TODO - Replace the line below with a correct solution.
       throw new RuntimeException("Not implemented");
   }

   /**
   * Removes and returns the item at the back of this Deque.
   *
   * @return the item at the back this Deque.
   * @throws NoSuchElementException if this Deque is empty.
   */
   public T popBack() {
       // TODO - Replace the line below with a correct solution.
       throw new RuntimeException("Not implemented");
   }
}

Solutions

Expert Solution

The completed sourcecode is given below:

package temp;

import java.util.NoSuchElementException;


public class Deque<T> {

   private Node first; // A reference to the first item in the Dequeue (or
   // null if empty)
   private Node last; // A reference to the last item in the Dequeue (or
   // null if empty)
   private int N; // The number of items currently in the Dequeue

   static class Node<T> {
  
   public T item; // The data stored at this node.
   public Node next; // The node to the right (or null if there is no
   // node to the right)
   public Node prev; // The node to the lett (or null if there is no
   // node to the left)
   }

   /**
   * Construct an empty Deque.
   */
   public Deque() {
   // TODO - Replace the line below with a correct solution.
   //throw new RuntimeException("Not implemented");
       first = null;
       last = null;
       N = 0;
   }

   /**
   * Tests if the Dequeue is empty.
   *
   * @return true if this Deque is empty and false
   * otherwise.
   */
   public boolean isEmpty() {
   // TODO - Replace the line below with a correct solution.
   //throw new RuntimeException("Not implemented");
       if(N==0) {
           return true;
       }else {
           return false;
       }
   }

   /**
   * Returns the number of items currently in this Deque.
   *
   * @return the number of items currently in this Deque
   */
   public int size() {
   // TODO - Replace the line below with a correct solution.
   //throw new RuntimeException("Not implemented");
       return N;
   }

   /**
   * Inserts an item into the front of this Deque.
   *
   * @param item
   * the item to be inserted
   */
   public void pushFront(T item) {
   // TODO - Replace the line below with a correct solution.
   //throw new RuntimeException("Not implemented");
       Node<T> n = new Node<T>();
       n.item = item;
       if(!isEmpty()) {
           first.prev=n;
           n.next = first;
           first = n;
       }else {
           first = n;
           last = n;
       }
       N++;
   }

   /**
   * Inserts an item into the back of this Deque.
   *
   * @param item
   * the item to be inserted
   */
   public void pushBack(T item) {
   // TODO - Replace the line below with a correct solution.
       Node<T> n = new Node<T>();
       n.item = item;
       if(!isEmpty()) {
           last.next = n;
           n.prev=last;
           last = n;
       }else {
           first = n;
           last = n;
       }
       N++;
   }

   /**
   * Removes and returns the item at the front of this Deque.
   *
   * @return the item at the front of this Deque.
   * @throws NoSuchElementException if this Deque is empty.
   */
   public T popFront() {
   // TODO - Replace the line below with a correct solution.
   // throw new RuntimeException("Not implemented");
       Node<T> n = null;
       if(isEmpty()) {
           throw new NoSuchElementException("Deque is empty");
       }else {
           n = first;
           first = n.next;
           N--;
       }
       return n.item;
   }

   /**
   * Removes and returns the item at the back of this Deque.
   *
   * @return the item at the back this Deque.
   * @throws NoSuchElementException if this Deque is empty.
   */
   public T popBack() {
   // TODO - Replace the line below with a correct solution.
   // throw new RuntimeException("Not implemented");
       Node<T> n = null;
       if(isEmpty()) {
           throw new NoSuchElementException("Deque is empty");
       }else {
           n = last;
           last = n.prev;
           N--;
       }
       return n.item;
   }
     
   /*
   * main() function to test code
   */
   public static void main(String[] args) {
       //Create empty deque
          Deque<Integer> deqeue = new Deque<Integer>();
           deqeue.pushFront(40);
           deqeue.pushFront(50);
           deqeue.pushBack(60);
           deqeue.pushBack(70);
          
           System.out.println("Front = "+deqeue.popFront());
           System.out.println("Back = "+deqeue.popBack());
   }
   }

You may add your own test code to test the implementation. The output of the current test is given below:


Related Solutions

JAVA CODE FILL IN THE BLANKS you are to complete the skeleton program by completing *...
JAVA CODE FILL IN THE BLANKS you are to complete the skeleton program by completing * the To Do sections. I would suggest doing this a * little bit at a time and testing each step before you * go on to the next step. * *           Add whatever code is necessary to complete this assignment *********************************************************************/ //Add whatever code is necessary to complete this assignment public class Methods {    public static void main(String[] args)    {...
Using the provided Java program below, complete the code to do the following. You may need...
Using the provided Java program below, complete the code to do the following. You may need to create other data items than what is listed for this assignment. The changes to the methods are listed in the comments above the method names. TODO comments exist. Apply any TODO tasks and remove the TODO comments when complete. Modify the method findMyCurrency() to do the following:    a. Use a do-while loop b. Prompt for a currency to find. c. Inside this...
Python Programming, Could you also show the right indents in Python Shell. Below are the skeleton...
Python Programming, Could you also show the right indents in Python Shell. Below are the skeleton of the program, and the input and output. # Initialize list mylist = [ ] # Set up loop to put menu on screen num = 1 while num != 0:      print(" ")     print(" ")     print("            Menu ")      print ("0 - Quit")     print ("1 - Add item to list")     print ("2 - Pop item off list and print...
Python Programming, Below included the Skeleton, input and output. Could you also include indents in the...
Python Programming, Below included the Skeleton, input and output. Could you also include indents in the solutions. In this programming laboratory you will create a Python program that evaluates the sum of the following mathematical series: Sum = x0 / 0! + x1 / 1! + x2 / 2! + x3 / 3! + … + xn / n!                  The variable x is a real (float) number and the variable n is an integer that can assume values greater than...
"You will need to compile each Java source file (provided below) separately, in its own file...
"You will need to compile each Java source file (provided below) separately, in its own file since they are public classes, to create a .class file for each, ideally keeping them all in the same directory." My class is asking me to do this and i am not understanding how to do it. I use JGRASP for all of my coding and i can't find a tutorial on how to do it. I just need a step by step tutorial...
''' Problem 1: Formin' Functions Define and complete the functions described below. The functions are called...
''' Problem 1: Formin' Functions Define and complete the functions described below. The functions are called in the code at the very bottom. So you should be able simply to run the script to test that your functions work as expected. ''' ''' * function name: say_hi * parameters: none * returns: N/A * operation: Uhh, well, just say "hi" when called. And by "say" I mean "print". * expected output: >>> say_hi() hi ''' ''' * function name: personal_hi...
''' Problem 1: Formin' Functions Define and complete the functions described below. The functions are called...
''' Problem 1: Formin' Functions Define and complete the functions described below. The functions are called in the code at the very bottom. So you should be able simply to run the script to test that your functions work as expected. ''' ''' * function name: say_hi * parameters: none * returns: N/A * operation: Uhh, well, just say "hi" when called. And by "say" I mean "print". * expected output: >>> say_hi() hi ''' ''' * function name: personal_hi...
''' Problem 2: Functions that give answers Define and complete the functions described below. The functions...
''' Problem 2: Functions that give answers Define and complete the functions described below. The functions are called in the code at the very bottom. So you should be able simply to run the script to test that your functions work as expected. ''' ''' * function name: get_name * parameters: none * returns: string * operation: Here, I just want you to return YOUR name. The expected output below assumes that your name is Paul. Of course, replace this...
Using the provided Java program, complete the code to do the following. You may need to...
Using the provided Java program, complete the code to do the following. You may need to create other data items than what is listed for this assignment. The changes to the methods are listed in the comments above the method names. TODO comments exist. Apply any TODO tasks and remove the TODO comments when complete. Modify the method findMyCurrency() to do the following:    a. Use a do-while loop b. Prompt for a currency to find. c. Inside this loop,...
QUESTION 19 You are also able to examine the full skeleton of this same early hominin...
QUESTION 19 You are also able to examine the full skeleton of this same early hominin and observe that it has a very narrow thumb metacarpal head. This observation suggests that: e. This hominin was capable of speech a. This hominin was capable of using tools b. This hominin was not capable of using tools d. This hominin was not bipdeal c. This hominin was bipedal 2 points    Tool-making allowed early hominins to shift from a leaf and fruit-based...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT