Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hacks for checkpoint 2 #41

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
307 changes: 6 additions & 301 deletions _notebooks/2023-03-20-Que.ipynb
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"attachments": {},
"cell_type": "markdown",
Expand All @@ -31,299 +24,6 @@
"- index values: 0123456\n",
"- using object.method nota tion : (word.length());"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import java.util.Iterator;\n",
"\n",
"/**\n",
" * Queue Iterator\n",
" *\n",
" * 1. \"has a\" current reference in Queue\n",
" * 2. supports iterable required methods for next that returns a generic T Object\n",
" */\n",
"/**\n",
" * Implementation of a Double Linked List; forward and backward links point to adjacent Nodes.\n",
" *\n",
" */\n",
"\n",
" public class LinkedList<T>\n",
" {\n",
" private T data;\n",
" private LinkedList<T> prevNode, nextNode;\n",
" \n",
" /**\n",
" * Constructs a new element\n",
" *\n",
" * @param data, data of object\n",
" * @param node, previous node\n",
" */\n",
" public LinkedList(T data, LinkedList<T> node)\n",
" {\n",
" this.setData(data);\n",
" this.setPrevNode(node);\n",
" this.setNextNode(null);\n",
" }\n",
" \n",
" /**\n",
" * Clone an object,\n",
" *\n",
" * @param node object to clone\n",
" */\n",
" public LinkedList(LinkedList<T> node)\n",
" {\n",
" this.setData(node.data);\n",
" this.setPrevNode(node.prevNode);\n",
" this.setNextNode(node.nextNode);\n",
" }\n",
" \n",
" /**\n",
" * Setter for T data in DoubleLinkedNode object\n",
" *\n",
" * @param data, update data of object\n",
" */\n",
" public void setData(T data)\n",
" {\n",
" this.data = data;\n",
" }\n",
" \n",
" /**\n",
" * Returns T data for this element\n",
" *\n",
" * @return data associated with object\n",
" */\n",
" public T getData()\n",
" {\n",
" return this.data;\n",
" }\n",
" \n",
" /**\n",
" * Setter for prevNode in DoubleLinkedNode object\n",
" *\n",
" * @param node, prevNode to current Object\n",
" */\n",
" public void setPrevNode(LinkedList<T> node)\n",
" {\n",
" this.prevNode = node;\n",
" }\n",
" \n",
" /**\n",
" * Setter for nextNode in DoubleLinkedNode object\n",
" *\n",
" * @param node, nextNode to current Object\n",
" */\n",
" public void setNextNode(LinkedList<T> node)\n",
" {\n",
" this.nextNode = node;\n",
" }\n",
" \n",
" \n",
" /**\n",
" * Returns reference to previous object in list\n",
" *\n",
" * @return the previous object in the list\n",
" */\n",
" public LinkedList<T> getPrevious()\n",
" {\n",
" return this.prevNode;\n",
" }\n",
" \n",
" /**\n",
" * Returns reference to next object in list\n",
" *\n",
" * @return the next object in the list\n",
" */\n",
" public LinkedList<T> getNext()\n",
" {\n",
" return this.nextNode;\n",
" }\n",
" \n",
" }\n",
"\n",
" class QueueIterator<T> implements Iterator<T> {\n",
" LinkedList<T> current; // current element in iteration\n",
"\n",
" // QueueIterator is pointed to the head of the list for iteration\n",
" public QueueIterator(LinkedList<T> head) {\n",
" current = head;\n",
" }\n",
"\n",
" // hasNext informs if next element exists\n",
" public boolean hasNext() {\n",
" return current != null;\n",
" }\n",
"\n",
" // next returns data object and advances to next position in queue\n",
" public T next() {\n",
" T data = current.getData();\n",
" current = current.getNext();\n",
" return data;\n",
" }\n",
"}\n",
"\n",
"/**\n",
" * Queue: custom implementation\n",
" * @author John Mortensen\n",
" *\n",
" * 1. Uses custom LinkedList of Generic type T\n",
" * 2. Implements Iterable\n",
" * 3. \"has a\" LinkedList for head and tail\n",
" */\n",
"public class Queue<T> implements Iterable<T> {\n",
" LinkedList<T> head = null, tail = null;\n",
"\n",
" /**\n",
" * Add a new object at the end of the Queue,\n",
" *\n",
" * @param data, is the data to be inserted in the Queue.\n",
" */\n",
" public void add(T data) {\n",
" // add new object to end of Queue\n",
" LinkedList<T> tail = new LinkedList<>(data, null);\n",
"\n",
" if (this.head == null) // initial condition\n",
" this.head = this.tail = tail;\n",
" else { // nodes in queue\n",
" this.tail.setNextNode(tail); // current tail points to new tail\n",
" this.tail = tail; // update tail\n",
" }\n",
" }\n",
"\n",
" /**\n",
" * Returns the data of head.\n",
" *\n",
" * @return data, the dequeued data\n",
" */\n",
" public T delete() {\n",
" T data = this.peek();\n",
" if (this.tail != null) { // initial condition\n",
" this.head = this.head.getNext(); // current tail points to new tail\n",
" if (this.head != null) {\n",
" this.head.setPrevNode(tail);\n",
" }\n",
" }\n",
" return data;\n",
" }\n",
"\n",
" /**\n",
" * Returns the data of head.\n",
" *\n",
" * @return this.head.getData(), the head data in Queue.\n",
" */\n",
" public T peek() {\n",
" return this.head.getData();\n",
" }\n",
"\n",
" /**\n",
" * Returns the head object.\n",
" *\n",
" * @return this.head, the head object in Queue.\n",
" */\n",
" public LinkedList<T> getHead() {\n",
" return this.head;\n",
" }\n",
"\n",
" /**\n",
" * Returns the tail object.\n",
" *\n",
" * @return this.tail, the last object in Queue\n",
" */\n",
" public LinkedList<T> getTail() {\n",
" return this.tail;\n",
" }\n",
"\n",
" /**\n",
" * Returns the iterator object.\n",
" *\n",
" * @return this, instance of object\n",
" */\n",
" public Iterator<T> iterator() {\n",
" return new QueueIterator<>(this.head);\n",
" }\n",
"}\n",
"\n",
"class QueueManager<T> {\n",
" // queue data\n",
" private final String name; // name of queue\n",
" private int count = 0; // number of objects in queue\n",
" public final Queue<T> queue = new Queue<>(); // queue object\n",
"\n",
" /**\n",
" * Queue constructor\n",
" * Title with empty queue\n",
" */\n",
" public QueueManager(String name) {\n",
" this.name = name;\n",
" }\n",
"\n",
" /**\n",
" * Queue constructor\n",
" * Title with series of Arrays of Objects\n",
" */\n",
" public QueueManager(String name, T[]... seriesOfObjects) {\n",
" this.name = name;\n",
" this.addList(seriesOfObjects);\n",
" }\n",
"\n",
" /**\n",
" * Add a list of objects to queue\n",
" */\n",
" public void addList(T[]... seriesOfObjects) { //accepts multiple generic T lists\n",
" for (T[] objects: seriesOfObjects)\n",
" for (T data : objects) {\n",
" this.queue.add(data);\n",
" this.count++;\n",
" printQueue();\n",
" }\n",
" }\n",
"\n",
" // add single object to queue\n",
" public void add(T data) {\n",
" this.queue.add(data);\n",
" this.count++;\n",
" printQueue();\n",
" }\n",
"\n",
" // delete objects from queue\n",
" public void deleteList(int count) {\n",
" for (int i = 0; i < count; i++) {\n",
" this.queue.delete();\n",
" this.count--;\n",
" printQueue();\n",
" }\n",
" }\n",
"\n",
" /**\n",
" * Print any array objects from queue\n",
" */\n",
" public void printQueue() {\n",
" System.out.println(this.name + \" count: \" + count);\n",
" System.out.print(this.name + \" data: \");\n",
" for (T data : queue)\n",
" System.out.print(data + \" \");\n",
" System.out.println();\n",
" }\n",
"}\n",
"\n",
"class QueueTester {\n",
" public static void main(String[] args) {\n",
" // create queue manager\n",
" QueueManager<Integer> qm = new QueueManager<>(\"Queue\");\n",
"\n",
" // add objects to queue\n",
" qm.addList(new Integer[]{1, 2, 3, 4, 5});\n",
"\n",
" // delete objects from queue\n",
" qm.deleteList(5);\n",
" }\n",
"}\n",
"\n",
"QueueTester.main(null);"
]
}
],
"metadata": {
Expand All @@ -333,7 +33,12 @@
"name": "java"
},
"language_info": {
"name": "java"
"codemirror_mode": "java",
"file_extension": ".jshell",
"mimetype": "text/x-java-source",
"name": "Java",
"pygments_lexer": "java",
"version": "17.0.2+8-86"
},
"orig_nbformat": 4
},
Expand Down
2 changes: 1 addition & 1 deletion _notebooks/2023-03-20-generics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@
"mimetype": "text/x-java-source",
"name": "Java",
"pygments_lexer": "java",
"version": "11.0.16+8-post-Ubuntu-0ubuntu120.04"
"version": "17.0.2+8-86"
},
"orig_nbformat": 4
},
Expand Down
Loading