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 implements Queue{

private Node head, tail;

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(e);

}

else {

tail.next = new Node(e);

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;

}

}

public interface Queue{ public T serve( ); public void enqueue(T e); public

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 q, int i), member of the class ArrayQueue, which inserts all elements of q after the element at position i. Num bering starts at 0. Assume that i is a valid position and that there is enough space to insert all elements of q. The input queue q must not change. 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 q, int i), member of the class ArrayQueue, which inserts all elements of q after the element at position i. Num bering starts at 0. Assume that i is a valid position and that there is enough space to insert all elements of q. The input queue q must not change

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!