0% found this document useful (0 votes)
4 views4 pages

Array-Based Advising Queue Implementation

Uploaded by

fsocgfa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Array-Based Advising Queue Implementation

Uploaded by

fsocgfa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

// AdvisingSchedule_Array.

java

// Array-based queue for adviser scheduling

public class AdvisingSchedule_Array implements QueueInterface<String> {

public static final int DEFAULT_CAPACITY = 10;

private final String[] data; // circular buffer

private int front; // index of current front element

private int rear; // index of last element

private int count; // number of elements in queue

public AdvisingSchedule_Array() {

this(DEFAULT_CAPACITY);

public AdvisingSchedule_Array(int capacity) {

if (capacity <= 0) throw new IllegalArgumentException("capacity must be > 0");

data = new String[capacity];

front = 0;

rear = capacity - 1;

count = 0;

// ----- core QueueInterface methods -----


@Override

public void enqueue(String element) throws QueueOverflowException {

if (isFull()) throw new QueueOverflowException("Advising queue is full");

rear = (rear + 1) % [Link];

data[rear] = element;

count++;

@Override

public String dequeue() throws QueueUnderflowException {

if (isEmpty()) throw new QueueUnderflowException("Advising queue is empty");

String x = data[front];

data[front] = null; // help GC / clarity

front = (front + 1) % [Link];

count--;

return x;

@Override

public boolean isEmpty() {

return count == 0;

@Override

public boolean isFull() {

return count == [Link];


}

// ----- handy extras the driver may use -----

/** Number of students currently in the queue. */

public int size() { return count; }

/** Capacity of the queue. */

public int getCapacity() { return [Link]; }

/** Look at the student at the front without removing. */

public String peek() throws QueueUnderflowException {

if (isEmpty()) throw new QueueUnderflowException("Advising queue is empty");

return data[front];

/** String form: front → back. */

@Override

public String toString() {

StringBuilder sb = new StringBuilder();

for (int i = 0; i < count; i++) {

if (i > 0) [Link](", ");

[Link](data[(front + i) % [Link]]);

return [Link]();

}
}

How it works :

Circular buffer: front is the index of the current first student; rear is the last. We wrap with
% [Link].

• count tracks size so isEmpty() and isFull() are O(1) and unambiguous.

• enqueue throws QueueOverflowException when the 10 slots are filled; dequeue


throws QueueUnderflowException when empty.

• All queue ops are O(1).

What should be seen when running the provided driver

• Scenario 1 (≤ capacity): All students are enqueued and later dequeued in FIFO
order; no exceptions; size never exceeds capacity.

• Scenario 2 (> capacity): The first 10 students are accepted; additional students
trigger QueueOverflowException (the driver should catch/print a friendly message).

You might also like