Concept explainers
Explanation of Solution
Implementation of PrioirtyQueue using Comparator:
//Import the required java package
import java.util.Comparator;
//Define the class Ex24_06
public class Ex24_06 {
//Define the main function
public static void main(String[] args) {
new Ex24_06();
}
//Declare the constructor
public Ex24_06() {
//Create the object for MyPriorityQueue
MyPriorityQueue<String> queue = new
MyPriorityQueue<String>((s1, s2) ->
s1.compareToIgnoreCase(s2));
//Add the elements into queue
queue.enqueue("Macon");
queue.enqueue("Atlanta");
queue.enqueue("Savannah");
queue.enqueue("Augusta");
queue.enqueue("Columbus");
//Check whether the queue size is greater than 0
while (queue.getSize() > 0) {
//Remove the elements from queue
System.out.print(queue.dequeue() + " ");
}
//Create the object for queue1
MyPriorityQueue<String> queue1 = new
MyPriorityQueue<String>((s1, s2) -> s1.length() –
s2.length());
//Add the elements into queue
queue1.enqueue("ABC");
queue1.enqueue("A");
queue1.enqueue("AB");
queue1.enqueue("ABCDE");
queue1.enqueue("ABCD");
System.out.println();
//Check whether the queue1 size is greater than 0
while (queue1.getSize() > 0) {
//Remove the elements from queue
System...
Want to see the full answer?
Check out a sample textbook solutionChapter 24 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
- Write an OOP complete program in class Overloading constructor, destructor and pointers by using friend function to print the values of x,y,z,n and m ? Note: x=3, y=6, z=9, n=7, m3D25. Using friend class, array of object & for loop.arrow_forwardPlease answer the following question in Python code: Inheritance (based on 8.38) You MUST use inheritance for this problem. A stack is a sequence container type that, like a queue, supports very restrictive access methods: all insertions and removals are from one end of the stack, typically referred to as the top of the stack. A stack is often referred to as a last-in first-out (LIFO) container because the last item inserted is the first removed. Implement a Stack class using Note that this means you may be able to inherit some of the methods below. Which ones? (Try not writing those and see if it works!) Constructor/_init__ - Can construct either an empty stack, or initialized with a list of items, the first item is at the bottom, the last is at the top. push() – take an item as input and push it on the top of the stack pop() – remove and return the item at the top of the stack isEmpty() – returns True if the stack is empty, False otherwise [] – return the item at a given…arrow_forwardJAVA CODE Learning Objectives: Detailed understanding of the linked list and its implementation. Practice with inorder sorting. Practice with use of Java exceptions. Practice use of generics. You have been provided with java code for SomeList<T> class. This code is for a general linked list implementation where the elements are not ordered. For this assignment you will modify the code provided to create a SortedList<T> class that will maintain elements in a linked list in ascending order and allow the removal of objects from both the front and back. You will be required to add methods for inserting an object in order (InsertInorder) and removing an object from the front or back. You will write a test program, ListTest, that inserts 25 random integers, between 0 and 100, into the linked list resulting in an in-order list. Your code to remove an object must include the exception NoSuchElementException. Demonstrate your code by displaying the ordered linked list and…arrow_forward
- Instructions-Java Assignment is to define a class named Address. The Address class will have three private instance variables: an int named street_number a String named street_name and a String named state. Write three constructors for the Address class: an empty constructor (no input parameters) that initializes the three instance variables with default values of your choice, a constructor that takes the street values as input but defaults the state to "Arizona", and a constructor that takes all three pieces of information as input Next create a driver class named Main.java. Put public static void main here and test out your class by creating three instances of Address, one using each of the constructors. You can choose the particular address values that are used. I recommend you make them up and do not use actual addresses. Run your code to make sure it works. Next add the following public methods to the Address class and test them from main as you go: Write getters and…arrow_forward(Java) In this task, a skip list data structure should be implemented. You can follow the followinginstructions:- Implement the class NodeSkipList with two components, namely key node and arrayof successors. You can also add a constructor, but you do not need to add anymethod.- In the class SkipList implement a constructor SkipList(long maxNodes). The parameter maxNodes determines the maximal number of nodes that can be added to askip list. Using this parameter we can determine the maximal height of a node, thatis, the maximal length of the array of successors. As the maximal height of a nodeit is usually taken the logarithm of the parameter. Further, it is useful to constructboth sentinels of maximal height.- In the class SkipList it is useful to write a method which simulates coin flip andreturns the number of all tosses until the first head comes up. This number representsthe height of a node to be inserted.- In the class SkipList implement the following methods:(i) insert, which…arrow_forwardA) Write a generic Java queue class (a plain queue, not a priority queue). Then, call it GenericQueue, because the JDK already has an interface called Queue. This class must be able to create a queue of objects of any reference type. Consider the GenericStack class shown below for some hints. Like the Stack class below, the GenericQueue should use an underlying ArrayList<E>. Write these methods and any others you find useful: enqueue() adds an E to the queue peek() returns a reference to the object that has been in the queue the longest, without removing it from the queue dequeue() returns the E that has been in the queue the longest, and removes it from the queue contains(T t) returns true if the queue contains at least one object that is equal to t *in the sense that calling .equals() on the object with t the parameter returns true.* Otherwise contains returns false. size() and isEmpty() are obvious.arrow_forward
- Why do we use generic methods? Generic methods allows for code reuse (for different data types) Generic methods allows for the Object class (the generic data type) to be used Generic methods allow for handling abstract class (of which there can be no instantiations) The compiler will automatically generate test cases based on the appropriate data typesarrow_forwardC# (Generic Method IsEqualTo) Write a simple generic version of method IsEqualTo that compares its two arguments with the Equals method, and returns true if they’re equal and false otherwise. Use this generic method in a program that calls IsEqualTo with a variety of simple types, such as object or int. What result do you get when you attempt to run this program?arrow_forwardC++ A queue is essentially a waiting list. It’s a sequence of elements with a front and a back. Elements can only be added to the back of the queue and they can only be removed from the front of the queue. Elements are kept in order so that the first element to enter the queue is the first one to leave it.arrow_forward
- Using java, answer the following questions about the code below. 1) Create a new function or method that checks to see if an int parameter is present in the list. 2) Create a main() method or JUnit tests to show that your list code functions as intended. (Code) private class Nodeint data;Node next;Node(int data) {this.data = data;this.next = null;}}private Node first;private Node last; public void addToEnd(int data){ Node newNode = new Node(data);if (first == null){first = newNode;last = newNode;} else {last.next = newNode;last = newNode;}}public void printList(){ Node current = first;while (current != null){System.out.print(current.data + " ");current = current.next;}System.out.println();}public static void main(String[] args){node list = new node();list.addToEnd(2);list.addToEnd(4);list.addToEnd(6);list.printList();}}arrow_forwardPython Program: Auction Housearrow_forwardProvide me complete and correct solution thanks 3arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education