Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a java program using the information and java codes given below.(Please don't just copy-paste wrong solutions from online or other chegg expert answers as

Write a java program using the information and java codes given below.(Please don't just copy-paste wrong solutions from online or other chegg expert answers as I need you to provide me with a program following the steps exactly as mentioned in the question and your output snapshots as well)

A dequeue is a double-ended queue. It enables the adding and removing of data elements from an underlying data structure from either front (head) or rear (tail). Thus it can be used either as a FIFO or a LIFO data structure.

image text in transcribed

(a) [50 points] Using an instance of doubly linked list as the underlying data structure, implement a double-ended queue data structure called MyDequeue that implements Dequeue interface:

public interface Dequeue{

public void addToFront(T e);

public void addToRear(T e);

public T removeFront() throws UnsupportedOperationException;

public T removeRear() throws UnsupportedOperationException;

public T peekFront() throws UnsupportedOperationException;

public T peekRear() throws UnsupportedOperationException;

public boolean isEmpty();

public int getSize();

public boolean search(T e);

public String toString() throws UnsupportedOperationException;

}

Note: The java.lang. UnsupportedOperationException is thrown if the dequeue instance is empty.

An instance of MyDequeue has the following behaviour: values can be inserted and removed from the front and the rear of the dequeue, and the contents of the queue can be printed from front-to-rear. You must implement the following methods or constructors:

public MyDeQueue()

Constructor: It creates an empty MyDequeue instance.

public void addToFront (T e)

Adds a new node containing e as data to the front of this dequeue.

public void addToRear (T e)

Adds a new node containing e as data to the end of this dequeue.

public T removeFront()

Removes the front node of this dequeue and returns it's data.

public T removeRear()

Removes the rear node of this queue and returns it's data.

public T peekFront()

Returns the front node's data without deleting the node.

public T peekRear()

Returns the rear node's data without deleting the node.

public int getSize()

Returns the number of elements in this dequeue.

public boolean search(T e)

Returns true if element e is present in the dequeue; otherwise it returns false.

public String toString()

Constructs a string representation of the dequeue, from front to rear, and returns it.

(b) [20 points] Write a menu driven console program to test MyDequeue. (The output should be according to the snapshots shown below. Also use the java files given below while programming.)

///DequeDriver.java////

import java.util.Scanner; public class DequeueDriver{ public static void main(String[] args){ //to be implemented by students, you may use the following getMenuChoice method } public static int getMenuChoice(){ Scanner scanner = new Scanner(System.in); int choice; do{ System.out.println("Please select the operation: "); System.out.println("1. Add to front"); System.out.println("2. Add to rear"); System.out.println("3. Peek front"); System.out.println("4. Peek rear"); System.out.println("5. Remove front"); System.out.println("6. Remove rear"); System.out.println("7. Search"); System.out.println("8. Get size"); System.out.println("9. Display dequeue"); System.out.println("10. Exit"); choice = scanner.nextInt(); if(choice 10) System.out.println("Error: Wrong operation!"); }while(choice 10); return choice; } }

///DLLNode////

public class DLLNode { public T info; public DLLNode next, prev; public DLLNode() { next = null; prev = null; } public DLLNode(T el) { info = el; next = null; prev = null; } public DLLNode(T el, DLLNode n, DLLNode p) { info = el; next = n; prev = p; } } ///MyDeque.java///

public class MyDequeue implements Dequeue { //to be implemented by students }

///Deque.java///

public interface Dequeue{ public void addToFront(T element); public void addToRear(T element); public T removeFront() throws UnsupportedOperationException; public T removeRear() throws UnsupportedOperationException; public abstract T peekFront() throws UnsupportedOperationException; public abstract T peekRear() throws UnsupportedOperationException; public boolean isEmpty(); public int getSize(); public boolean search(T element); public String toString() throws UnsupportedOperationException; }

Snapshots of Output:

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Deletion Deletion 30 40 50 60 70 80 10 20 Insertion 1 Insertion Front Rear ----GRASP exec: java DequeueDriver Please select the operation: 1. Add to front 2. Add to rear 3. Peek front 4. Peek rear 5. Remove front 6. Remove rear 7. Search 8. Get size 9. Display dequeue 10. Exit java.lang. UnsupportedOperationException: Dequeue is empty! Please select the operation: 1. Add to front 2. Add to rear 3. Peek front 4. Peek rear 5. Remove front 6. Remove rear 7. Search 8. Get size 9. Display dequeue 10. Exit 6. Remove rear 7. Search 8. Get size 9. Display dequeue 10. Exit 9 java.lang. UnsupportedOperationException: Dequeue is empty! Please select the operation: 1. Add to front 2. Add to rear 3. Peek front 4. Peek rear 5. Remove front 6. Remove rear 7. Search 8. Get size 9. Display dequeue 10. Exit 3 java.lang. UnsupportedOperationException: Dequeue is empty! Please select the operation: 1. Add to front 2. Add to rear 3. Peek front 4. Peek rear 5. Remove front 6. Remove rear 7. Search 8. Get size 9. Display dequeue 10. Exit > 6. Remove rear 7. Search 8. Get size 9. Display dequeue 10. Exit 3 java.lang. Unsupported OperationException: Dequeue is empty! Please select the operation: 1. Add to front 2. Add to rear 3. Peek front 4. Peek rear 5. Remove front 6. Remove rear 7. Search 8. Get size 9. Display dequeue 10. Exit 56 Error: Wrong operation! Please select the operation: 1. Add to front 2. Add to rear 3. Peek front 4. Peek rear 5. Remove front 6. Remove rear 7. Search 8. Get size 9. Display dequeue 10. Exit > 9. Display dequeue 10. Exit 56 Error: Wrong operation! Please select the operation: 1. Add to front 2. Add to rear 3. Peek front 4. Peek rear 5. Remove front 6. Remove rear 7. Search 8. Get size 9. Display dequeue 10. Exit 1 Enter number of elements to add: 4 Enter 4 elements: 20 40 15 35 Please select the operation: 1. Add to front 2. Add to rear 3. Peek front 4. Peek rear 5. Remove front 6. Remove rear 7. Search 8. Get size 9. Display dequeue 10. Exit 9. Display dequeue 10. Exit 2 Enter number of elements to add: 2 Enter 2 elements: 60 17 Please select the operation: 1. Add to front 2. Add to rear 3. Peek front 4. Peek rear 5. Remove front 6. Remove rear 7. Search 8. Get size 9. Display dequeue 10. Exit 9 35 15 40 20 60 17 Please select the operation: 1. Add to front 2. Add to rear 3. Peek front 4. Peek rear 5. Remove front 6. Remove rear 7. Search 8. Get size 9. Display dequeue 10. Exit 8. Get size 9. Display dequeue 10. Exit 9 15 40 20 60 Please select the operation: 1. Add to front 2. Add to rear 3. Peek front 4. Peek rear 5. Remove front 6. Remove rear 7. Search 8. Get size 9. Display dequeue 10. Exit 7 Enter the element to search: 40 Element is in dequeue. Please select the operation: 1. Add to front 2. Add to rear 3. Peek front 4. Peek rear 5. Remove front 6. Remove rear 7. Search 8. Get size 9. Display dequeue 10. Exit 8. Get size 9. Display dequeue 10. Exit 7 Enter the element to search: 40 Element is in dequeue. Please select the operation: 1. Add to front 2. Add to rear 3. Peek front 4. Peek rear 5. Remove front 6. Remove rear 7. Search 8. Get size 9. Display dequeue 10. Exit 8 The dequeue size is 4 Please select the operation: 1. Add to front 2. Add to rear 3. Peek front 4. Peek rear 5. Remove front 6. Remove rear 7. Search 8. Get size 9. Display dequeue 10. Exit Enter the element to search: 40 Element is in dequeue. Please select the operation: 1. Add to front 2. Add to rear 3. Peek front 4. Peek rear 5. Remove front 6. Remove rear 7. Search 8. Get size 9. Display dequeue 10. Exit 8 The dequeue size is 4 Please select the operation: 1. Add to front 2. Add to rear 3. Peek front 4. Peek rear 5. Remove front 6. Remove rear 7. Search 8. Get size 9. Display dequeue 10. Exit 10 Terminating ... ----GRASP: operation complete

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Identify four applications of HRM to healthcare organizations.

Answered: 1 week ago