View the step-by-step solution to:

Assignment #2 Due: All you will be given is the public interface for the class (the specications ); the choices of representation and algorithms are...

**
Prioritized queue class
Solves CPS 2232 programming assignment #2

@author ...
@version ...
*/
import java.util.NoSuchElementException;

public class PrioritizedQueue<T> {

/**
Construct an empty queue
*/
public PrioritizedQueue() {
...
}

/**
Add specified item to the rear of this queue, subject to the
specified priority; lower priority numbers have preference
[Runs in O(n) time]

@param item the item to be added
*/
public void enqueue(T item, int priority) {
...
}

/**
Remove and return the item at the front of this queue
Throw NoSuchElementException if queue is empty
[Runs in O(1) time]
*/
public T dequeue() {
...
}

/**
Determine the number of elements in the queue
[Runs in O(1) time]

@return the number of elements in the queue
*/
public int size() {
...
}

/**
Is the queue empty?
[Runs in O(1) time]

@return true if the queue is empty, false otherwise
*/
public boolean isEmpty() {
...
}

/**
Return the position of the specified item in this queue,
if found, and -1 if not found.
Position 0 is the front of the queue
[Runs in O(n) time]

@param item the item to locate
@return the position of the item, or -1
*/
int positionOf(T item) {
...
}

/**
Clear the queue so that it is now empty
[Runs in O(1) time]
*/
public void clear() {
...
}

/**
Remove the first occurence of the specified item from the queue
Throw NoSuchElementException if item is not in queue
[Runs in O(n) time]

@param item the item to remove
*/
public void remove(T item) {
...
}

/**
Remove all occurences of the specified item from the queue
Throw NoSuchElementException if item is not in queue
[Runs in O(n) time]

@param item the item to remove
*/
public void removeAll(T item) {
...
}

/**
Get the first element in the queue; set the cursor for iteration
[Runs in O(1) time]

@return the first element in the last; null if queue is empty
*/
public T first() {
...
}

/**
Get the next element in the queue, relative to a previous call
[Runs in O(1) time]

@return the next element in the queue; null if no more elements
*/
public T next() {
...
}

}

Assignment #2 Due: Friday, November 14, 2008 Fall 2008 CPS 2232—Data Structures and Algorithm Analysis This assignment is your first opportunity to implement a data structure completely on your own. All you will be given is the public interface for the class (the specifications ); the choices of representation and algorithms are up to you. A prioritized queue embodies some of the qualities of both a traditional queue and an ordered list. Each item entered into the queue has a inte- ger priority value. This priority determines where, precisely, in the queue the item will be placed: closer to the front than all lower priority items, but behind all items having the same or higher priority. For example, if a prioritized queue contains the following items with the given priorities in parentheses: George (5), John (6), Tom (6), Jim (6), Jim (10), John (10) [Note that, as is traditional in computer science, higher priorities are rep- resented by lower numbers. Note also that the priority values need not be contiguous.] If we were to add Bill to the queue with a priority of 10 (or even 11 or greater), he would be placed at the rear of the queue: George (5), John (6), Tom (6), Jim (6), Jim (10), John (10), Bill (10) On the other hand, if we had given Bill a priority of 6, he would have have been placed between the two Jims: George (5), John (6), Tom (6), Jim (6), Bill (6), Jim (10), John (10) Or, if we had given him a priority of 2 (or less), he would have jumped right to the front of the queue: Bill (2), George (5), John (6), Tom (6), Jim (6), Jim (10), John (10) Your job is to implement a PrioritizedQueue class in Java. Your class will provide a single constructor (with no parameters), which creates an empty queue, and the following methods: Efficiency Method Description O ( n ) void enqueue(T item, int p) Adds specified item to this queue, subject to priority p O (1) T dequeue() Removes and returns the item at the front of this queue; throws NoSuchElementException if queue is empty
Background image of page 1
Efficiency Method Description O (1) int size() Returns the number of items in the queue O (1) boolean isEmpty() Returns true if this queue is empty, false otherwise O ( n ) int positionOf(T item) Returns the position of the specified item in the queue, - 1 if not found; position 0 is the front of the queue O (1) clear() Removes all elements from this queue O ( n ) void remove(T item) Removes first occurrence (from front) of specified item; throws NoSuchElementException if item not in queue O ( n ) void removeAll(T item) Removes all occurrences of specified item from this queue; throws NoSuchElementException if item not in queue O (1) T first() Returns the first item in the queue (front); null if empty) O (1) T next() Return the next item in the queue, relative to a previous call to first or next ; returns null if end of queue reached 1. Copy the file named PrioritizedQueue-skeleton.java * from the di- rectory named leew/cps2232/hmwk0 into your own area, renaming it PrioritizedQueue.java . This file contains the Java code needed to define the public interface of the PrioritizedQueue class specified above. No data fields have been defined, nor have any of the methods been implemented. These are your responsibility. You may implement them in any way you wish, subject to the following two conditions: (a) You may not change any of the already existing code (the class’s public interface), and (b) Any data fields or methods you add must be declared private ; only the already declared methods may be public . 2. Note that the PrioritizedQueue class does not have (or need) a main() method. Neither does it do any input or output: Your com- pleted code must not use any Scanner objects or println statements. To test your code, you will need to write a separate program that cre- ates and manipulates PrioritizedQueue objects. You will not hand * Also at ftp://eve.kean.edu/pub/leew/cps2232/PrioritizedQueue-skeleton.java
Background image of page 2
Show entire document

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question