Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package csc143.data_structures;
- import java.lang.Throwable;
- /**
- * @author Vita Wiebe
- * @version PA5
- */
- public class UnboundedArrayQueue<E> implements UnboundedQueue<E> {
- // Fields:
- // unbAQueue is our underlying array object.
- // O(n)
- UnboundedArrayQueue<?>[] unbAQueue;
- // Capacity is the number of elements that can be
- // contained in UnbAQueue in its current incarnation.
- private int capacity;
- /**
- * O(n)
- * A default constructor, creates underlying array
- * with 10 elements.
- * @param Int capacity, how many elements it should
- * initially be able to contain.
- */
- public UnboundedArrayQueue(int capacity) throws QueueIsEmpty {
- unbAQueue = new UnboundedArrayQueue[capacity];
- }
- public UnboundedArrayQueue() throws QueueIsEmpty {
- unbAQueue = new UnboundedArrayQueue[10];
- }
- /**
- * Add adds the object in question, o, to the queue
- * in a FIFO manner.
- * O(n^2)
- * @param E e
- */
- public boolean add(E e) {
- // Temp variable declared in case
- // needed to adjust storage capacity.
- UnboundedArrayQueue<?>[] tempQueue = new UnboundedArrayQueue<?>[size() * 2];
- if(hasSpace()) {
- unbAQueue[size()] = e;
- } else {
- // New array with an additional storage.
- tempQueue = new UnboundedArrayQueue[unbAQueue.length * 2];
- for(int i = 0; i < unbAQueue.length - 1; i++) {
- tempQueue[i] = unbAQueue[i];
- }
- tempQueue[size()] = e;
- }
- // Set the new array to the inital array while disposing
- // of the inital array.
- unbAQueue = tempQueue;
- return true;
- }
- /**
- * Remove removes first object added to the queue.
- * O(n^2)
- * (Elements are removed in the order they are added, FIFO).
- * @throws NullPointerException
- */
- public E remove() {
- // New array with one less element.
- Object[] tempQueue = new Object[unbAQueue.length];
- // "removed" is the Object taken from the head
- // of the queue.
- E removed = null;
- if(hasContents()) {
- removed = unbAQueue[0];
- // Copy all the elements from the initial array
- // to new array.
- for(int i = 0; i < size(); i++){
- tempQueue[i] = unbAQueue[i + 1];
- }
- }
- // Set the new array to the inital array while disposing
- // of the inital array.
- unbAQueue = tempQueue;
- return removed;
- }
- /**
- * O(k)
- * @param None
- * @return The first element added to the queue (
- * i.e. the last element.
- */
- public E peek() {
- try {
- if (unbAQueue.length == 0) {
- throw new QueueIsEmpty("Empty.");
- }
- return unbAQueue[unbAQueue.length - 1];
- }catch(QueueIsEmpty q) {
- System.out.println(q.getMessage());
- }
- return 0;
- }
- /**
- * O(n^2)
- * Checks to see whether the queue has capacity to add more elements.
- * @param None
- * @return boolean
- */
- public boolean hasSpace() {
- for (int i=0; i < unbAQueue.length; i++){
- if (unbAQueue[i] == null) {
- return true;
- }
- }
- return false;
- }
- /**
- * O(n^2)
- * Checks to see whether the queue has had any elements added to it.
- * @param None
- * @return boolean
- */
- public boolean hasContents() {
- for (int i = 0; i < unbAQueue.length; i++){
- if(unbAQueue[i] != null) {
- return true;
- }
- }
- return false;
- }
- /**
- * O(n^2)
- * @param None
- * @return The number of elements that are currently stored in the structure.
- */
- public int size() {
- // Tracks the number of elements that aren't null (empty).
- int count = 0;
- if(hasContents()) {
- for(int i = 0; i < unbAQueue.length - 1; i++) {
- if((Integer)unbAQueue[i] != 0){
- count++;
- }
- }
- }
- return count;
- }
- /**
- * O(n^2)
- * @param None
- * @return A string representation of the structure.
- */
- public String toString() {
- String stringVersion = new String("");
- for (int i = 0; i < unbAQueue.length - 1; i++){
- stringVersion += (String)unbAQueue[i];
- }
- return stringVersion;
- }
- /**
- * An overload of toString to return only
- * a String representation of the first item in queue.
- * @param Object current, the object at head of
- * the queue.
- */
- public String toString(E current) {
- String stringVersion = new String("" + current + " ");
- return stringVersion;
- }
- public static void main(String[] args) throws QueueIsEmpty {
- UnboundedArrayQueue ourQueue = new UnboundedArrayQueue();
- //ourQueue.add("Miser");
- System.out.println(ourQueue.toString() + " ");
- System.out.println(ourQueue.toString(ourQueue.peek()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement