diff --git a/src/assn03/COMP210-F23-Assn-3-v2.6.pdf b/src/assn03/COMP210-F23-Assn-3-v2.6.pdf new file mode 100644 index 0000000..501cc31 Binary files /dev/null and b/src/assn03/COMP210-F23-Assn-3-v2.6.pdf differ diff --git a/src/assn03/LinkedList.java b/src/assn03/LinkedList.java new file mode 100644 index 0000000..bbcdd57 --- /dev/null +++ b/src/assn03/LinkedList.java @@ -0,0 +1,367 @@ +package assn03; + +public class LinkedList { + private Node head = null; + private Node tail = null; + private int size = 0; + + + /** + * Task1 + * Remove the node at index i of the list. + * Note that the first element is at index 0 + * If i is larger than the size of the list, throw an IndexOutOfBounds Exception + * + * ex: list: A -> B -> C -> D + * i: 1 + * list after removeAtIndex: A -> C -> D + * + * @param i - index of node to remove + */ + public void removeAtIndex(int i) { + if (i < 0 || i >= size) + throw new IndexOutOfBoundsException(); + + if (i == 0) { + head = head.getNext(); + size--; + return; + } + + Node current = head; + for (int j = 0; j < i - 1; j++) { + current = current.getNext(); + } + + current.setNext(current.getNext().getNext()); + size--; + } + + + + /** + * Task2 + * Return true if this linked list is equal to the list argument, false otherwise. + * Two lists are equal if they have the same size, and the same + * elements in the same order. + * ex: list: 1 -> 4 -> 2 + * list2: 1 -> 4 -> 2 + * return: true + * + * list: 1 -> 5 + * list2: 2 -> 5 + * return false; + * + * @param list2 - the list to compare with the current list + * @return true if the lists have the same elements in the same order, false otherwise + */ + public boolean isEqual(LinkedList list2) { + if (size != list2.size()) + return false; + + Node current = head; + Node current2 = list2.getHead(); + while (current != null) { + if (!current.getValue().equals(current2.getValue())) + return false; + current = current.getNext(); + current2 = current2.getNext(); + } + + return true; + } + + + + /** + * Task3 + * Given a sorted linked list, remove the duplicate values from the list + * ex: list: 5 -> 6 -> 7 -> 7 -> 7 -> 8 -> 8 -> 9 + * list after removeRepeats: 5 -> 6 -> 7 -> 8 -> 9 + * + */ + public void removeRepeats() { + if (isEmpty()) + return; + + T[] arr = (T[]) new Object[size()]; + Node current = head; + int i = 0; + + while (current != null && current.getNext() != null) { + if (current.getValue().equals(current.getNext().getValue())) { + current.setNext(current.getNext().getNext()); + size--; + } else { + current = current.getNext(); + } + } + } + + + + + /** + * Task4 + * Reverse the list + * + * ex list: 10 -> 9 -> 8 -> 7 + * list after reverse: 7 -> 8 -> 9 -> 10 + * + */ + public void reverse() { + if (isEmpty()) + return; + + Node current = head; + Node prev = null; + Node next = null; + + while (current != null) { + next = current.getNext(); + current.setNext(prev); + prev = current; + current = next; + } + + head = prev; + } + + + + + + /** + * Task5 + * Merge the given linked list2 into the current list. The 2 lists will always be + * either the same size, or the current list will be longer than list2. + * The examples below show how to handle each case. + * + * Note: Do NOT create and return a new list, merge the second list into the first one. + * + * ex: list: 1 -> 2 -> 3 + * list2: 4 -> 5 -> 6 + * return: 1 -> 4 -> 2 -> 5 -> 3 -> 6 + * + * list: 1 -> 2 -> 3 -> 4 + * list2: 5 -> 6 + * return 1 -> 5 -> 2 -> 6 -> 3 -> 4 + * + * @param list2 + */ + public void merge(LinkedList list2) { + if (isEmpty()) + return; + + Node current = head; + Node current2 = list2.getHead(); + Node next = null; + + while (current2 != null) { + next = current.getNext(); + current.setNext(current2); + current2 = current2.getNext(); + current.getNext().setNext(next); + current = next; + } + } + + /* Implementations below are being given to you. Do not modify below this. */ + + public int size() { + return size; + } + + public boolean isEmpty() { + return size == 0; + } + + public void clear() { + head = null; + tail = null; + size = 0; + } + + public boolean contains(Object element) { + Node current = head; + while(current != null) { + if(current.getValue().equals(element)) { + return true; + } + current = current.getNext(); + } + return false; + } + + public T[] toArray() { + T[] arr = (T[]) new Object[size()]; + Node current = head; + int i = 0; + if(isEmpty()) { + return arr; + } + while(current != null){ + arr[i] = current.getValue(); + current = current.getNext(); + i++; + } + return arr; + } + + public void add(Object element) { + Node newNode = new NodeImpl((T) element, null); + if(isEmpty()) { + head = newNode; + tail = newNode; + size++; + } else { + tail.setNext(newNode); + tail = newNode; + size++; + } + + } + + public boolean remove(Object element) { + Node current = head; + if(isEmpty()) { + return false; + } + if(current.getValue() == element){ + head = head.getNext(); + size--; + return true; + } + while(current.getNext().getValue() != element) { + current = current.getNext(); + if(current == null) { + return false; + } + } + if(current.getNext().getNext() == null) { + tail = current; + } + current.setNext(current.getNext().getNext()); + size--; + return true; + } + + public T get(int index) { + validIndex(index); + Node current = head; + int i = 0; + while (i < index) { + current = current.getNext(); + i++; + } + return current.getValue(); + } + + public T set(int index, Object element) { + validIndex(index); + Node current = head; + T prevValue = null; + int i = 0; + if(index == 0) { + prevValue = head.getValue(); + head.setValue((T) element); + } else { + while(current != null) { + if(i == index) { + prevValue = current.getValue(); + current.setValue((T) element); + return prevValue; + } + current = current.getNext(); + i++; + } + } + + return prevValue; + } + + public void add(int index, Object element) { + if(index > size) { + validIndex(index); + } + Node current = head; + int i = 0; + if(index == 0) { + if(isEmpty()) { + add(element); + return; + } else { + Node newNode = new NodeImpl((T) element, head.getNext()); + head = newNode; + size++; + return; + } + + } else if(index == size) { + add(element); + return; + } + while(current != null) { + if(i == (index - 1)) { + Node temp = current.getNext(); + Node newNode = new NodeImpl((T) element, temp); + current.setNext(newNode); + size++; + return; + } else { + current = current.getNext(); + i++; + } + } + } + + public int indexOf(Object element) { + Node current = head; + int index = 0; + while(current != null) { + if(current.getValue().equals((T) element)) { + return index; + } + index++; + current = current.getNext(); + } + return -1; + } + + public int lastIndexOf(Object element) { + Node current = head; + int index = -1; + int i = 0; + while(current != null) { + if(current.getValue().equals ((T) element)) { + index = i; + } + i++; + current = current.getNext(); + } + return index; + } + + public void validIndex(int i) { + if(i < 0 || i >= size) { + throw new IndexOutOfBoundsException("Invalid index"); + } + } + public Node getHead() { + return head; + } + + @Override + public String toString() { + String list = ""; + Node current = head; + while(current != null) { + if(current.getNext() == null) + list+= current.getValue(); + else + list += current.getValue() + " -> "; + current = current.getNext(); + } + return list; + } +} \ No newline at end of file diff --git a/src/assn03/Main.java b/src/assn03/Main.java new file mode 100644 index 0000000..3195142 --- /dev/null +++ b/src/assn03/Main.java @@ -0,0 +1,72 @@ +package assn03; +public class Main { + + public static void main(String[] args) { + LinkedList list = new LinkedList(); + list.add(10); + list.add(20); + list.add(60); + list.add(30); + System.out.println("list = " + list.toString()); + System.out.println("size of list = " + list.size()); + System.out.println("list contains 10?: " + list.contains(10)); // implemented + System.out.println("list contains 50?: " + list.contains(50)); + System.out.println("set element at index 2 to be 10"); + list.set(2, 10); + System.out.println("get element at index 2 = " + list.get(2)); + System.out.println("list = " + list.toString()); + System.out.println("Last Index of element 10 in list = " + list.lastIndexOf(10)); + + list.remove(20); + System.out.println("list after removing 20 = " + list.toString()); + + System.out.println("index of '30' = " + list.indexOf(30)); + + // Test task 1 + list.removeAtIndex(1); // TBD + System.out.println("Task 1: list after removing element at index 1 = " + list.toString()); + + // Test task 2 + LinkedList list2 = new LinkedList(); + list2.add(10); + list2.add(10); + System.out.println("list2 = " + list2.toString()); + System.out.println("Task 2: list == list2 ?: " + list.isEqual(list2)); // not yet implemented + + // Test task 3 + System.out.println("list before removing repeats = " + list.toString()); + list.removeRepeats(); + System.out.println("Task 3: list after removing repeats = " + list.toString()); + + // Test task 4 + LinkedList list3 = new LinkedList(); + list3.add(10); + list3.add(20); + list3.add(30); + list3.add(40); + System.out.println("list3 = " + list3.toString()); + list3.reverse(); + System.out.println("Task 4: list3 after reversing = " + list3.toString()); + + // Test task 5 + + // * list: 1 -> 2 -> 3 -> 4 + // * list2: 5 -> 6 + // * return 1 -> 5 -> 2 -> 6 -> 3 -> 4 + // * + list = new LinkedList(); + list2 = new LinkedList(); + int[] arr = { 1, 2, 3, 4 }; + int[] arr2 = { 5, 6 }; + for (int i = 0; i < arr.length; i++) { + list.add(arr[i]); + } + for (int i = 0; i < arr2.length; i++) { + list2.add(arr2[i]); + } + System.out.println("list = " + list.toString()); + System.out.println("list2 = " + list2.toString()); + list.merge(list2); + System.out.println("Task 5: list after merging with list2 = " + list.toString()); + } +} diff --git a/src/assn03/Node.java b/src/assn03/Node.java new file mode 100644 index 0000000..15e778d --- /dev/null +++ b/src/assn03/Node.java @@ -0,0 +1,18 @@ +package assn03; + + +public interface Node { + + T getValue(); + + void setValue(T value); + + Node getNext(); + + void setNext(Node next); + + + default boolean hasNext() { + return (getNext() != null); + } +} \ No newline at end of file diff --git a/src/assn03/NodeImpl.java b/src/assn03/NodeImpl.java new file mode 100644 index 0000000..85d8c6d --- /dev/null +++ b/src/assn03/NodeImpl.java @@ -0,0 +1,33 @@ +package assn03; + + +public class NodeImpl implements Node { + + private T _value; + private Node _next; + + public NodeImpl(T value, Node next) { + _value = value; + _next = next; + } + + @Override + public T getValue() { + return _value; + } + + @Override + public void setValue(T value) { + _value = value; + } + + @Override + public Node getNext() { + return _next; + } + + @Override + public void setNext(Node next) { + _next = next; + } +} diff --git a/src/assn04/BST.java b/src/assn04/BST.java new file mode 100644 index 0000000..9a67100 --- /dev/null +++ b/src/assn04/BST.java @@ -0,0 +1,51 @@ +package assn04; + +public interface BST> { + + /** + * Inserts element into the tree in the appropriate position. + * Either returns the mutated tree after insertion or a new tree + * with the inserted element. + * + * @param element to be added to the tree + * @return BST after insertion + **/ + BST insert(T element); + + /** + * Removes the element from the tree if it is present. + * Either returns the possibly mutated tree after removal or an empty tree. + * + * @param element to be removed from tree + * @return BST after removal + */ + BST remove(T element); + + /** + * Prints the tree in depth-first pre-order traversal. + * Print the elements all in one line with a space after each element. + */ + void printPreOrderTraversal(); + + /** + * Prints the tree in post-order traversal. + * Print the elements all in one line with a space after each element. + */ + void printPostOrderTraversal(); + + //@return int which is based on the number of edges. -1 for an EmptyBST. + int getHeight(); + +/** + * Following are some methods which are self-explanatory. + */ + T findMin(); + + BST getLeft(); + + BST getRight(); + + T getElement(); + + boolean isEmpty(); +} diff --git a/src/assn04/COMP210-F23-Assn4-v2.4.pdf b/src/assn04/COMP210-F23-Assn4-v2.4.pdf new file mode 100644 index 0000000..faf58fa Binary files /dev/null and b/src/assn04/COMP210-F23-Assn4-v2.4.pdf differ diff --git a/src/assn04/EmptyBST.java b/src/assn04/EmptyBST.java new file mode 100644 index 0000000..5f73061 --- /dev/null +++ b/src/assn04/EmptyBST.java @@ -0,0 +1,54 @@ +package assn04; + +public class EmptyBST> implements BST { + + @Override + public BST insert(T element) { + return new NonEmptyBST(element); + } + + @Override + public BST remove(T element) { + return this; + } + + @Override + public int getHeight() { + return -1; + } + + @Override + public void printPreOrderTraversal() { + return; + } + @Override + public T findMin() { + throw new UnsupportedOperationException(); + } + + @Override + public void printPostOrderTraversal() { + return; + } + + @Override + public BST getLeft() { + throw new UnsupportedOperationException(); + } + + @Override + public BST getRight() { + throw new UnsupportedOperationException(); + } + + @Override + public T getElement() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isEmpty() { + return true; + } + +} diff --git a/src/assn04/Main.java b/src/assn04/Main.java new file mode 100644 index 0000000..3e72881 --- /dev/null +++ b/src/assn04/Main.java @@ -0,0 +1,19 @@ +package assn04; + +public class Main { + public static void main(String[] args) { + /* + This is a basic example of how to create a BST and add values + to it (which have been commented out). + You should add more examples and use this class to debug your code + */ + BST bst = new NonEmptyBST(3); + bst = bst.insert(8); + bst = bst.insert(1); + bst = bst.insert(9); + bst = bst.insert(4); + bst.printPreOrderTraversal(); + + } + +} diff --git a/src/assn04/NonEmptyBST.java b/src/assn04/NonEmptyBST.java new file mode 100644 index 0000000..7ebc142 --- /dev/null +++ b/src/assn04/NonEmptyBST.java @@ -0,0 +1,97 @@ +package assn04; + +public class NonEmptyBST> implements BST { + private T _element; + private BST _left; + private BST _right; + + public NonEmptyBST(T element) { + _left = new EmptyBST(); + _right = new EmptyBST(); + _element = element; + } + + // TODO: insert + @Override + public BST insert(T element){ + if (element.compareTo(_element) < 0) { + _left = _left.insert(element); + } else if (element.compareTo(_element) >= 0) { + _right = _right.insert(element); + } + + return this; + } + + // TODO: findMin + @Override + public T findMin() { + BST current = this; + while (!current.getLeft().isEmpty()) { + current = current.getLeft(); + } + return current.getElement(); + } + + // TODO: remove + @Override + public BST remove(T element) { + if (element.compareTo(_element) < 0) { + _left = _left.remove(element); + } else if (element.compareTo(_element) > 0) { + _right = _right.remove(element); + } else { + if (_left.isEmpty()) { + return _right; + } else if (_right.isEmpty()) { + return _left; + } else { + _element = _right.findMin(); + _right = _right.remove(_element); + } + } + + return this; + } + + // TODO: printPreOrderTraversal + @Override + public void printPreOrderTraversal() { + System.out.print(_element + " "); + _left.printPreOrderTraversal(); + _right.printPreOrderTraversal(); + } + + // TODO: printPostOrderTraversal + @Override + public void printPostOrderTraversal() { + _left.printPostOrderTraversal(); + _right.printPostOrderTraversal(); + System.out.print(_element + " "); + } + + @Override + public int getHeight() { + return Math.max(_left.getHeight(), _right.getHeight())+1; + } + + @Override + public BST getLeft() { + return _left; + } + + @Override + public BST getRight() { + return _right; + } + + @Override + public T getElement() { + return _element; + } + + @Override + public boolean isEmpty() { + return false; + } +} diff --git a/src/assn05/BinaryHeap.java b/src/assn05/BinaryHeap.java new file mode 100644 index 0000000..78f4e39 --- /dev/null +++ b/src/assn05/BinaryHeap.java @@ -0,0 +1,37 @@ +package assn05; + + +public interface BinaryHeap> { + /** + * Returns number of elements in heap + * @return numbers of elements in heap + */ + int size(); + + /** + * Create new hospital.Prioritized object and insert it into the heap + * @param value + * @param priority + */ + void enqueue(V value, P priority); + + /** + * Remove the element with the smallest priority from the heap + * and return its value + * @return the value of the removed element + */ + V dequeue(); + + /** + * return the largest value in the heap without removing it + * @return the largest value in the heap + */ + V getMax(); + + + /** + * Retrieves contents of heap as an array. + * @return array of hospital.Prioritized objects in the order stored in the heap + */ + Prioritized[] getAsArray(); +} diff --git a/src/assn05/COMP210-F23-Assn5-v1.4.pdf b/src/assn05/COMP210-F23-Assn5-v1.4.pdf new file mode 100644 index 0000000..d8297aa Binary files /dev/null and b/src/assn05/COMP210-F23-Assn5-v1.4.pdf differ diff --git a/src/assn05/Main.java b/src/assn05/Main.java new file mode 100644 index 0000000..f8a78b1 --- /dev/null +++ b/src/assn05/Main.java @@ -0,0 +1,129 @@ +package assn05; + +public class Main { + + public static void main(String[] args) { + testP1(); + testP2(); + testP3(); + testP4(); + } + + // test Part 1 + public static void testP1(){ + /* + Part 1 - Write some tests to convince yourself that your code for Part 1 is working + */ + + SimpleEmergencyRoom simplePQ = new SimpleEmergencyRoom(); + simplePQ.addPatient(5,5); + simplePQ.addPatient(2,2); + simplePQ.addPatient(3,3); + simplePQ.addPatient(1,1); + simplePQ.addPatient(4,4); + + System.out.println("Simple ER: "); + while (simplePQ.size() > 0) System.out.println(simplePQ.dequeue().getValue()); + System.out.println(); + } + + // test Part 2 + public static void testP2(){ + /* + Part 2 - Write some tests to convince yourself that your code for Part 2 is working + */ + + MaxBinHeapER binHeap = new MaxBinHeapER(); + binHeap.enqueue(5, 5); + binHeap.enqueue(2, 2); + binHeap.enqueue(3, 3); + binHeap.enqueue(1, 1); + binHeap.enqueue(4, 4); + + System.out.println("MaxBinHeapER: "); + while (binHeap.size() > 0) + System.out.println(binHeap.dequeue()); + System.out.println(); + } + + /* + Part 3 + */ + public static void testP3(){ + MaxBinHeapER transfer = new MaxBinHeapER(makePatients()); + Prioritized[] arr = transfer.getAsArray(); + for(int i = 0; i < transfer.size(); i++) { + System.out.println("Value: " + arr[i].getValue() + + ", Priority: " + arr[i].getPriority()); + } + } + + /* + Part 4 + */ + public static void testP4() { + /* + Part 4 - Write some tests to convince yourself that your code for Part 4 is working + */ + System.out.println("Compare runtimes: "); + double[] results = compareRuntimes(); + System.out.println("Total time: " + results[0] + ", Average time: " + results[1]); + System.out.println("Total time: " + results[2] + ", Average time: " + results[3]); + } + + public static void fillER(MaxBinHeapER complexER) { + for(int i = 0; i < 100000; i++) { + complexER.enqueue(i); + } + } + public static void fillER(SimpleEmergencyRoom simpleER) { + for(int i = 0; i < 100000; i++) { + simpleER.addPatient(i); + } + } + + public static Patient[] makePatients() { + Patient[] patients = new Patient[10]; + for(int i = 0; i < 10; i++) { + patients[i] = new Patient(i); + } + return patients; + } + + public static double[] compareRuntimes() { + // Array which you will populate as part of Part 4 + double[] results = new double[4]; + + SimpleEmergencyRoom simplePQ = new SimpleEmergencyRoom(); + fillER(simplePQ); + + // Code for (Task 4.1) Here + double start = System.nanoTime(); + int size = simplePQ.size(); + for (int i = 0; i < size; i++) { + simplePQ.dequeue(); + } + double total = System.nanoTime() - start; + results[0] = total; + results[1] = total / size; + + MaxBinHeapER binHeap = new MaxBinHeapER(); + fillER(binHeap); + + // Code for (Task 4.2) Here + start = System.nanoTime(); + size = binHeap.size(); + for (int i = 0; i < size; i++) { + binHeap.dequeue(); + } + total = System.nanoTime() - start; + results[2] = total; + results[3] = total / size; + + return results; + } + +} + + + diff --git a/src/assn05/MaxBinHeapER.java b/src/assn05/MaxBinHeapER.java new file mode 100644 index 0000000..f4e8a7b --- /dev/null +++ b/src/assn05/MaxBinHeapER.java @@ -0,0 +1,117 @@ +package assn05; + +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.List; + +public class MaxBinHeapER > implements BinaryHeap { + + private List> _heap; + + /** + * Constructor that creates an empty heap of hospital.Prioritized objects. + */ + public MaxBinHeapER() { + _heap = new ArrayList<>(); + } + + /** + * Constructor that builds a heap given an initial array of hospital.Prioritized objects. + */ + // TODO (Part 3): overloaded constructor + public MaxBinHeapER(Prioritized[] initialEntries ) { + _heap = new ArrayList<>(); + for (Prioritized entry : initialEntries) + _heap.add(entry); + + for (int i = size() / 2 - 1; i >= 0; i--) + heapDown(i); + } + + @Override + public int size() { + return _heap.size(); + } + + // TODO: enqueue + @Override + public void enqueue(V value, P priority) { + Patient patient = new Patient(value, priority); + _heap.add(patient); + heapUp(size() - 1); + } + + // TODO: enqueue + public void enqueue(V value) { + Patient patient = new Patient(value); + _heap.add(patient); + heapUp(size() - 1); + } + + // TODO: dequeue + @Override + public V dequeue() { + if (size() == 0) + return null; + + + Prioritized max = _heap.get(0); + _heap.set(0, _heap.get(size() - 1)); + _heap.remove(size() - 1); + + if (size() > 1) + heapDown(0); + + return max.getValue(); + + } + + // TODO: getMax + @Override + public V getMax() { + if (size() == 0) + return null; + + return _heap.get(0).getValue(); + } + + @Override + public Prioritized[] getAsArray() { + Prioritized[] result = (Prioritized[]) Array.newInstance(Prioritized.class, size()); + return _heap.toArray(result); + } + + private void heapUp(int index) { + if (index == 0) + return; + + int parentIndex = (index - 1) / 2; + if (_heap.get(index).compareTo(_heap.get(parentIndex)) > 0) { + swap(index, parentIndex); + heapUp(parentIndex); + } + } + + private void heapDown(int index) { + int left = 2 * index + 1; + int right = 2 * index + 2; + int maxIndex = index; + + if (left < size() && _heap.get(left).compareTo(_heap.get(maxIndex)) > 0) + maxIndex = left; + + if (right < size() && _heap.get(right).compareTo(_heap.get(maxIndex)) > 0) + maxIndex = right; + + if (maxIndex != index) { + swap(index, maxIndex); + heapDown(maxIndex); + } + } + + private void swap(int index1, int index2) { + Prioritized temp = _heap.get(index1); + _heap.set(index1, _heap.get(index2)); + _heap.set(index2, temp); + } +} diff --git a/src/assn05/Patient.java b/src/assn05/Patient.java new file mode 100644 index 0000000..7ed4de5 --- /dev/null +++ b/src/assn05/Patient.java @@ -0,0 +1,37 @@ +package assn05; + +import java.util.Random; + +public class Patient> implements Prioritized { + private Integer priority; + private V value; + + + public Patient(V value, Integer priority) { + this.value = value; + this.priority = priority; + } + + public Patient(V value) { + this.value = value; + calculatePriority(); + } + + @Override + public V getValue() { + return value; + } + + @Override + public Integer getPriority() { return priority; } + + private void calculatePriority() { + Random random = new Random(); + this.priority = (Integer) new java.lang.Integer(random.nextInt(1000000)); + } + + @Override + public int compareTo(Prioritized other) { + return this.priority.compareTo(other.getPriority()); + } +} diff --git a/src/assn05/Prioritized.java b/src/assn05/Prioritized.java new file mode 100644 index 0000000..7ef4b60 --- /dev/null +++ b/src/assn05/Prioritized.java @@ -0,0 +1,7 @@ +package assn05; + +public interface Prioritized> { + V getValue(); + P getPriority(); + int compareTo(Prioritized other); +} \ No newline at end of file diff --git a/src/assn05/SimpleEmergencyRoom.java b/src/assn05/SimpleEmergencyRoom.java new file mode 100644 index 0000000..68f29dc --- /dev/null +++ b/src/assn05/SimpleEmergencyRoom.java @@ -0,0 +1,41 @@ +package assn05; + +import java.util.ArrayList; +import java.util.List; + +public class SimpleEmergencyRoom { + private List patients; + + public SimpleEmergencyRoom() { + patients = new ArrayList<>(); + } + + public Patient dequeue() { + Patient max = patients.get(0); + + for (Patient patient : patients) + if (patient.compareTo(max) > 0) max = patient; + + patients.remove(max); + return max; + } + + public void addPatient(V value, P priority) { + Patient patient = new Patient(value, (Integer) priority); + patients.add(patient); + } + + public void addPatient(V value) { + Patient patient = new Patient(value); + patients.add(patient); + } + + public List getPatients() { + return patients; + } + + public int size() { + return patients.size(); + } + +} diff --git a/submit b/submit index c00bde0..8135fc5 100755 --- a/submit +++ b/submit @@ -1,2 +1,3 @@ #! /bin/bash -zip -r submission.zip src/assn$1 +rm -rf submission.zip +zip -r submission.zip src/assn$1/*.java \ No newline at end of file