From 1955c8b659aeeb735369f65436370e43ab643134 Mon Sep 17 00:00:00 2001 From: Frank Date: Fri, 29 Aug 2025 17:54:13 -0600 Subject: [PATCH] Adding in more work with Data Structures --- src/Chapter9Assignment.java | 58 +++++++++++++++++++ src/DoublyLinkedList.java | 103 ++++++++++++++++++++++++++++++++++ src/JavaDeQueue.java | 33 +++++++++++ src/JavaMapAndDictionary.java | 35 ++++++++++++ src/JavaPriorityQueue.java | 24 ++++++++ src/JavaQueue.java | 24 ++++++++ src/JavaSet.java | 46 +++++++++++++++ src/Node.java | 1 + 8 files changed, 324 insertions(+) create mode 100644 src/Chapter9Assignment.java create mode 100644 src/DoublyLinkedList.java create mode 100644 src/JavaDeQueue.java create mode 100644 src/JavaMapAndDictionary.java create mode 100644 src/JavaPriorityQueue.java create mode 100644 src/JavaQueue.java create mode 100644 src/JavaSet.java diff --git a/src/Chapter9Assignment.java b/src/Chapter9Assignment.java new file mode 100644 index 0000000..03b9e93 --- /dev/null +++ b/src/Chapter9Assignment.java @@ -0,0 +1,58 @@ +import java.util.Deque; +import java.util.LinkedList; +import java.util.Scanner; +import java.util.Stack; + +class Queue { + LinkedList queue = new LinkedList<>(); + + public void insert(String data) { + queue.add(data); + } + + public String retrieve() { + String value = queue.getFirst(); + queue.removeFirst(); + return value; + } + + public boolean isEmpty() { + return queue.isEmpty(); + } +} + +public class Chapter9Assignment { + public static void main(String[] args) { + String[] data = { "A", "B", "C", "D", "E" }; + Queue queue = new Queue(); + Scanner scanner = new Scanner(System.in); + Stack stack = new Stack<>(); + + String item = scanner.next(); + + LinkedList list = new LinkedList<>(); + Deque dequeue = new LinkedList<>(); + + dequeue.pollFirst(); + list.addLast("test"); + + while (!item.equals(".")) { + queue.insert(item); + stack.push(item); + System.out.println("Enter Next Item: "); + item = scanner.next(); + } + + System.out.println("Retrieving Queue:"); + + while (!queue.isEmpty()) { + System.out.println(queue.retrieve()); + } + + System.out.println("<---------->"); + System.out.println("Retrieving Stack:"); + while (!stack.isEmpty()) { + System.out.println(stack.pop()); + } + } +} diff --git a/src/DoublyLinkedList.java b/src/DoublyLinkedList.java new file mode 100644 index 0000000..1aa7562 --- /dev/null +++ b/src/DoublyLinkedList.java @@ -0,0 +1,103 @@ +public class DoublyLinkedList { + + public static void main(String[] args) { + // Create the list + DoublyLinkedList list = new DoublyLinkedList(); + + // Add item to head + list.addToHead(50); + + // Add item to tail + list.addToTail(100); + + list.addToHead(25); + + // Insert 75 after 50 + list.insertNode(list.head.next, 75); + + // Print + list.printList(list.head); + } + + Node head; + + public void addToHead(int element) { + Node n = new Node(element); + + n.next = head; + n.previous = null; + + if (head != null) { + head.previous = n; + } + + head = n; + } + + public void addToTail(int element) { + Node n = new Node(element); + Node end = head; + n.next = null; + + // If the list is empty make the new node the head + if (head == null) { + n.previous = null; + head = n; + return; + } + + // Iterate until you find the last Node + while (end.next != null) { + end = end.next; + } + + // Change the next of the last Node + end.next = n; + + // Make the last Node the previous of the new Node + n.previous = end; + } + + public void insertNode(Node previous, int element) { + // Is the given Node null + if (previous == null) { + System.out.println("Cannot have previous Node of null"); + return; + } + + // Create a new Node and add data + Node n = new Node(element); + + // Make new Node.next the next of the previous + n.next = previous.next; + + // Make next of previous the new Node + previous.next = n; + + // Make previous Node as previous of new Node + n.previous = previous; + + // Change previous of new Nodes next Node + if (n.next != null) { + n.next.previous = n; + } + } + + public void printList(Node node) { + System.out.println("Going forward --> "); + Node end = null; + + while (node != null) { + System.out.print(node.element + " "); + end = node; + node = node.next; + } + System.out.println(); + System.out.println("<-- Going backward "); + + while (end != null) { + System.out.print(end.element + " "); + end = end.previous; + } + } +} diff --git a/src/JavaDeQueue.java b/src/JavaDeQueue.java new file mode 100644 index 0000000..0b4205d --- /dev/null +++ b/src/JavaDeQueue.java @@ -0,0 +1,33 @@ +import java.util.Deque; +import java.util.LinkedList; + +public class JavaDeQueue { + public static void main(String[] args) { + Deque workQueue = new LinkedList<>(); + + workQueue.add("widget 001"); + workQueue.add("widget 002"); + workQueue.add("widget 003"); + workQueue.add("widget 004"); + workQueue.add("widget 005"); + System.out.println(workQueue); + + System.out.println("Inspecting " + workQueue.peek()); + // Simulated processing of widgets + int status = 1; + if (status == 1) { + workQueue.removeFirst(); + System.out.println("Widget passed inspection; removed from dequeue."); + } else if (status == 0) { // Minor work required + Object inspectionItem = workQueue.pollFirst(); + workQueue.addLast(inspectionItem); + System.out.println("Widget moved to tail of dequeue for Major work."); + } else if (status == -1) { // Failed inspection - send to end of dequeue + Object inspectionItem = workQueue.pollFirst(); + workQueue.addLast(inspectionItem); + + } + + System.out.println(workQueue); + } +} diff --git a/src/JavaMapAndDictionary.java b/src/JavaMapAndDictionary.java new file mode 100644 index 0000000..b78addb --- /dev/null +++ b/src/JavaMapAndDictionary.java @@ -0,0 +1,35 @@ +import java.util.*; + +public class JavaMapAndDictionary { + public static void main(String[] args) { + // Map Example + double[] mapGPA = new double[] { 4.0, 3.0, 2.0, 1.0, 0.0 }; + char[] mapLetter = new char[] { 'A', 'B', 'C', 'D', 'F' }; + Map map = new HashMap(); + + for (int i = 0; i < mapGPA.length; i++) { + map.put(mapLetter[i], mapGPA[i]); // Insert key/value into map + } + + for (Character key : map.keySet()) { + System.out.println(key + " " + map.get(key)); + } + + // END: Map Example + System.out.println(); + // Dictionary Example + double[] dictGPA = new double[] { 4.0, 3.0, 2.0, 1.0, 0.0 }; + char[] dictLetter = new char[] { 'A', 'B', 'C', 'D', 'F' }; + Dictionary dictionary = new Hashtable(); + + for (int i = 0; i < dictGPA.length; i++) { + dictionary.put(dictLetter[i], dictGPA[i]); // Insert key/value into map + } + + for (Enumeration keys = dictionary.keys(); keys.hasMoreElements(); ) { + char keyVal = keys.nextElement(); + System.out.println(keyVal + " " + dictionary.get(keyVal)); + } + // END: Dictionary Example + } +} diff --git a/src/JavaPriorityQueue.java b/src/JavaPriorityQueue.java new file mode 100644 index 0000000..c60b016 --- /dev/null +++ b/src/JavaPriorityQueue.java @@ -0,0 +1,24 @@ +import java.util.PriorityQueue; + +public class JavaPriorityQueue { + public static void main(String[] args) { + PriorityQueue myPriorityQueue = new PriorityQueue(); + + myPriorityQueue.add("Agustus"); + myPriorityQueue.add("Tiberius"); + myPriorityQueue.add("Caligula"); + myPriorityQueue.add("Claudius"); + myPriorityQueue.add("Nero"); + myPriorityQueue.add("Galba"); + myPriorityQueue.add("Otho"); + myPriorityQueue.add("Aulus Vitellius"); + myPriorityQueue.add("Vespasian"); + myPriorityQueue.add("Titus"); + myPriorityQueue.add("Domitian"); + myPriorityQueue.add("Nerva"); + + System.out.println(myPriorityQueue); + myPriorityQueue.remove(); + System.out.println(myPriorityQueue); + } +} diff --git a/src/JavaQueue.java b/src/JavaQueue.java new file mode 100644 index 0000000..0f15735 --- /dev/null +++ b/src/JavaQueue.java @@ -0,0 +1,24 @@ +import java.util.LinkedList; +import java.util.NoSuchElementException; +import java.util.Queue; + +public class JavaQueue { + public static void main(String[] args) { + Queue coffeeShop = new LinkedList(); + coffeeShop.add("Sally"); + coffeeShop.add("Debra"); + coffeeShop.add("Jimmy"); + coffeeShop.add("Cindy"); + System.out.println(coffeeShop.peek()); + coffeeShop.remove(); + coffeeShop.remove(); + System.out.println(coffeeShop.element()); + coffeeShop.remove(); + coffeeShop.poll(); + try { + coffeeShop.remove(); + } catch (NoSuchElementException error) { + System.out.println("Oops! It doesn't exist!"); + } + } +} diff --git a/src/JavaSet.java b/src/JavaSet.java new file mode 100644 index 0000000..7fa2515 --- /dev/null +++ b/src/JavaSet.java @@ -0,0 +1,46 @@ +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; +import java.util.TreeSet; + +public class JavaSet { + public static void main(String[] args) { + + // HashSet + Set trees = new HashSet<>(); + trees.add("Larch"); + trees.add("Pine"); + trees.add("Balsam"); + trees.add("Birch"); + trees.add("Ash"); + + for (String s : trees) { + System.out.println("Tree = " + s); + } + + trees.forEach(System.out::println); + + Iterator ir = trees.iterator(); + while (ir.hasNext()) { + System.out.println("Iterator Result = " + ir.next()); + } + + // END: HashSet + + // TreeSet + + Set setTrees = new TreeSet<>(); + setTrees.add("Pine"); + setTrees.add("Larch"); + setTrees.add("Balsam"); + setTrees.add("Birch"); + setTrees.add("Ash"); + + Iterator setIR = setTrees.iterator(); + while (setIR.hasNext()) { + System.out.println("Iterator Result = " + setIR.next()); + } + + // END: TreeSet + } +} diff --git a/src/Node.java b/src/Node.java index e7ff614..dcaa80d 100644 --- a/src/Node.java +++ b/src/Node.java @@ -1,6 +1,7 @@ public class Node { int element; Node next; + Node previous; public Node(int element) { this.element = element;