From ff28fcb09946a000c9efd8f81e069c90234dde89 Mon Sep 17 00:00:00 2001 From: Matti Date: Sat, 27 Apr 2024 23:34:56 +0200 Subject: [PATCH] Add 1 5 (Queue) --- .idea/misc.xml | 1 - README.md | 12 +++++++- src/part1/aufg5/Anwendung.java | 43 ++++++++++++++++++++++++++++ src/part1/aufg5/queue.java | 50 +++++++++++++++++++++++++++++++++ src/part1/aufg6/LinkedList.java | 28 +++++++++++++++--- 5 files changed, 128 insertions(+), 6 deletions(-) create mode 100644 src/part1/aufg5/Anwendung.java create mode 100644 src/part1/aufg5/queue.java diff --git a/.idea/misc.xml b/.idea/misc.xml index 6f29fee..5af9c98 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/README.md b/README.md index 5f70c32..690ad31 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,14 @@ ## Vorhandene Aufgaben: - Vorlesung 1 - Aufgabe 4 (ADT zum Verwalten von Produkten) - - Aufgabe 6 (Linked List erweitern) \ No newline at end of file + - Aufgabe 5 (Queue Implementieren) + - Aufgabe 6 (Linked List erweitern) + +## Fehlende (Teil-)Aufgaben +- VL 1 Aufgabe 5 +
Implementierung durch Verwendung einer List anstelle des Arrays +- Vl 1 Aufgabe 6 +
"Remove" Methode hinzufügen + +## Fragen zur Richtigkeit +- VL1 Aufg. 6 "printALL" Methode
ca. Zeile 35 \ No newline at end of file diff --git a/src/part1/aufg5/Anwendung.java b/src/part1/aufg5/Anwendung.java new file mode 100644 index 0000000..ba15353 --- /dev/null +++ b/src/part1/aufg5/Anwendung.java @@ -0,0 +1,43 @@ +package part1.aufg5; + +/** + * Aufgabe 5 + * Implementieren Sie eine generische Schlange in Java! + * Verwenden Sie dabei zunächst ein Feld mit der (Anfangs-)Dimension + * 100 als innere Datenstruktur! + * Speichern und Entnehmen Sie Objekte einer selbstdefinierten Klasse! + * Ändern Sie nun die innere Struktur in eine Liste um! + */ +public class Anwendung { + public static void main(String[] args){ + + queue schlenglein = new queue(); + + + schlenglein.add("1"); + schlenglein.add("2"); + schlenglein.add("3"); + schlenglein.add("4"); + schlenglein.add("5"); + + schlenglein.print(); + + schlenglein.remove(); + + schlenglein.print(); + + schlenglein.add("6"); + + schlenglein.print(); + + schlenglein.remove(); + schlenglein.remove(); + schlenglein.remove(); + schlenglein.remove(); + schlenglein.remove(); + + schlenglein.print(); + + schlenglein.remove(); + } +} diff --git a/src/part1/aufg5/queue.java b/src/part1/aufg5/queue.java new file mode 100644 index 0000000..5b5f1f6 --- /dev/null +++ b/src/part1/aufg5/queue.java @@ -0,0 +1,50 @@ +package part1.aufg5; + +/* + Aufgabe 5 +Implementieren Sie eine generische Schlange in Java! +Verwenden Sie dabei zunächst ein Feld mit der (Anfangs-)Dimension +100 als innere Datenstruktur! +Speichern und Entnehmen Sie Objekte einer selbstdefinierten Klasse! +Ändern Sie nun die innere Struktur in eine Liste um! + */ + + +public class queue { + private int ARRAY_SIZE = 100; + private String[] array = new String[ARRAY_SIZE]; + private int firstFreeIndex = 0; + + public void add(String s) { + if (firstFreeIndex == ARRAY_SIZE) { + System.out.println("Unable to add into List of max length!"); + } else { + array[firstFreeIndex] = s; + firstFreeIndex++; + } + } + + public void remove() { + if (firstFreeIndex == 0) { + System.out.println("Unable to remove Element from empty List!"); + } else { + for (int i = 0; i < firstFreeIndex; i++) { + array[i] = array[i + 1]; + } + + firstFreeIndex--; + } + } + + public void print() { + if (firstFreeIndex == 0) { + System.out.println("Unable to print empty List!!"); + + } else { + for (int i = 0; i < (firstFreeIndex - 1); i++) { + System.out.print(array[i] + " ; "); + } + System.out.println(array[firstFreeIndex - 1]); + } + } +} diff --git a/src/part1/aufg6/LinkedList.java b/src/part1/aufg6/LinkedList.java index dd0d2bf..2d771aa 100644 --- a/src/part1/aufg6/LinkedList.java +++ b/src/part1/aufg6/LinkedList.java @@ -32,14 +32,34 @@ public class LinkedList{ } } - public void printALL(){ - System.out.println(anchor); + public void printALL(){ // correct??? + Cell next = anchor.next; - Cell next = anchor.next; + System.out.println(anchor.data); while (next != null){ - System.out.println(next); + System.out.println(next.data); next = next.next; } } + + public void deleteFirst(T value){ + Cell next = anchor.next; + + // Check Head + if (anchor.data == value){ + anchor = anchor.next; + return; + } + + // Check Tail + while (next != null){ // starts with 2nd Element of the List + if(next.data == value){ + next = next.next.next; + return; + } + + next = next.next; // must happen at end of loop + } + } }