Ubgrade 16.1.2023
parent
dfbc01fc9b
commit
f470c248c7
|
@ -1,71 +0,0 @@
|
|||
|
||||
/**
|
||||
* Beschreiben Sie hier die Klasse List.
|
||||
*
|
||||
* @author (Ihr Name)
|
||||
* @version (eine Versionsnummer oder ein Datum)
|
||||
*/
|
||||
public class LinkedList2<T>
|
||||
{
|
||||
public List<T> first;
|
||||
|
||||
public LinkedList2(){}
|
||||
public void einfuegen (T neu) {
|
||||
List<T> n = new List<T>(neu);//Neue Node mit Zahl "neu " anlegen
|
||||
|
||||
//Überprüfe ob die liste leer ist
|
||||
if(first == null){
|
||||
//setze neue node als erster Eintrag
|
||||
first = n;
|
||||
}
|
||||
else {
|
||||
List<T> current = first;
|
||||
|
||||
while(current.next != null){
|
||||
current = current.next;
|
||||
}
|
||||
current.setNext(n);
|
||||
}
|
||||
|
||||
//current ist jetzt der letzte Eintrag
|
||||
//setze neue Node als Nachfolger von bisher letztem Eintrag
|
||||
}
|
||||
public boolean isEmpty(){
|
||||
if (first == null){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public int laenge(){
|
||||
List current = first;
|
||||
int laenge = 0;
|
||||
while(current != null){
|
||||
current = current.next;
|
||||
laenge++;
|
||||
}
|
||||
return laenge;
|
||||
}
|
||||
|
||||
|
||||
public T getNteZahl(int n){
|
||||
List<T> current = first;
|
||||
for(int i = 0; i<n; i++){
|
||||
current = current.next;
|
||||
}
|
||||
return current.zahl;
|
||||
}
|
||||
|
||||
public void loesche(int n){
|
||||
List<T> current = first;
|
||||
|
||||
//gehe an den Vorgänger des zu löschenden Eintrags
|
||||
for(int i=0; i<n-1; i++){
|
||||
current = current.next;
|
||||
}
|
||||
//setze Übernächsten Eintrag als Nachfolger
|
||||
current.next = current.next.next;
|
||||
|
||||
}
|
||||
|
||||
}
|
82
List.java
82
List.java
|
@ -7,19 +7,81 @@
|
|||
*/
|
||||
public class List<T>
|
||||
{
|
||||
|
||||
public T zahl;
|
||||
public Node<T> first;
|
||||
public List(){
|
||||
}
|
||||
|
||||
public List next;
|
||||
public void einfuegen (T neu) {
|
||||
Node<T> n = new Node<T>(neu);//Neue Node mit Zahl "neu " anlegen
|
||||
|
||||
public List(T g){
|
||||
zahl = g;
|
||||
}
|
||||
public void setNext(List h){
|
||||
next = h;
|
||||
//Überprüfe ob die liste leer ist
|
||||
if(first == null){
|
||||
//setze neue node als erster Eintrag
|
||||
first = n;
|
||||
}
|
||||
else {
|
||||
Node<T> current = first;
|
||||
|
||||
while(current.next != null){
|
||||
current = current.next;
|
||||
}
|
||||
current.setNext(n);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//current ist jetzt der letzte Eintrag
|
||||
//setze neue Node als Nachfolger von bisher letztem Eintrag
|
||||
}
|
||||
|
||||
public int laenge(){
|
||||
Node current = first;
|
||||
int laenge = 0;
|
||||
while(current != null){
|
||||
current = current.next;
|
||||
laenge++;
|
||||
}
|
||||
return laenge;
|
||||
|
||||
}
|
||||
|
||||
public T getNteZahl(int n){
|
||||
Node<T> current = first;
|
||||
for(int i = 0; i<n; i++){
|
||||
current = current.next;
|
||||
}
|
||||
return current.zahl;
|
||||
}
|
||||
|
||||
public void loesche(int n){
|
||||
Node<T> current = first;
|
||||
|
||||
//gehe an den Vorgänger des zu löschenden Eintrags
|
||||
for(int i=0; i<n-1; i++){
|
||||
current = current.next;
|
||||
}
|
||||
//setze Übernächsten Eintrag als Nachfolger
|
||||
current.next = current.next.next;
|
||||
|
||||
}
|
||||
|
||||
public void hinzufügen(int n, T wert){
|
||||
|
||||
Node<T> neu = new Node<T>(wert);
|
||||
if(n==0){
|
||||
//setze Nachfolger auf bisherigen Stand
|
||||
neu.setNext(first);
|
||||
//setze neuen Start auf neuen
|
||||
first = neu;
|
||||
}else{
|
||||
Node<T> current = first;
|
||||
//gehe an den Vorgänger des zu löschenden Eintrags
|
||||
for(int i=0; i<n-1; i++){
|
||||
current = current.next;
|
||||
}
|
||||
//setze Übernächsten Eintrag als Nachfolger
|
||||
current.next = current.next.next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ public class Node<T>
|
|||
|
||||
public T zahl;
|
||||
|
||||
public Node next;
|
||||
public Node<T> next;
|
||||
|
||||
public Node(T z){
|
||||
zahl = z;
|
||||
|
|
28
test.java
28
test.java
|
@ -11,7 +11,7 @@ public class test
|
|||
Node<String> test = new Node<String>("Hallo");
|
||||
Node<Integer> test2 = new Node<Integer>(5);
|
||||
|
||||
LinkedList liste = new LinkedList();
|
||||
List liste = new List<String>();
|
||||
liste.einfuegen(5);
|
||||
liste.einfuegen(6);
|
||||
liste.einfuegen(8);
|
||||
|
@ -19,7 +19,7 @@ public class test
|
|||
liste.einfuegen(7);
|
||||
liste.einfuegen(9);
|
||||
liste.loesche(2);
|
||||
liste.hinzufügen(8,"wirklich");
|
||||
liste.hinzufügen(4,"wirklich");
|
||||
Node current = liste.first;
|
||||
|
||||
while(current != null){
|
||||
|
@ -32,29 +32,5 @@ public class test
|
|||
|
||||
|
||||
}
|
||||
public static void test2(){
|
||||
List<String> test = new List<String>("Hallo");
|
||||
List<Integer> test2 = new List<Integer>(5);
|
||||
|
||||
LinkedList2 liste = new LinkedList2();
|
||||
liste.einfuegen(5);
|
||||
liste.einfuegen(6);
|
||||
liste.einfuegen(8);
|
||||
liste.einfuegen(3);
|
||||
liste.einfuegen(7);
|
||||
liste.einfuegen(9);
|
||||
liste.loesche(2);
|
||||
|
||||
List current = liste.first;
|
||||
|
||||
while(current != null){
|
||||
System.out.println( current.zahl );
|
||||
current = current.next;
|
||||
}
|
||||
|
||||
System.out.println("Die Liste hat "+liste.laenge()+" Einträge. ");
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue