heute zu viel extra arbeit gemacht
parent
9f52e15d8b
commit
e781629e78
|
@ -0,0 +1,83 @@
|
|||
|
||||
public class LinkedList
|
||||
{
|
||||
private Node start;
|
||||
public LinkedList(){
|
||||
}
|
||||
public LinkedList(int[] arr){
|
||||
for (int i = 0; i < arr.length; i++){
|
||||
this.append(arr[i]);
|
||||
}
|
||||
}
|
||||
public LinkedList(int st){
|
||||
start = new Node(st);
|
||||
}
|
||||
|
||||
public void append(int zahl){
|
||||
//anlegen eines neuen Nodes
|
||||
Node neu = new Node(zahl);
|
||||
Node 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.next != null){
|
||||
current = current.next;
|
||||
count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
public int erste(){
|
||||
if (start == null) return 0;
|
||||
int i = this.start.wert;
|
||||
this.start = start.next;
|
||||
return i;
|
||||
}
|
||||
|
||||
public int getNteZahl(int n){
|
||||
Node current = this.start;
|
||||
for ( int i = 0; i < n ; i++ ){
|
||||
if (current == null)return 0;
|
||||
current = current.next;
|
||||
}
|
||||
if (current == null ) return 0;
|
||||
return current.wert;
|
||||
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
String result = "";
|
||||
|
||||
for (int i = 0; i < this.length(); i++){
|
||||
result += this.getNteZahl(i) + ",";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/*public String toString(){
|
||||
String result = "";
|
||||
|
||||
Node current = start;
|
||||
|
||||
while(current != null){
|
||||
result += current.wert + ",";
|
||||
current = current.next;
|
||||
}
|
||||
return result;
|
||||
}*/
|
||||
}
|
38
Node.java
38
Node.java
|
@ -1,33 +1,17 @@
|
|||
|
||||
/**
|
||||
* Beschreiben Sie hier die Klasse Node.
|
||||
*
|
||||
* @author (Ihr Name)
|
||||
* @version (eine Versionsnummer oder ein Datum)
|
||||
*/
|
||||
public class Node
|
||||
{
|
||||
// Instanzvariablen - ersetzen Sie das folgende Beispiel mit Ihren Variablen
|
||||
private int x;
|
||||
|
||||
/**
|
||||
* Konstruktor für Objekte der Klasse Node
|
||||
*/
|
||||
public Node()
|
||||
{
|
||||
// Instanzvariable initialisieren
|
||||
x = 0;
|
||||
public int wert;
|
||||
public Node next;
|
||||
public Node(int w){
|
||||
this.wert = w;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ein Beispiel einer Methode - ersetzen Sie diesen Kommentar mit Ihrem eigenen
|
||||
*
|
||||
* @param y ein Beispielparameter für eine Methode
|
||||
* @return die Summe aus x und y
|
||||
*/
|
||||
public int beispielMethode(int y)
|
||||
{
|
||||
// tragen Sie hier den Code ein
|
||||
return x + y;
|
||||
public void setWert(int w){
|
||||
this.wert = w;
|
||||
}
|
||||
|
||||
public void setNext (Node n){
|
||||
this.next = n;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
import java.util.Random;
|
||||
public class test
|
||||
{
|
||||
public static void test1(){
|
||||
Random r = new Random();
|
||||
int[] arr = new int[5];
|
||||
for (int i = 0; i < arr.length; i++){
|
||||
arr[i] = r.nextInt();
|
||||
}
|
||||
LinkedList test = new LinkedList(arr);
|
||||
|
||||
System.out.println(test);
|
||||
System.out.println(test.erste());
|
||||
System.out.println(test);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue