please provide complete Java class code with main() function. 1.Extend the queue
ID: 3806227 • Letter: P
Question
please provide complete Java class code with main() function.
1.Extend the queue ADT to include the following operations:
last that will return the rear element of the queue (which shall not be deleted from the queue)
length that will return the number of elements on the queue.
enqueueN that will enqueue an array of N elements on the queue
dequeueN that will dequeue and return an array of N elements from the queue
Write an implementation for each operation and compute their time complexity in Big-O notation (no justification is needed).
Explanation / Answer
import java.util.LinkedList;
public class Queue {
LinkedList<Integer> q=new LinkedList<Integer>();
public void enqueue(int n)
{
q.addLast(n);
}
public int dequeue()
{
return q.removeFirst();
}
public boolean isEmpty()
{
return q.isEmpty();
}
public int getLast()
{
return q.getLast();
}
public int length()
{
return q.size();
}
public void enqueueN(int[] arr)
{
for(int i=0;i<arr.length;i++)
{
q.addLast(arr[i]);
}
}
public int[] dequeueN(int N)
{
int[] arr=new int[N];
for(int i=0;i<N;i++)
{
arr[i]=q.removeLast();
}
return arr;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.