Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Complete the implementation of the static method int remove(Queue q, E e, int n), which removes the first n occurrences of e in q. The
Complete the implementation of the static method int remove(Queue q, E e, int n), which removes the first n occurrences of e in q. The method must work for any implementation of the interface Queue: public interface Queue {boolean isEmpty(); void enqueue(E e); E dequeue ();} Following a call to the method remove, the elements in the queue must remain in the same order, except that the first n occurrences of e have been removed. The method throws NullPointerException if either q or e are null. It throws IllegalArgumentException if n is a negative number. If the queue had less than n occurrences of e, the returned value represents the excess of elements that could not be removed. Consider the example below. Since you do not know the size of the queue, you cannot use an array for temporary storage. Instead, you must either use a queue or a stack (or both). You can assume the existence of the class LinkedQueue, which implements the interface Queue, as well as LinkedStack. which implements the interface Stack. public interface Stack {boolean isEmpty(); E peek (); E pop(); void push(E e); Executing the test program below produces the following output: LinkedQueue: {A, B, R, A, C, A, D, A, B, R, A) 0 LinkedQueue: {B, R, C, D, A, B, R, A} 2 LinkedQueue: {B, R, D, A, B, R, A} Queue q; q = new LinkedQueue(); q.enqueue("A"); q.enqueue("B"); q.enqueue("R"); q.enqueue("A"); q.enqueue("C"); q.enqueue("A"); q.enqueue("D"); q.enqueue("A"); q.enqueue("B"); q.enqueue("R"); q.enqueue("A"); System .out. println(q); System. out. println (remove (q, "A", 3)); System .out. println(q); System. out. println (remove(q, "C", 3)); System .out. println(q)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started