Question: public interface Queue { public T serve( ); public void enqueue(T e); public int length( ); public boolean full( ); } public class LinkedQueue implements
public interface Queue
public T serve( );
public void enqueue(T e);
public int length( );
public boolean full( );
}
public class LinkedQueue
private Node
private int size;
/** Creates a new instance of LinkedQueue */
public LinkedQueue() {
head = tail = null;
size = 0;
}
public boolean full() {
return false;
}
public int length (){
return size;
}
public void enqueue(T e) {
if(tail == null){
head = tail = new Node
}
else {
tail.next = new Node
tail = tail.next;
}
size++;
}
public T serve() {
T x = head.data;
head = head.next;
size--;
if(size == 0)
tail = null;
return x;
}
}
public class ArrayQueue
private int maxsize;
private int size;
private int head, tail;
private T[] nodes;
/** Creates a new instance of ArrayQueue */
public ArrayQueue(int n) {
maxsize = n;
size = 0;
head = tail = 0;
nodes = (T[])new Object[n];
}
public boolean full () {
return size == maxsize;
}
public int length () {
return size;
}
public void enqueue(T e) {
nodes[tail] = e;
tail = (tail + 1) % maxsize;
size++;
}
public T serve () {
T e = nodes[head];
head = (head + 1) % maxsize;
size--;
return e;
}
}

1. Write the method public T servelast, member of the class LinkedQueue which removes and returns the last element of the queue. Assume that the queue is not empty 2. Write the method public void remove (int i), member of ArrayQueue which re- moves the element at position i from the queue. The numbering starts at 0 Assume that i is a valid position. 3. Write the method private void insert (Queue
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
