Question

In: Computer Science

(Implement GenericQueue using inheritance) In section 24.5 Stacks and Queues.GenericQueue is implemented using compositions.Define a new...

(Implement GenericQueue using inheritance) In section 24.5 Stacks and Queues.GenericQueue is implemented using compositions.Define a new queue class that extends java.util.linkedlist

Solutions

Expert Solution

SOLUTION-
I have solve the problem in Java code with comments for easy understanding :)

CODE-

public class GenericQueue<E> extends LinkedList<E> {

public GenericQueue() {

}

public GenericQueue(LinkedList<E> list) {

super(list);

}

public void enqueue(E e) {

addLast(e);

}

public E dequeue() {

return removeFirst();

}

public static void main(String[] args) {

LinkedList<String> li = new LinkedList<String>();

li.add("Tom");

li.add("Vincent");

li.add("John");

// need to pass the object li to the constructor

GenericQueue<String> q = new GenericQueue<String>(li);

q.enqueue("baba");

q.enqueue("Ali");

q.enqueue("john");

System.out.println("First element : "+q.removeFirst());

System.out.print("Remaining elementd : ");

/*

* need use the while-loop with isEmpty method instead of for-loop with

* size() method

*/

while (!q.isEmpty()) {

System.out.print(q.dequeue() + " ");

}

}

}

Output

First element : Tom
Remaining elementd : Vincent John baba Ali john


IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

(Implement GenericQueue using inheritance) In Section 24.5, Stacks and Queues, GenericQueue is implemented using composition. Define...
(Implement GenericQueue using inheritance) In Section 24.5, Stacks and Queues, GenericQueue is implemented using composition. Define a new queue class that extends java.util.LinkedList..
Describe how stacks and their main operations can be implemented using arrays and stacks. Provide the...
Describe how stacks and their main operations can be implemented using arrays and stacks. Provide the pros and cons for each approach. Research some applications, that can use stacks as a possible implementation vehicle.
Write a C++ class that implement two stacks using a single C++ array. That is, it...
Write a C++ class that implement two stacks using a single C++ array. That is, it should have functions pop_first(), pop_second(), push_first(…), push_second(…), size_first(), size_second(), …. When out of space, double the size of the array (similarly to what vector is doing). Notes: Complete all the functions in exercise_2.cpp, then submit this cpp file. pop_first() and pop_second() should throw std::out_of_range exception when stack is empty. CODE: #include <cstdio> #include <stdexcept> template <class T> class TwoStacks { public:   // Constructor, initialize...
We are trying to use two stacks to implement a queue. Name the two stacks as...
We are trying to use two stacks to implement a queue. Name the two stacks as E and D. We will enqueue into E and dequeue from D. To implement enqueue(e), simply call E.push(e). To implement dequeue(), simply call D.pop(), provided that D is not empty. If D is empty, iteratively pop every element from E and push it onto D, until E is empty, and then call D.pop(). Considering the worst case running time, what is the performance in...
Write the code to implement the concept of inheritance forVehicles. You are required to implement inheritance...
Write the code to implement the concept of inheritance forVehicles. You are required to implement inheritance betweenclasses. You have to write four classes in C++ i.e. one superclass, two sub classes and one driver class. Vehicle is the super class whereas Bus and Truck are sub classesof Vehicle class. Transport is a driver class which contains mainmethod. Detailed description of Vehicle (Super class): The class Vehicle must have following attributes: 1. Vehicle model 2. Registration number 3. Vehicle speed (km/hour)...
This is for a java program. Payroll System Using Inheritance and Polymorphism 1. Implement an interface...
This is for a java program. Payroll System Using Inheritance and Polymorphism 1. Implement an interface called EmployeeInfo with the following constant variables: FACULTY_MONTHLY_SALARY = 6000.00 STAFF_MONTHLY_HOURS_WORKED = 160 2. Implement an abstract class Employee with the following requirements: Attributes last name (String) first name (String) ID number (String) Sex - M or F Birth date - Use the Calendar Java class to create a date object Default argument constructor and argument constructors. Public methods toString - returning a string...
Show how to implement three stacks in one array (in Java)
Show how to implement three stacks in one array (in Java)
1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement...
1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement the Stack.java in a class called ArrayStack.java that uses Generics. Write a main function that creates two stacks, one containing 10 Integers and a different one containing 10 Strings, and reverses there contents using a method called reverse that takes as a paramater a Generic array. You will need to implement all the methods of Stack.java as well as create a toString method in...
Implement a class named stack pair that provides a pair of stacks. Make the class a...
Implement a class named stack pair that provides a pair of stacks. Make the class a template class. So, you will have two files: stack pair.h and stack pair.template, following the style of the text. The basic idea is that two stacks can share a single static array. This may be advantageous if only one of the stacks will be in heavy use at any one time. • The class should have various methods to manipulate the stack: T pop...
(Implement a doubly linked list) The MyLinkedList class used in Listing 24.5 is a one-way directional...
(Implement a doubly linked list) The MyLinkedList class used in Listing 24.5 is a one-way directional linked list that enables one-way traversal of the list. Modify the Node class to add the new data field name previous to refer to the previous node in the list, as follows : public class Node { E element; Node next; Node previous; public Node(E e) { element = e; } } Implement a new class named TwoWayLinkedList that uses a doubly linked list...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT