Question

In: Computer Science

Create a Deque class based on the following description of deque (double-ended queues). A dequeue is...

Create a Deque class based on the following description of deque (double-ended queues). A dequeue is a double-ended queue. You can insert items at either end and delete them from either end. Therefore, the deque offers two insert operations and two remove operations: insertLeft() and insertRight() removeLeft() and removeRight(). Deques may also have "peek" methods ( peekLeft() and peekRight() )that let you see the left or right item in the deque without remove the item from the deque. For this problem, your Deque class should contain the following methods: insertLeft(), insertRight(), removeLeft(), removeRight(), peekRight(), peekLeft(), isEmpty(), and isFull(). It will need to support wraparound at the end of the array, as the Queue class in the provided code does. You are asked to use an array to define the Deque class. Write a driver class to test class Deque.

Solutions

Expert Solution

Following is the java Program:

Dequeue.java

import java.util.NoSuchElementException;
import java.util.Random;

public class Deque {

    private final int arr[];
    private int head;
    private int size;

    public Deque(int capacity) {
        arr = new int[capacity];
    }

    public void insertLast(int element) {
        checkCapacity();
        arr[(head + size++) % arr.length] = element;
    }

    public void insertFirst(int element) {
        checkCapacity();

        if (head == 0) {
            arr[(head = arr.length - 1)] = element;
        } else {
            arr[(--head) % arr.length] = element;
        }

        size++;
    }

    public int removeFirst() {
        checkNotEmpty();

        int ret = arr[head];
        head = (head + 1) % arr.length;
        size--;
        return ret;
    }

    public int removeLast() {
        checkNotEmpty();

        int ret = arr[(head + size - 1) % arr.length];
        size--;
        return ret;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("[");

        for (int i = 0; i < size; ++i) {
            sb.append(arr[(head + i) % arr.length]);

            if (i < size - 1) {
                sb.append(", ");
            }
        }

        return sb.append("]").toString();
    }

    public int size() {
        return size;
    }

    private void checkCapacity() {
        if (arr.length == size) {
            throw new IllegalStateException("No more space is available.");
        }
    }

    private void checkNotEmpty() {
        if (size == 0) {
            throw new NoSuchElementException("Trying to pop an empty deque.");
        }
    }

    public static void main(String args[]) {
        Deque deque = new Deque(10);
        Random random = new Random();

        for (int op = 0; op < 30; ++op) {
            if (deque.size() == 0) {
                int num = random.nextInt(100);
                System.out.print("Adding " + num + " to front: ");
                deque.insertLast(num);
                System.out.println(deque);
            } else {
                boolean add = random.nextBoolean();

                if (add) {
                    boolean front = random.nextBoolean();
                    int num = random.nextInt(100);

                    if (front) {
                        System.out.print("Adding " + num + " to front: ");
                        deque.insertFirst(num);
                        System.out.println(deque);
                    } else {
                        System.out.print("Adding " + num + " to back:  ");
                        deque.insertLast(num);
                        System.out.println(deque);
                    }
                } else {
                    boolean front = random.nextBoolean();

                    if (front) {
                        System.out.print("Removing from front: ");
                        deque.removeFirst();
                        System.out.println(deque);
                    } else {
                        System.out.print("Removing from back:  ");
                        deque.removeLast();
                        System.out.println(deque);
                    }
                }
            }
        }
    }
}

DequeApp.java

import java.util.Scanner;

public class Deque {
    public static void main(String[] args) {
        Deque t= new Deque(10);

        //for stack
        t.insertFirst(2);
        t.insertFirst(3);
        t.removeLast();

        //for queue
        t.insertFirst(4);
        t.insertFirst(5);
        t.removeFirst();

        //dequeue
        t.insertFirst(5);
        t.insertLast(6);
        t.removeFirst();
        t.removeLast();
    }
}

Related Solutions

Problem 1: based on java.util.LinkedList class, create MyQueue class that should have the methods:enqueue(), dequeue(), peek(),...
Problem 1: based on java.util.LinkedList class, create MyQueue class that should have the methods:enqueue(), dequeue(), peek(), size(), isEmpty(). Problem 2: Create a new Java Application that test MyQueue by having the following methods in main class: A method to generate a number of element between two given values and save them in a queue A method to print a queue (10 elements per line). The original queue should remain as is after the print A method to exchange the first...
1. A double-ended queue, or deque, is a data structure consisting of a list of items...
1. A double-ended queue, or deque, is a data structure consisting of a list of items on which the following operations are defined: addToBack(x): insert item x on the back end of the queue addToFront(x): insert item x on the front end of the queue getBack(): returns the element on the back end of the queue getFront(): returns the element on the front end of the queue removeBack(): remove the back item from the queue removeFront(): remove the front item...
A deque (short for double-ended queue, but pronounced “deck”) is an abstract data type that supports...
A deque (short for double-ended queue, but pronounced “deck”) is an abstract data type that supports adding and removing at both the front and back. So, at a bare minimum, a deque has four operations: addFront(), removeFront(), addBack(), removeBack(). Suppose you have a deque D containing the numbers (1, 2, 3, 4, 5, 6, 7, 8), in this order. Suppose further that you have an initially empty queue Q. Give pseudo-code description of a method that uses only D and...
Study and implement data structure deque (double ended queue, pronounced as "deck"). IN C++ PLEASE
Study and implement data structure deque (double ended queue, pronounced as "deck"). IN C++ PLEASE
Description of the Assignment: Write a Python program to create a deque and append few elements...
Description of the Assignment: Write a Python program to create a deque and append few elements to the left and right, then remove some elements from the left, right sides and reverse the deque. Please use the following comments in your python script: # ************************************************* # COP3375 # Student's full name ( <<< your name goes here) # Week 9: Assignment 1 # ************************************************* # Write your code below: # Create a deque # Print a deque # Append to...
In Java...create a class Realestate.java with the following fields: location, price, and description. Create a class...
In Java...create a class Realestate.java with the following fields: location, price, and description. Create a class RealestateFinder.java that reads in real estate data from a CSV file (RealestateList.csv). Then provide the user with the following options: Sort per Price, Sort per Location, and Exit. Use ArrayList and Arrays.sort to perform the sorts and display the data to the user.
Create a class called Sphere. The class will contain the following    Instance data double radius...
Create a class called Sphere. The class will contain the following    Instance data double radius Methods Constructor with one parameter which will be used to set the radius instance data Getter and Setter for radius             Area - calculate the area of the sphere (4 * PI * radius * radius)             Volume - calculate and return the volume of the sphere (4/3 * PIE * radius * radius * radius) toString - returns a string with the...
Consider the following statements: #include #include class Temporary { private: string description; double first; double second;...
Consider the following statements: #include #include class Temporary { private: string description; double first; double second; public: Temporary(string = "", double = 0.0, double = 0.0); void set(string, double, double); double manipulate(); void get(string&, double&, double&); void setDescription(string); void setFirst(double); void setSecond(double); }; Write the definition of the member function set() so that the instance variables are set according to the parameters. Write the definition of the constructor so that it initializes the instance variables using the function set() Write...
Programming Project – Deques, Stacks & Queues General Description: Design and develop array based and linked...
Programming Project – Deques, Stacks & Queues General Description: Design and develop array based and linked list-based implementations for the Dequeue ADT. Your implementation must support generic data types using C++ templates. Develop Adapter Files to provide Stack and Queue functionality for the Deques. Definitions: You should implement the ADTs precisely as described in the following partial header files. Deque.h template class Deque { public:         Deque();                    //constructor         ~Deque();                 //destructor         void insertFront(const E& e);...
Give a pseudo-code description for an array-based implementation of the deque ADT (Abstract Data Type). What...
Give a pseudo-code description for an array-based implementation of the deque ADT (Abstract Data Type). What is the running time for each operation?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT