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...
"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...
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...
Your primary task for this exercise is to complete header file by writing three functions with...
Your primary task for this exercise is to complete header file by writing three functions with its description below: removeAt function – to remove the item from the list at the position specified by location. Because the list elements are in no particular order (unsorted list), you could simple remove the element by swapping the last element of the list with the item to be removed and reducing the length of the list. insertAt function - to insert an item...
Instructions: You must complete the questions below. All will be included in a SINGLE PDF file...
Instructions: You must complete the questions below. All will be included in a SINGLE PDF file and turned into Blackboard or handed at the start of class. The due date is specified on the syllabus. Remember, late work is not accepted. You will need an Executive Summary, PowerPoint (one to page/color) and Excel spreadsheet. Be prepared to present your findings in class. Purpose: You must evaluate the various options to ship freight from Asia to the USA. You MUST find...
Write a complete Java program that does the following: Open an input file named data.txt that...
Write a complete Java program that does the following: Open an input file named data.txt that consists of a series of unknown number of integers. If data.txt does not exist, give an appropriate error message and terminate the program. Define a constant MAX of value 100 and create an array of size MAX to hold items from the input file. Make sure your program will not generate ArrayIndexOutOfBounds exception. Open an output file named result.txt and write the array elements...
IN C++, ONE FILE Also include the main class that just runs these functions, thanks. Write...
IN C++, ONE FILE Also include the main class that just runs these functions, thanks. Write a recursive function to produce a pattern of n lines of asterisks. The first line contains one asterisk, the next line contains two, and so on, up to the nth line, which contains n asterisks. For example, if the non-negative integer is 5, the pattern generated is: * ** *** **** ***** Prototype: void pattern(unsigned n); Write a recursive function, sum_range that finds the...
For the given functions f and​ g, complete parts​ (a)-(h). For parts​ (a)-(d), also find the...
For the given functions f and​ g, complete parts​ (a)-(h). For parts​ (a)-(d), also find the domain. f left parenthesis x right parenthesis equals StartFraction 7 x plus 9 Over 9 x minus 7 EndFractionf(x)=7x+99x−7​; g left parenthesis x right parenthesis equals StartFraction 9 x Over 9 x minus 7 EndFractiong(x)=9x9x−7​(a) Find ​(fplus+​g)(x). ​(fplus+​g)(x)equals=nothing ​(Simplify your​ answer.)What is the domain of fplus+​g? Select the correct choice below​ and, if​ necessary, fill in the answer box to complete your choice. A.The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT