Adding in more work with Data Structures

This commit is contained in:
Frank
2025-08-29 17:54:13 -06:00
parent 6a9d55e07d
commit 1955c8b659
8 changed files with 324 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
import java.util.Deque;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Stack;
class Queue {
LinkedList<String> 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<String> stack = new Stack<>();
String item = scanner.next();
LinkedList<String> list = new LinkedList<>();
Deque<String> 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());
}
}
}

103
src/DoublyLinkedList.java Normal file
View File

@@ -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;
}
}
}

33
src/JavaDeQueue.java Normal file
View File

@@ -0,0 +1,33 @@
import java.util.Deque;
import java.util.LinkedList;
public class JavaDeQueue {
public static void main(String[] args) {
Deque<Object> 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);
}
}

View File

@@ -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<Character, Double> map = new HashMap<Character, Double>();
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<Character, Double> dictionary = new Hashtable<Character, Double>();
for (int i = 0; i < dictGPA.length; i++) {
dictionary.put(dictLetter[i], dictGPA[i]); // Insert key/value into map
}
for (Enumeration<Character> keys = dictionary.keys(); keys.hasMoreElements(); ) {
char keyVal = keys.nextElement();
System.out.println(keyVal + " " + dictionary.get(keyVal));
}
// END: Dictionary Example
}
}

View File

@@ -0,0 +1,24 @@
import java.util.PriorityQueue;
public class JavaPriorityQueue {
public static void main(String[] args) {
PriorityQueue<String> myPriorityQueue = new PriorityQueue<String>();
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);
}
}

24
src/JavaQueue.java Normal file
View File

@@ -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<String> coffeeShop = new LinkedList<String>();
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!");
}
}
}

46
src/JavaSet.java Normal file
View File

@@ -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<String> 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<String> ir = trees.iterator();
while (ir.hasNext()) {
System.out.println("Iterator Result = " + ir.next());
}
// END: HashSet
// TreeSet
Set<String> setTrees = new TreeSet<>();
setTrees.add("Pine");
setTrees.add("Larch");
setTrees.add("Balsam");
setTrees.add("Birch");
setTrees.add("Ash");
Iterator<String> setIR = setTrees.iterator();
while (setIR.hasNext()) {
System.out.println("Iterator Result = " + setIR.next());
}
// END: TreeSet
}
}

View File

@@ -1,6 +1,7 @@
public class Node {
int element;
Node next;
Node previous;
public Node(int element) {
this.element = element;