AbstrakteDatentypen/LinkedList.java

84 lines
1.8 KiB
Java

public class LinkedList<T>
{
private Node<T> start;
public LinkedList(){
}
public LinkedList(T[] arr){
for (int i = 0; i < arr.length; i++){
this.append(arr[i]);
}
}
public LinkedList(T st){
start = new Node(st);
}
public void append(T zahl){
//anlegen eines neuen Nodes
Node<T> neu = new Node(zahl);
Node<T> current = this.start;
if (this.start == null){
this.start = neu;
}
else{
while (current.next != null){ //solange es einen Nachfolger gibt
current = current.next;
}
//current ist jetzt das letzte Element
// hängt neuen Node an
current.next = neu;
}
}
public int length(){
Node current = this.start;
int count= 0;
while(current != null){
current = current.next;
count++;
}
return count;
}
public T erste(){
if (start == null) return null;
T i = this.start.wert;
this.start = start.next;
return i;
}
public T getNtenWert(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;
}
public String toString(){
String result = "";
for (int i = 0; i < this.length(); i++){
result += this.getNtenWert(i) + ",";
}
return result;
}
/*public String toString(){
String result = "";
Node current = start;
while(current != null){
result += current.wert + ",";
current = current.next;
}
return result;
}*/
}