iterator() {
+ return new LinkedListIterator();
+ }
+
+ /**
+ * Performs the given action for each element of the {@code Iterable}
+ * until all elements have been processed or the action throws an
+ * exception. Actions are performed in the order of iteration, if that
+ * order is specified. Exceptions thrown by the action are relayed to the
+ * caller.
+ *
+ * The behavior of this method is unspecified if the action performs
+ * side effects that modify the underlying source of elements, unless an
+ * overriding class has specified a concurrent modification policy.
+ *
+ * @param action The action to be performed for each element
+ * @throws NullPointerException if the specified action is null
+ * @implSpec
The default implementation behaves as if:
+ *
{@code
+ * for (T t : this)
+ * action.accept(t);
+ * }
+ * @since 1.8
+ */
+ @Override
+ public void forEach(Consumer super E> action) {
+ List.super.forEach(action);
+ }
+
+ /**
+ * Creates a {@link Spliterator} over the elements described by this
+ * {@code Iterable}.
+ *
+ * @return a {@code Spliterator} over the elements described by this
+ * {@code Iterable}.
+ * @implSpec The default implementation creates an
+ * early-binding
+ * spliterator from the iterable's {@code Iterator}. The spliterator
+ * inherits the fail-fast properties of the iterable's iterator.
+ * @implNote The default implementation should usually be overridden. The
+ * spliterator returned by the default implementation has poor splitting
+ * capabilities, is un-sized, and does not report any spliterator
+ * characteristics. Implementing classes can nearly always provide a
+ * better implementation.
+ * @since 1.8
+ */
+ @Override
+ public Spliterator spliterator() {
+ return List.super.spliterator();
+ }
+
+ private class LinkedListIterator implements Iterator {
+ private int i;
+ Node current = head;
+
+ private LinkedListIterator() {
+ this.i = 0;
+ }
+
+ /**
+ * Tracker of if there's a next for the iterator.
+ *
+ * @return returns the boolean representing if there's another element after the current element.
+ */
+ public boolean hasNext() {
+ return i < size;
+ }
+
+ /**
+ * Item to retrieve the element values for the iterator.
+ * Runtime is O(1) because it's only looking at a single location and not looping.
+ *
+ * @return the data of the current element.
+ */
+ public E next() {
+ E currentItem = current.data;
+ current = current.next;
+ i++;
+ return currentItem;
+ }
+ }
+
+}
diff --git a/src/LinkedQueue.java b/src/LinkedQueue.java
new file mode 100644
index 0000000..698f2a9
--- /dev/null
+++ b/src/LinkedQueue.java
@@ -0,0 +1,134 @@
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+/**
+ * * Implementation of the LinkedQueue using the Queue interface. This is my term assignment 2 FIFO Queue.
+ *
+ * @param class / data type of the items in the LinkedQueue
+ * @author Rob Smith
+ * @version 1.0
+ */
+public class LinkedQueue implements Queue {
+ private Node oldest;
+ private Node newest;
+ private int size;
+
+ /**
+ * Constructor for LinkedQueue class.
+ */
+ public LinkedQueue() {
+ this.oldest = null;
+ this.newest = null;
+ this.size = 0;
+ }
+
+ private class Node {
+ E data;
+ Node next;
+ }
+
+ /**
+ * Checks if the queue is empty.
+ * Runtime of this is O(1). It's simply returning a single boolean with no loops.
+ *
+ * @return true if the LinkedQueue is empty, false otherwise
+ */
+ public boolean isEmpty() {
+ return size == 0;
+ }
+
+ /**
+ * Checks the size of the list.
+ * Runtime of this is O(1). It's simply returning a single int with no loops.
+ *
+ * @return the size of the LinkedQueue
+ */
+ public int size() {
+ return size;
+ }
+
+ /**
+ * Adds the specified object to the front of the list.
+ * Runtime of this is O(1). It always takes the same amount of time to add a node to the end of a linked list when
+ * using a second pointer because we don't need to traverse the linked list to locate the end node.
+ *
+ * @param item the item to be added
+ */
+ public void enqueue(E item) {
+ Node newNode = new Node();
+ if (size == 0) {
+ newest = newNode;
+ oldest = newest;
+ } else {
+ newest.next = newNode;
+ newest = newNode;
+ }
+ newNode.data = item;
+ newest.next = null;
+ size++;
+
+ }
+
+ /**
+ * Dequeue removes the last element in the linked list and returns the value of the data inside it.
+ * Runtime for this operation is O(1), removing the last element is a constant time operation regardless of the
+ * size of the list since we're tracking it with a tail pointer.
+ *
+ * @return the data from the node removed from the list.
+ */
+ public E dequeue() {
+ if (oldest == null) {
+ throw new NoSuchElementException("The list is empty");
+ } else {
+ E result = oldest.data;
+ oldest = oldest.next;
+ size--;
+ if (isEmpty()) {
+ newest = null;
+ }
+ return result;
+ }
+ }
+
+ /**
+ * returns an object to iterate through the LinkedQueue.
+ * Runtime is O(1) because it is merely instantiating an object with no looping.
+ *
+ * @return an object to iterate through the LinkedQueue
+ */
+ @Override
+ public Iterator iterator() {
+ return new LinkedQueueIterator();
+ }
+
+ private class LinkedQueueIterator implements Iterator {
+
+ private Node current = oldest;
+
+ /**
+ * Returns {@code true} if the iteration has more elements.
+ * (In other words, returns {@code true} if {@link #next} would
+ * return an element rather than throwing an exception.)
+ * Runtime is O(1) because there is no looping involved it is just checking where int he LinkedQueue the
+ * iterator is currently located.
+ *
+ * @return {@code true} if the current position is not the end of the list, aka, null.
+ */
+ public boolean hasNext() {
+ return current != null;
+ }
+
+ /**
+ * Returns the next element in the iteration.
+ * Runtime for this is O(1) because it's only accessing a single point of data not looping through the
+ * LinkedQueue in its entirety.
+ *
+ * @return the next element in the iteration
+ */
+ public E next() {
+ E value = current.data;
+ current = current.next;
+ return value;
+ }
+ }
+}
diff --git a/src/LinkedStack.java b/src/LinkedStack.java
new file mode 100644
index 0000000..ee0c2df
--- /dev/null
+++ b/src/LinkedStack.java
@@ -0,0 +1,140 @@
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+/**
+ * Linked Node stack implementation. LIFO.
+ *
+ * @param class / data type of the items in the LinkedStack
+ * @author Rob smith
+ * @version 1.0
+ */
+public class LinkedStack implements Stack {
+
+ private Node head;
+ private int size;
+
+ private class Node {
+ E data;
+ Node next;
+ }
+
+ /**
+ * Constructor for LinkedStack class.
+ */
+ public LinkedStack() {
+ this.head = null;
+ this.size = 0;
+ }
+
+ /**
+ * Checks if the list is empty.
+ * Runtime of this is O(1). It's simply returning a single boolean with no loops.
+ *
+ * @return true if the LinkedStack is empty, false otherwise
+ */
+ public boolean isEmpty() {
+ return size == 0;
+ }
+
+ /**
+ * Checks the size of the list.
+ * Runtime of this is O(1). It's simply returning a single int with no loops.
+ *
+ * @return the size of the LinkedStack
+ */
+ public int size() {
+ return size;
+ }
+
+ /**
+ * Adds the specified object to the front of the list.
+ * Runtime of this is O(1). It always takes the same amount of time to add a node to the front of a linked list
+ * because there is no looping to shift like in an array.
+ *
+ * @param item the item to be added
+ */
+ public void push(E item) {
+ Node newNode = new Node();
+ newNode.data = item;
+ if (head != null) {
+ newNode.next = head;
+ }
+ head = newNode;
+ this.size++;
+ }
+
+
+ /**
+ * Pop removes the first element in the linked list and returns the value of the data inside it.
+ * Runtime for this operation is O(1), removing the first element is a constant time operation regardless of the
+ * size of the list.
+ *
+ * @return the data from the node removed from the list.
+ */
+ public E pop() {
+ if (head == null) {
+ throw new NoSuchElementException("The list is empty");
+ } else {
+ E result = head.data;
+ head = head.next;
+ size--;
+ return result;
+ }
+ }
+
+ /**
+ * Returns the item at the top of the stack.
+ * Does not modify the stack or the item at the top.
+ * Runtime is O(1) because there is no looping involved.)
+ *
+ * @return item at the top of the stack.
+ */
+ @Override
+ public E peek() {
+ return head.data;
+ }
+
+ /**
+ * returns an object to iterate through the Linked-stack.
+ * Runtime is O(1) because it is merely instantiating an object with no looping.
+ *
+ * @return an object to iterate through the Linked-stack
+ */
+ public Iterator iterator() {
+ return new LinkedStackIterator();
+ }
+
+ private class LinkedStackIterator implements Iterator {
+
+ Node current = head;
+
+ /**
+ * Returns {@code true} if the iteration has more elements.
+ * (In other words, returns {@code true} if {@link #next} would
+ * return an element rather than throwing an exception.)
+ * Runtime is O(1) because there is no looping involved it is just checking where int he LinkedStack the
+ * iterator is currently located.
+ *
+ * @return {@code true} if the current position is not the end of the list, aka, null.
+ */
+ @Override
+ public boolean hasNext() {
+ return current != null;
+ }
+
+ /**
+ * Returns the next element in the iteration.
+ * Runtime for this is O(1) because it's only accessing a single point of data not looping through the
+ * LinkedStack in its entirety.
+ *
+ * @return the next element in the iteration
+ */
+ @Override
+ public E next() {
+ E value = current.data;
+ current = current.next;
+ return value;
+ }
+ }
+
+}
diff --git a/src/LinkedStackTestClient.java b/src/LinkedStackTestClient.java
new file mode 100644
index 0000000..8cf5428
--- /dev/null
+++ b/src/LinkedStackTestClient.java
@@ -0,0 +1,26 @@
+import java.util.Scanner;
+
+/**
+ * LinkedStackTestClient for term-project assignment 2 to implement and test a Stack ADT class, LinkedStack.
+ *
+ * @author Rob Smith
+ * @version 1.0
+ */
+public class LinkedStackTestClient {
+
+ public static void main(String[] args) {
+
+ Stack stringStack = new LinkedStack<>();
+ Scanner in = new Scanner("to be or not to - be - - that - - - is");
+
+ while (in.hasNext()) {
+ String item = in.next();
+ if (!item.equals("-")) {
+ stringStack.push(item);
+ } else if (!stringStack.isEmpty()) {
+ System.out.print(stringStack.pop() + " ");
+ }
+ }
+ System.out.println("(" + stringStack.size() + " left on the stack" + ")");
+ }
+}
diff --git a/src/Main.java b/src/Main.java
index 8f8f984..49a1c3f 100644
--- a/src/Main.java
+++ b/src/Main.java
@@ -1,3 +1,9 @@
+/**
+ * Implementation of the LinkedList and ArrayList using the List interface. This is my term assignment 1.
+ *
+ * @author Rob Smith
+ * "
+ */
//TIP To Run code, press or
// click the icon in the gutter.
public class Main {
@@ -5,6 +11,6 @@ public static void main(String[] args) {
//TIP Press with your caret at the highlighted text
// to see how IntelliJ IDEA suggests fixing it.
System.out.println("Hello and welcome!");
-
+ System.out.println("This was a good project, I learned a lot.");
}
-}
\ No newline at end of file
+}
diff --git a/src/QueueTestClient.java b/src/QueueTestClient.java
new file mode 100644
index 0000000..6483531
--- /dev/null
+++ b/src/QueueTestClient.java
@@ -0,0 +1,26 @@
+import java.util.Scanner;
+
+/**
+ * Queue test client for term-project assignment 2 to implement and test a Stack ADT class, LinkedQueue.
+ *
+ * @author Rob Smith
+ * @version 1.0
+ */
+public class QueueTestClient {
+
+ public static void main(String[] args) {
+
+ Queue stringQueue = new LinkedQueue<>();
+ Scanner in = new Scanner("to be or not to - be - - that - - - is");
+
+ while (in.hasNext()) {
+ String item = in.next();
+ if (!item.equals("-")) {
+ stringQueue.enqueue(item);
+ } else if (!stringQueue.isEmpty()) {
+ System.out.print(stringQueue.dequeue() + " ");
+ }
+ }
+ System.out.println("(" + stringQueue.size() + " left on the stack" + ")");
+ }
+}
diff --git a/src/ResizingArrayStack.java b/src/ResizingArrayStack.java
new file mode 100644
index 0000000..4004e01
--- /dev/null
+++ b/src/ResizingArrayStack.java
@@ -0,0 +1,127 @@
+import java.util.Iterator;
+
+/**
+ * Stack (LIFO: last-in, first-out) API for resizing array implementation
+ *
+ * @param class / data type of the items in the Stack
+ * @author Rob Smith
+ * @version 1.0
+ */
+public class ResizingArrayStack implements Stack {
+ private E[] buffer;
+ private int size;
+
+ /**
+ * Constructor for the ResizingArrayStack class.
+ */
+ public ResizingArrayStack() {
+ this.size = 0;
+ buffer = (E[]) new Object[10];
+ }
+
+ private void resize(int max) {
+ E[] temp = (E[]) new Object[max];
+ if (size >= 0) {
+ System.arraycopy(buffer, 0, temp, 0, size);
+ }
+ buffer = temp;
+ }
+
+ /**
+ * Add an item to the stack.
+ * This method is a O(1) operation if a resize is not triggered because it doesn't loop through the array.
+ * If resize is triggered it becomes O(n) because resize loops through the entire array to size it up.
+ *
+ * @param item the item to be added
+ */
+ @Override
+ public void push(E item) {
+ if (size == buffer.length) {
+ resize(2 * buffer.length);
+ }
+ buffer[size] = item;
+ size++;
+ }
+
+ /**
+ * Removes the most recently added item from the stack.
+ * Runtime for this method is O(1) if a resize is not triggered because it doesn't loop through the array.
+ * If resize is triggered it becomes O(n) because resize loops through the entire array.
+ *
+ * @return the item that was removed
+ */
+ @Override
+ public E pop() {
+ E value = buffer[size - 1];
+ buffer[size - 1] = null;
+ if (size == buffer.length / 4) {
+ resize(buffer.length / 2);
+ }
+ size--;
+ return value;
+ }
+
+ /**
+ * Returns the item at the top of the stack.
+ * Does not modify the stack or the item at the top.
+ * The runtime for this method is O(1) because there is no looping, it simply retrieves an item.
+ *
+ * @return item at the top of the stack.
+ */
+ @Override
+ public E peek() {
+ return buffer[size - 1];
+ }
+
+ /**
+ * Checks to see if the stack is empty.
+ * Runtime of O(1), single check of variable with no looping.
+ *
+ * @return true if the stack is empty, false otherwise
+ */
+ @Override
+ public boolean isEmpty() {
+ return size == 0;
+ }
+
+ /**
+ * Returns a count of the number of items in the stack.
+ * Runtime of O(1), single check of variable with no looping.
+ *
+ * @return the number of items in the stack
+ */
+ @Override
+ public int size() {
+ return size;
+ }
+
+ /**
+ * Returns an iterator over elements of type {@code T}.
+ *
+ * @return an Iterator.
+ */
+ @Override
+ public Iterator iterator() {
+ return new ReverseArrayIterator();
+ }
+
+ private class ReverseArrayIterator implements Iterator {
+ private int iterSize = size;
+
+ public boolean hasNext() {
+ return iterSize > 0;
+ }
+
+ public E next() {
+ E value = buffer[iterSize - 1];
+ iterSize--;
+ return value;
+ }
+
+ /**
+ * Unused method in this implementation.
+ */
+ public void remove() {
+ }
+ }
+}
diff --git a/src/StackTestClient.java b/src/StackTestClient.java
new file mode 100644
index 0000000..421baae
--- /dev/null
+++ b/src/StackTestClient.java
@@ -0,0 +1,25 @@
+import java.util.Scanner;
+
+/**
+ * StackTestClient for term-project assignment 2 to implement and test a Stack ADT class, ResizingArrayStack.
+ *
+ * @author Rob Smith
+ * @version 1.0
+ */
+public class StackTestClient {
+ public static void main(String[] args) {
+
+ Stack stringStack = new ResizingArrayStack<>();
+ Scanner in = new Scanner("to be or not to - be - - that - - - is");
+
+ while (in.hasNext()) {
+ String item = in.next();
+ if (!item.equals("-")) {
+ stringStack.push(item);
+ } else if (!stringStack.isEmpty()) {
+ System.out.print(stringStack.pop() + " ");
+ }
+ }
+ System.out.println("(" + stringStack.size() + " left on the stack" + ")");
+ }
+}
diff --git a/src/Stats.java b/src/Stats.java
new file mode 100644
index 0000000..d79cfb8
--- /dev/null
+++ b/src/Stats.java
@@ -0,0 +1,33 @@
+import java.util.Scanner;
+
+/**
+ * LinkedBag test client for term-project assignment 2 to implement and test a Stack ADT class, LinkedBag.
+ *
+ * @author Rob Smith
+ * @version 1.0
+ */
+public class Stats {
+ public static void main(String[] args) {
+
+ Bag numbers = new LinkedBag<>();
+ Scanner in = new Scanner("100 99 101 120 98 107 109 81 101 90");
+ while (in.hasNextDouble()) {
+ numbers.add(in.nextDouble());
+ }
+ double sum = 0.0;
+ for (double x : numbers) {
+ sum += x;
+ }
+ double mean = sum / numbers.size();
+
+ sum = 0.0;
+ for (double x : numbers) {
+ sum += (x - mean) * (x - mean);
+ }
+ double std = Math.sqrt(sum / (numbers.size() - 1));
+
+ System.out.printf("Mean: %.2f\n", mean);
+ System.out.printf("Std dev: %.2f\n", std);
+ }
+
+}