Ubgrade 11.1
parent
8a8fbc4e93
commit
a137bfc354
|
@ -5,14 +5,14 @@
|
||||||
* @author (Ihr Name)
|
* @author (Ihr Name)
|
||||||
* @version (eine Versionsnummer oder ein Datum)
|
* @version (eine Versionsnummer oder ein Datum)
|
||||||
*/
|
*/
|
||||||
public class LinkedList
|
public class LinkedList<T>
|
||||||
{
|
{
|
||||||
public Node first;
|
public Node<T> first;
|
||||||
public LinkedList(){
|
public LinkedList(){
|
||||||
}
|
}
|
||||||
|
|
||||||
public void einfuegen (int neu) {
|
public void einfuegen (T neu) {
|
||||||
Node n = new Node(neu);//Neue Node mit Zahl "neu " anlegen
|
Node<T> n = new Node<T>(neu);//Neue Node mit Zahl "neu " anlegen
|
||||||
|
|
||||||
//Überprüfe ob die liste leer ist
|
//Überprüfe ob die liste leer ist
|
||||||
if(first == null){
|
if(first == null){
|
||||||
|
@ -20,7 +20,7 @@ public class LinkedList
|
||||||
first = n;
|
first = n;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Node current = first;
|
Node<T> current = first;
|
||||||
|
|
||||||
while(current.next != null){
|
while(current.next != null){
|
||||||
current = current.next;
|
current = current.next;
|
||||||
|
@ -32,4 +32,52 @@ public class LinkedList
|
||||||
//setze neue Node als Nachfolger von bisher letztem 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){
|
||||||
|
neu.setNext(first);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -5,14 +5,14 @@
|
||||||
* @author (Ihr Name)
|
* @author (Ihr Name)
|
||||||
* @version (eine Versionsnummer oder ein Datum)
|
* @version (eine Versionsnummer oder ein Datum)
|
||||||
*/
|
*/
|
||||||
public class Node
|
public class Node<T>
|
||||||
{
|
{
|
||||||
|
|
||||||
public int zahl;
|
public T zahl;
|
||||||
|
|
||||||
public Node next;
|
public Node next;
|
||||||
|
|
||||||
public Node(int z){
|
public Node(T z){
|
||||||
zahl = z;
|
zahl = z;
|
||||||
}
|
}
|
||||||
public void setNext(Node n){
|
public void setNext(Node n){
|
||||||
|
|
|
@ -7,7 +7,9 @@
|
||||||
*/
|
*/
|
||||||
public class test
|
public class test
|
||||||
{
|
{
|
||||||
public void test(){
|
public static void test(){
|
||||||
|
Node<String> test = new Node<String>("Hallo");
|
||||||
|
Node<Integer> test2 = new Node<Integer>(5);
|
||||||
|
|
||||||
LinkedList liste = new LinkedList();
|
LinkedList liste = new LinkedList();
|
||||||
liste.einfuegen(5);
|
liste.einfuegen(5);
|
||||||
|
@ -16,7 +18,8 @@ public class test
|
||||||
liste.einfuegen(3);
|
liste.einfuegen(3);
|
||||||
liste.einfuegen(7);
|
liste.einfuegen(7);
|
||||||
liste.einfuegen(9);
|
liste.einfuegen(9);
|
||||||
|
liste.loesche(2);
|
||||||
|
liste.hinzufügen(8,"wirklich");
|
||||||
Node current = liste.first;
|
Node current = liste.first;
|
||||||
|
|
||||||
while(current != null){
|
while(current != null){
|
||||||
|
@ -24,8 +27,10 @@ public class test
|
||||||
current = current.next;
|
current = current.next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
System.out.println("Die Liste hat "+liste.laenge()+" Einträge. ");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue