Teil 1 / Aufgabe 6

This commit is contained in:
Sebastian Brosch 2024-05-21 14:33:51 +02:00
commit acb7881eb6
2 changed files with 104 additions and 0 deletions

View File

@ -0,0 +1,26 @@
package T01.Aufgabe06;
/**
* Teil 1 / Aufgabe 6
*
* @author Sebastian Brosch
*/
public class Aufgabe06 {
public static void main(String[] args) {
VerketteteListe<Integer> list = new VerketteteListe<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.printAll();
System.out.println("--------------------");
list.delete(1);
list.printAll();
System.out.println("--------------------");
list.delete(3);
list.printAll();
System.out.println("--------------------");
list.delete(4);
list.printAll();
}
}

View File

@ -0,0 +1,78 @@
package T01.Aufgabe06;
/**
* Class to represent a VerketteteListe.
*/
public class VerketteteListe<T> {
/**
* Class to represent a cell of VerketteteListe.
*/
private class Cell<V> {
V value;
Cell<V> next;
Cell(V value) {
this.value = value;
}
}
private Cell<T> anchor;
/**
* Method to add a value to VerketteteListe.
*
* @param value The value to add.
*/
public void add(T value) {
Cell<T> node = new Cell<T>(value);
if (this.anchor == null) {
this.anchor = node;
node.next = null;
} else {
node.next = anchor;
this.anchor = node;
}
}
/**
* Method to delete the first cell with value from VerketteteListe.
*
* @param value The value to delete the cell.
*/
void delete(T value) {
if (this.anchor == null) {
return;
}
if (this.anchor.value == value) {
this.anchor = this.anchor.next;
return;
}
Cell<T> node = this.anchor;
while (node.next != null) {
if (node.next.value == value) {
node.next = node.next.next;
return;
}
node = node.next;
}
}
/**
* Method to print all cells of VerketteteListe.
*/
void printAll() {
Cell<T> node = this.anchor;
while (node != null) {
System.out.println(node.value);
node = node.next;
}
}
}