In: Computer Science
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 element and the last element in a queue
A Boolean method to search for a value in a queue. The queue should remain the same after the search is
finished.
A method to check if a queue is included in another queue (q1 is included in q2 if q1 is a sub-queue of q2). q1
and q2 should remain as they were originally.
A method to inverse a queue.
A method to make a copy of a queue into a queue (the original queue should remain as it is).
The main method that does
Create 2 queues Q1 and Q2
Insert 23 integers between 20 and 60 (do not insert duplicates) into Q1
Insert 35 integers between 10 and 80 (do not insert duplicates) into Q2.
Give the user the choice which methods (2-7) to call and option to exit
A queue is a FIFO( First In First Out) data structure.
Java provides a Queue Interface.
OUTPUT:
Here is the required java code.
import java.util.*;
class QueueDemo
{
// Queue implementation in java
public static void main(String[] args)
{
Queue<String> queue = new
LinkedList<String>();
queue.add("A"); //
Insert "A" in the queue
queue.add("B"); //
Insert "B" in the queue
queue.add("C"); //
Insert "C" in the queue
queue.add("D"); //
Insert "D" in the queue
// Prints the front of the queue
("A")
System.out.println("Front element
is: " + queue.peek());
queue.remove(); //
removing the front element ("A")
queue.remove(); //
removing the front element ("B")
// Prints the front of the queue
("C")
System.out.println("Front element
is: " + queue.peek());
// Returns the number of
elements present in the queue
System.out.println("Queue size is "
+ queue.size());
// check if queue is empty
if (queue.isEmpty())
System.out.println("Queue is Empty");
else
System.out.println("Queue is not Empty");
}
}
I hope it was helpful.