Initial sharing of project
commit
e4e0712be5
|
@ -0,0 +1,90 @@
|
||||||
|
public class LinkedList<T>
|
||||||
|
{
|
||||||
|
public Node<T> first;
|
||||||
|
|
||||||
|
public LinkedList() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void einfuegen(T neu) {
|
||||||
|
Node n = new Node<T>(neu); // Neue Node mit Zahl "neu" anlegen
|
||||||
|
|
||||||
|
// Überprüfe, ob Liste leer
|
||||||
|
if (first == null) {
|
||||||
|
// setze neue Node als ersten Eintrag
|
||||||
|
first = n;
|
||||||
|
} else {
|
||||||
|
Node<T> current = first;
|
||||||
|
|
||||||
|
// gehe zum letzten Eintrag
|
||||||
|
while (current.next != null) {
|
||||||
|
current = current.next;
|
||||||
|
}
|
||||||
|
|
||||||
|
// current ist jetzt der letzte Eintrag
|
||||||
|
// setze neue Node als Nachfolger von bisher letzem Eintrag
|
||||||
|
current.setNext(n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int laenge() {
|
||||||
|
Node<T> current = first;
|
||||||
|
int laenge = 0;
|
||||||
|
|
||||||
|
while (current != null) {
|
||||||
|
current = current.next;
|
||||||
|
laenge++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return laenge;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T getNtenWert(int n) {
|
||||||
|
Node<T> current = first;
|
||||||
|
|
||||||
|
for (int i=0; i<n; i++) {
|
||||||
|
current = current.next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return current.wert;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void loesche(int n) {
|
||||||
|
if (n == 0) {
|
||||||
|
first = first.next;
|
||||||
|
} 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void einfuegen(int n, T wert) {
|
||||||
|
// Neue Node anlegen
|
||||||
|
Node<T> neu = new Node<T>(wert);
|
||||||
|
|
||||||
|
if (n == 0) {
|
||||||
|
// Setze Nachfolger auf bisherigen Start
|
||||||
|
neu.setNext(first);
|
||||||
|
// Setze neuen Start auf neues Element
|
||||||
|
first = neu;
|
||||||
|
} else {
|
||||||
|
Node<T> current = first;
|
||||||
|
// gehe an den Vorgänger
|
||||||
|
for (int i=0; i<n-1; i++) {
|
||||||
|
current = current.next;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ändere neu.next auf current.next
|
||||||
|
neu.setNext(current.next);
|
||||||
|
|
||||||
|
// ändere current.next auf neu
|
||||||
|
current.setNext(neu);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,119 @@
|
||||||
|
public class List<T>
|
||||||
|
{
|
||||||
|
public Node<T> first;
|
||||||
|
|
||||||
|
public List() {}
|
||||||
|
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return first == null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int size() {
|
||||||
|
Node<T> current = first;
|
||||||
|
int laenge = 0;
|
||||||
|
|
||||||
|
while (current != null) {
|
||||||
|
current = current.next;
|
||||||
|
laenge++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return laenge;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T get(int n) {
|
||||||
|
Node<T> current = first;
|
||||||
|
|
||||||
|
if (n >= size()) return null;
|
||||||
|
|
||||||
|
for (int i=0; i<n; i++) {
|
||||||
|
current = current.next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return current.wert;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void add(T neu) {
|
||||||
|
Node n = new Node<T>(neu); // Neue Node mit Zahl "neu" anlegen
|
||||||
|
|
||||||
|
// Überprüfe, ob Liste leer
|
||||||
|
if (first == null) {
|
||||||
|
// setze neue Node als ersten Eintrag
|
||||||
|
first = n;
|
||||||
|
} else {
|
||||||
|
Node<T> current = first;
|
||||||
|
|
||||||
|
// gehe zum letzten Eintrag
|
||||||
|
while (current.next != null) {
|
||||||
|
current = current.next;
|
||||||
|
}
|
||||||
|
|
||||||
|
// current ist jetzt der letzte Eintrag
|
||||||
|
// setze neue Node als Nachfolger von bisher letzem Eintrag
|
||||||
|
current.setNext(n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void add(int n, T wert) {
|
||||||
|
// Neue Node anlegen
|
||||||
|
Node<T> neu = new Node<T>(wert);
|
||||||
|
|
||||||
|
if (n == 0) {
|
||||||
|
// Setze Nachfolger auf bisherigen Start
|
||||||
|
neu.setNext(first);
|
||||||
|
// Setze neuen Start auf neues Element
|
||||||
|
first = neu;
|
||||||
|
} else {
|
||||||
|
Node<T> current = first;
|
||||||
|
// gehe an den Vorgänger
|
||||||
|
for (int i=0; i<n-1; i++) {
|
||||||
|
if (current.next != null) {
|
||||||
|
current = current.next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ändere neu.next auf current.next
|
||||||
|
neu.setNext(current.next);
|
||||||
|
|
||||||
|
// ändere current.next auf neu
|
||||||
|
current.setNext(neu);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean contains(T wert) {
|
||||||
|
Node<T> current = first;
|
||||||
|
|
||||||
|
while (current != null) {
|
||||||
|
if (current.wert == wert) return true;
|
||||||
|
|
||||||
|
current = current.next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T remove(int n) {
|
||||||
|
if (n >= size()) return null;
|
||||||
|
|
||||||
|
if (n == 0) {
|
||||||
|
T wert = first.wert;
|
||||||
|
|
||||||
|
first = first.next;
|
||||||
|
|
||||||
|
return wert;
|
||||||
|
} 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
T wert = current.next.wert;
|
||||||
|
|
||||||
|
// Setze Übernächsten Eintrag als Nachfolger
|
||||||
|
current.next = current.next.next;
|
||||||
|
|
||||||
|
return wert;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
public class Node<T>
|
||||||
|
{
|
||||||
|
public T wert;
|
||||||
|
|
||||||
|
public Node<T> next;
|
||||||
|
|
||||||
|
public Node(T w) {
|
||||||
|
wert = w;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNext(Node n) {
|
||||||
|
next = n;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
------------------------------------------------------------------------
|
||||||
|
Dies ist die README-Datei des Projekts. Hier sollten Sie Ihr Projekt
|
||||||
|
beschreiben.
|
||||||
|
Erzählen Sie dem Leser (jemand, der nichts über dieses Projekt weiss),
|
||||||
|
alles, was er/sie wissen muss. Üblicherweise sollte der Kommentar
|
||||||
|
zumindest die folgenden Angaben umfassen:
|
||||||
|
------------------------------------------------------------------------
|
||||||
|
|
||||||
|
PROJEKTBEZEICHNUNG:
|
||||||
|
PROJEKTZWECK:
|
||||||
|
VERSION oder DATUM:
|
||||||
|
WIE IST DAS PROJEKT ZU STARTEN:
|
||||||
|
AUTOR(EN):
|
||||||
|
BENUTZERHINWEISE:
|
|
@ -0,0 +1,25 @@
|
||||||
|
public class Test
|
||||||
|
{
|
||||||
|
public static void test() {
|
||||||
|
List liste = new List<String>();
|
||||||
|
liste.add("Hallo");
|
||||||
|
liste.add("Welt");
|
||||||
|
liste.add("Informatik");
|
||||||
|
liste.add("ist");
|
||||||
|
liste.add("toll");
|
||||||
|
liste.add("!!!");
|
||||||
|
|
||||||
|
liste.remove(1);
|
||||||
|
|
||||||
|
liste.add(3, "wirklich");
|
||||||
|
|
||||||
|
Node<String> current = liste.first;
|
||||||
|
|
||||||
|
while (current != null) {
|
||||||
|
System.out.println(current.wert);
|
||||||
|
current = current.next;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(liste.contains("ist"));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,69 @@
|
||||||
|
#BlueJ package file
|
||||||
|
dependency1.from=LinkedList
|
||||||
|
dependency1.to=Node
|
||||||
|
dependency1.type=UsesDependency
|
||||||
|
dependency2.from=List
|
||||||
|
dependency2.to=Node
|
||||||
|
dependency2.type=UsesDependency
|
||||||
|
dependency3.from=Test
|
||||||
|
dependency3.to=List
|
||||||
|
dependency3.type=UsesDependency
|
||||||
|
dependency4.from=Test
|
||||||
|
dependency4.to=Node
|
||||||
|
dependency4.type=UsesDependency
|
||||||
|
editor.fx.0.height=1055
|
||||||
|
editor.fx.0.width=960
|
||||||
|
editor.fx.0.x=960
|
||||||
|
editor.fx.0.y=25
|
||||||
|
editor.fx.1.height=1055
|
||||||
|
editor.fx.1.width=960
|
||||||
|
editor.fx.1.x=0
|
||||||
|
editor.fx.1.y=25
|
||||||
|
objectbench.height=191
|
||||||
|
objectbench.width=557
|
||||||
|
package.divider.horizontal=0.6
|
||||||
|
package.divider.vertical=0.8002018163471241
|
||||||
|
package.editor.height=786
|
||||||
|
package.editor.width=821
|
||||||
|
package.editor.x=0
|
||||||
|
package.editor.y=25
|
||||||
|
package.frame.height=1055
|
||||||
|
package.frame.width=960
|
||||||
|
package.numDependencies=4
|
||||||
|
package.numTargets=4
|
||||||
|
package.showExtends=true
|
||||||
|
package.showUses=true
|
||||||
|
project.charset=UTF-8
|
||||||
|
readme.height=60
|
||||||
|
readme.name=@README
|
||||||
|
readme.width=48
|
||||||
|
readme.x=10
|
||||||
|
readme.y=10
|
||||||
|
target1.height=70
|
||||||
|
target1.name=Test
|
||||||
|
target1.showInterface=false
|
||||||
|
target1.type=ClassTarget
|
||||||
|
target1.width=120
|
||||||
|
target1.x=500
|
||||||
|
target1.y=90
|
||||||
|
target2.height=70
|
||||||
|
target2.name=Node
|
||||||
|
target2.showInterface=false
|
||||||
|
target2.type=ClassTarget
|
||||||
|
target2.width=120
|
||||||
|
target2.x=130
|
||||||
|
target2.y=100
|
||||||
|
target3.height=70
|
||||||
|
target3.name=List
|
||||||
|
target3.showInterface=false
|
||||||
|
target3.type=ClassTarget
|
||||||
|
target3.width=120
|
||||||
|
target3.x=300
|
||||||
|
target3.y=460
|
||||||
|
target4.height=70
|
||||||
|
target4.name=LinkedList
|
||||||
|
target4.showInterface=false
|
||||||
|
target4.type=ClassTarget
|
||||||
|
target4.width=120
|
||||||
|
target4.x=130
|
||||||
|
target4.y=290
|
Loading…
Reference in New Issue