In: Computer Science
(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
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!!!!!!!!----------