Informatikks1/LinkedList.java

67 lines
1.3 KiB
Java

public class LinkedList<T>
{
private Node<T> start;
public LinkedList(){
}
public void einfuegen(T zahl){
Node neu = new Node();
neu.wert = zahl;
if (this.start == null) {
this.start = neu;
Node current = this.start;
while (current.next != null) {
current = current.next;
}
current.next = neu;
}
}
public int laenge(){
int count = 0;
Node current = this.start;
while(current != null) {
current = current.next;
count++;
}
return count;
}
public String toString(){
String result = " ";
Node current = this.start;
while( current != null) {
result += current.wert + ",";
current = current.next;
}
return result;
}
public T erste(){
if ( this.start == null){
return null;
}
T tmp = this.start.wert;
this.start = this.start.next;
return tmp;
}
public T getNteZahl(int n){
Node<T> current = this.start;
for (int i = 0; i < n; i++){
if (current == null) return null;
current = current.next;
}
if( current == null) return null;
return current.wert;
}
}