Consider the implementation of an array based Queue in handout D, the peek operation allows you to
Question:
Consider the implementation of an array based Queue in handout D, the peek operation allows you to view the value in the front element of the queue before dequeueing it, sometimes it might also be valuable to see the second element as well. Write an instance method “peek2” that returns the value in the second element in the queue, without removing it
public S peek2()
Handout D: Generic Array-based Queue Specification
The following represents the set of methods for a queue class.
Public class Queue implements QueueInterface{
private S[] elements;
private int currentSize;
private int maxSize;
private int front;
private int rear;
public Queue(int max);
// allocates a new queue, capable of containing maxsize values,
//Preconditions: MaxSize > 0,
// throws QueueException if maxSize <=0
public boolean isFull();
// determines whether the queue is full.
// returns true if currentSize == maxSize;
public boolean isEmpty();
// determines whether the queue is empty.
// returns true if currentSize ==0
public void enqueue( S newValue);
// adds a new value to the end of the queue
// Preconditions: Queue must not be full, QueueException is thrown
// if the queue is full
// Postconditions: NewValue is inserted at the end of the queue
public S dequeue();
// retrieves the value at the front of the queue
// Precondition: The queue must not be empty.
// Postconditon: If the queue is not empty, the value at
// the front of the queue is removed and returned.
//_Errors: throws QueueException if the queue is empty
public S peek();
// returns the value at the front of the queue without removing it.
// Preconditions: The queue must not be empty.
// Postconditon: If the queue is not empty, the value at
// the front of the queue is returned.
//_Errors: throws QueueException if the queue is empty
public int size();
// returns the number of elements in the queue.
public int getMaxSize();
// returns the maximum number of elements the queue can hold.
public void clear ( int position)
//Description: deletes all elements from the Queue
public String toString()
//Description: displays the contents of the Queue as a comma delimited string
Financial Accounting and Reporting a Global Perspective
ISBN: 978-1408076866
4th edition
Authors: Michel Lebas, Herve Stolowy, Yuan Ding