From 7eb050d9d318016477a3853d13c6d534b39e1e41 Mon Sep 17 00:00:00 2001 From: dfvcjal <@> Date: Mon, 18 Dec 2023 16:41:22 +0100 Subject: [PATCH] verbesserung durch jessi --- List.java | 80 ++++++++++++++++++++++++++++++++++++++++--------------- Node.java | 26 +++++++++--------- 2 files changed, 73 insertions(+), 33 deletions(-) diff --git a/List.java b/List.java index 24c6586..8e4e7ac 100644 --- a/List.java +++ b/List.java @@ -5,59 +5,97 @@ * @author (Ihr Name) * @version (eine Versionsnummer oder ein Datum) */ + public class List { private Node first; - public List(){ //leere liste soll erzeugt werden, wert wird zugedingst + + public List() { first = null; } - public boolean isEmpty(){ - if (first == null)return true; + + public boolean isEmpty() { + if (first == null) return true; return false; } - public int size(){ + + public int size() { Node current = first; int count = 0; - while (current != null){ + + while (current != null) { count++; current = current.next; } + return count; } - public T get(int n){ + + public T get(int n) { Node current = first; - for(int i = 0; i neu = new Node(); neu.wert = val; - if (first ==null){ + if (first == null) { first = neu; - }else{ + } else { Node current = first; - while (current.next !=null){ + while (current.next != null) { current = current.next; } current.next = neu; } } - public void add(int n, T val){ - Node added - } - public boolean contains(T val){ - Node current = first; - while (current != null){ - if (current.wert.equals(val)) return true; + + 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 } - return false; - } - public T remove(int n){ + + Node neu = new Node (); + 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 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 current = first; + + while (current != null) { + if (current.wert.equals(val)) return true; + } + + return false; + } + + public T remove(int n) { + return null; //Platzhalter + } } + diff --git a/Node.java b/Node.java index 974f2fd..1f71993 100644 --- a/Node.java +++ b/Node.java @@ -5,17 +5,19 @@ * @author (Ihr Name) * @version (eine Versionsnummer oder ein Datum) */ -public class Node + +public class Node //T für Typ und die Variable die wir hsben möchten (also int,string,...) { - public T wert; - public Node next; - public Node(T w){ - this.wert = w; - } - public void setWert(T w){ - this.wert = w; - } - public void setNext(Node n){ - this.next = n; - } + public T wert; + public Node next; + + + public void setWert(T w){ //statt int T damit es allgemeiner ist + this.wert = w; + } + + + public void setNext(Node n){ + this.next = n; + } }