Question

In: Computer Science

Language: Java Topic: Deques Using the following variables/class: public class LinkedDeque<T> { // Do not add...

Language: Java

Topic: Deques

Using the following variables/class:

public class LinkedDeque<T> {

// Do not add new instance variables or modify existing ones.
private LinkedNode<T> head;
private LinkedNode<T> tail;
private int size;

Q1: Write a method called "public void addFirst(T data)" that does the following:

* Adds the element to the front of the deque.

* Must be O(1)

* @param data the data to add to the front of the deque

* @throws java.lang.IllegalArgumentException if data is null

Q2: Write a method called "public void addLast(T data)" that does the following:

* Adds the element to the back of the deque.

* Must be O(1)

* @param data the data to add to the back of the deque

* @throws java.lang.IllegalArgumentException if data is null

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Assumptions: This is based on singly linked list and LinkedNode contains a data field and next field, a constructor that takes value for data field and initializes next to null.

//method to add an element to the front

public void addFirst(T data) {

      // throwing exception if data is null

      if (data == null) {

            throw new IllegalArgumentException();

      }

      // creating a new node, assuming there exists a constructor for

      // LinkedNode that takes a value of type T

      LinkedNode<T> newNode = new LinkedNode<T>(data);

      // adding as both head and tail node if dequeue is currently empty

      if (size == 0) {

            head = newNode;

            tail = newNode;

      } else {

            // setting head as next of newNode, assuming there exists a field

            // called next in LinkedNode class which is directly accessible

            newNode.next = head;

            // updating head node

            head = newNode;

      }

      // updating size

      size++;

}

// method to add an element to the tail

public void addLast(T data) {

      // throwing exception if data is null

      if (data == null) {

            throw new IllegalArgumentException();

      }

      LinkedNode<T> newNode = new LinkedNode<T>(data);

      // adding as both head and tail node if dequeue is currently empty

      if (size == 0) {

            head = newNode;

            tail = newNode;

      } else {

            // appending to tail node and updating tail node

            tail.next = newNode;

            tail = newNode;

      }

      // updating size

      size++;

}


Related Solutions

Language: Java Topic: Deques Using the following variables/class: public class ArrayDeque<T> { /** * The initial...
Language: Java Topic: Deques Using the following variables/class: public class ArrayDeque<T> { /** * The initial capacity of the ArrayDeque. * * DO NOT MODIFY THIS VARIABLE. */ public static final int INITIAL_CAPACITY = 11; // Do not add new instance variables or modify existing ones. private T[] backingArray; private int front; private int size; Q1: write a method called "public void addLast(T data)" which adds an element to the back of a Deque. If sufficient space is not available...
Using Java The given class SetInterface.java is : public interface SetInterface<T> {    /**    *...
Using Java The given class SetInterface.java is : public interface SetInterface<T> {    /**    * Gets the current number of entries in this set.    *    * @return The integer number of entries currently in the set.    */    public int getSize();    /**    * Sees whether this set is empty.    *    * @return True if the set is empty, or false if not.    */    public boolean isEmpty();    /**    *...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class,...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. -Add a method addToCollection. In this method, add one to collected...
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with...
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with the higher value data. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head; while (tempPtr != null) { tempPtr = tempPtr.nextNode; result =...
Using this BubbleSort implementation in Java: public class BubbleSort<T extends Comparable<T>> {    private static <T...
Using this BubbleSort implementation in Java: public class BubbleSort<T extends Comparable<T>> {    private static <T extends Comparable<T>>    void swap(T[] data, int index1, int index2)    {       T temp = data[index1];       data[index1] = data[index2];       data[index2] = temp;    }    public void sort(T[] data)    {       int position, scan;       for (position = data.length - 1; position >= 0; position--)       {          for (scan = 0; scan <= position - 1; scan++)          {...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static void main(String[] args) { String bottle1_label="Milk"; float bottle1_volume=250; float bottle1_capacity=500; bottle1_volume=addVolume(bottle1_label, bottle1_volume,bottle1_capacity,200); System.out.println("bottle label: " + bottle1_label + ", volume: " + bottle1_volume + ", capacity: " +bottle1_capacity); String bottle2_label="Water"; float bottle2_volume=100; float bottle2_capacity=250; bottle2_volume=addVolume(bottle2_label, bottle2_volume,bottle2_capacity,500); System.out.println("bottle label: " + bottle2_label + ", volume: " + bottle2_volume + ", capacity: " +bottle2_capacity); } public static float addVolume(String label, float bottleVolume, float capacity, float addVolume)...
//Using Java language Write a copy instructor for the following class. Be as efficient as possible....
//Using Java language Write a copy instructor for the following class. Be as efficient as possible. import java.util.Random; class Saw {    private int x;    private Integer p; //------------------------------------------ public Saw() { Random r = new Random();        x = r.nextInt();        p = new Integer(r.nextInt()); } }
Add a generic Node class to the Java project. In the class Declare the fields using...
Add a generic Node class to the Java project. In the class Declare the fields using the generic type parameter, follow the book specification Define the accessor method getLink( ) Define the constructor, follow the book implementation Note: at the definition the name of the constructor is Node, however every time you use it as a type must postfix the generic type like Node<T> Define the addNodeAfter( ) method as explained in the PP presentation, use the generic type as...
IN JAVA: Repeat Exercise 28, but add the methods to the LinkedStack class. Add the following...
IN JAVA: Repeat Exercise 28, but add the methods to the LinkedStack class. Add the following methods to the LinkedStacked class, and create a test driver for each to show that they work correctly. In order to practice your array related coding skills, code each of these methods by accessing the internal variables of the LinkedStacked, not by calling the previously defined public methods of the class. - String toString()—creates and returns a string that correctly represents the current stack....
Using the following in Java- package intersectionprinter; import java.awt.Rectangle; public class IntersectionPrinter { public static void...
Using the following in Java- package intersectionprinter; import java.awt.Rectangle; public class IntersectionPrinter { public static void main(String[] args) { Rectangle r1 = new Rectangle(0,0,100,150); System.out.println(r1);    Rectangle r2 = new Rectangle(50,75,100,150); System.out.println(r2);    Rectangle r3 = r1.intersection(r2);    } } Write a program that takes both Rectangle objects, and uses the intersection method to determine if they overlap. If they do overlap, then print it's coordinates along with its width and height. If there is no intersection, then have the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT