add ist neu und irgendwas anderes noch + mit Kommentaren yay
parent
87aca7f523
commit
f0288f959f
|
@ -8,9 +8,7 @@
|
|||
public class LinkedList<T> // T muss immer in der Klasse gennant werden
|
||||
{
|
||||
private Node<T> start; //Node<T> braucht auvh Datentyp T
|
||||
public LinkedList(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void einfuegen(T zahl){
|
||||
// Lege einen neuen Knoten an mit der Zahl
|
||||
|
|
|
@ -0,0 +1,99 @@
|
|||
|
||||
/**
|
||||
* Beschreiben Sie hier die Klasse List_T_.
|
||||
*
|
||||
* @author (Ihr Name)
|
||||
* @version (eine Versionsnummer oder ein Datum)
|
||||
*/
|
||||
public class List<T>
|
||||
{
|
||||
private Node<T> first;
|
||||
|
||||
public List() {
|
||||
first = null;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
if (first == null) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
Node<T> current = first;
|
||||
int count = 0;
|
||||
|
||||
while (current != null) {
|
||||
count++;
|
||||
current = current.next;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
public T get(int n) {
|
||||
Node<T> current = first;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (current == null) return null;
|
||||
current = current.next;
|
||||
}
|
||||
|
||||
if (current == null) return null;
|
||||
return current.wert;
|
||||
}
|
||||
|
||||
public void add(T val) {
|
||||
Node<T> neu = new Node<T>();
|
||||
neu.wert = val;
|
||||
|
||||
if (first == null) {
|
||||
first = neu;
|
||||
} else {
|
||||
Node<T> current = first;
|
||||
while (current.next != null) {
|
||||
current = current.next;
|
||||
}
|
||||
current.next = neu;
|
||||
}
|
||||
}
|
||||
|
||||
public void add(int n, T val) {
|
||||
if (n>= size()){ //überprüfung am anfang wenn n >= ist wie die länge, also zahlen die zu lang sind
|
||||
add(val); //wenn n zu groß dann hänge am ende an...
|
||||
return; // und beende die Methode
|
||||
}
|
||||
|
||||
Node<T> neu = new Node <T>();
|
||||
neu.wert = val;
|
||||
|
||||
if (n == 0){ // Wenn Nachfolger an stelle 0 einfügen möchte dann
|
||||
neu.next = first;// Nachfolger auf bisher ersten(first) setzten
|
||||
first = neu;// Neuer Eintrag ist der neue Erste(first)
|
||||
return; //beenden
|
||||
}
|
||||
|
||||
Node<T> current = first;
|
||||
|
||||
for (int i = 0; i < n-1; i++){
|
||||
current = current.next;
|
||||
}
|
||||
|
||||
neu.next =current.next;
|
||||
current.next = neu;
|
||||
|
||||
}
|
||||
|
||||
public boolean contains(T val) {
|
||||
Node<T> current = first;
|
||||
|
||||
while (current != null) {
|
||||
if (current.wert.equals(val)) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public T remove(int n) {
|
||||
return null; //Platzhalter
|
||||
}
|
||||
}
|
|
@ -5,15 +5,17 @@
|
|||
* @author (Ihr Name)
|
||||
* @version (eine Versionsnummer oder ein Datum)
|
||||
*/
|
||||
public class Node <T> //T für Typ und die Variable die wir hsben möchten (also int,string,...)
|
||||
public class Node<T> //T für Typ und die Variable die wir hsben möchten (also int,string,...)
|
||||
{
|
||||
public T wert;
|
||||
public Node next;
|
||||
public Node<T> next;
|
||||
|
||||
|
||||
public void setWert(T w){ //statt int T damit es allgemeiner ist
|
||||
this.wert = w;
|
||||
}
|
||||
|
||||
|
||||
public void setNext(Node n){
|
||||
this.next = n;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
|
||||
/**
|
||||
* Beschreiben Sie hier die Klasse NodeT2.
|
||||
*
|
||||
* @author (Ihr Name)
|
||||
* @version (eine Versionsnummer oder ein Datum)
|
||||
*/
|
||||
public class Node2<T>
|
||||
{
|
||||
public class Node<T>
|
||||
{
|
||||
public T wert;
|
||||
public Node<T> next;
|
||||
}
|
||||
}
|
|
@ -25,7 +25,7 @@ public class Test2
|
|||
|
||||
System.out.println(l.getNteZahl(23)); // gibt 0 aus weil Liste nicht so lang ist
|
||||
|
||||
LinkedList<String> 12= new LinkedList<Strings>();
|
||||
LinkedList<String> 12 = new LinkedList<Strings>();
|
||||
12.einfuegen("Hallo");
|
||||
12.einfuegen("Welt");
|
||||
|
||||
|
|
Loading…
Reference in New Issue