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();    /**    *...
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++)          {...
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 =...
***JAVA LANGUAGE, NO CHANGES NEEDED FOR THE Movie CLASS. USE THIS AS A REFERENCE*** public class...
***JAVA LANGUAGE, NO CHANGES NEEDED FOR THE Movie CLASS. USE THIS AS A REFERENCE*** public class Movie { private String title; private int length; private static int numMovies = 0; public Movie() { this("", 0); } public Movie(String title, int length) { this.title = title; this.length = length; ++numMovies; } public String getTitle() { return this.title; } public int getLength() { return this.length; } public static int getNumMovies() { return numMovies; } public void setTitle(String title) { this.title = title;...
In java, write a class that tests the following code. Add screen shots as well. public...
In java, write a class that tests the following code. Add screen shots as well. public class DoubleStack<E> {    private E[] elements;    private int top1, top2;    private static int DEFAULT_SIZE = 10;       public DoubleStack() {        elements = (E[])new Object[DEFAULT_SIZE];        top1 = 0;        top2 = elements.length - 1;    }       public boolean isFull(int stack) {        if(top2 == top1 + 1)            return true;   ...
Can you fix my code and remove the errors in java language. public class LinkedStack<T> implements...
Can you fix my code and remove the errors in java language. public class LinkedStack<T> implements Stack<T> { private Node<T> top; private int numElements = 0; public int size() { return (numElements); } public boolean isEmpty() { return (top == null); } public T top() throws StackException { if (isEmpty()) throw new StackException("Stack is empty."); return top.info; } public T pop() throws StackException { Node<T> temp; if (isEmpty()) throw new StackException("Stack underflow."); temp = top; top = top.getLink(); return temp.getInfo();...
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, Complete LinkedListSet: package Homework3; public class LinkedListSet <T> extends LinkedListCollection <T> { LinkedListSet() {...
Using Java, Complete LinkedListSet: package Homework3; public class LinkedListSet <T> extends LinkedListCollection <T> { LinkedListSet() { } public boolean add(T element) { // Code here return true; } } Homework3 class: public class Homework3 { public static void main(String[] args) { ArrayCollection ac1 = new ArrayCollection(); // Calling Default Constructor ArrayCollection ac2 = new ArrayCollection(2); // Calling overloaded constructor ArraySet as1 = new ArraySet(); ac2.add("Apple"); ac2.add("Orange"); ac2.add("Lemon"); // This can't be added into ac2 as collection is full System.out.println(ac2.remove("Apple")); //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT