Ubgrade 18.1. 2023

master
david 2023-01-18 10:57:28 +01:00
parent f470c248c7
commit 90ff5d3a2a
6 changed files with 160 additions and 9 deletions

View File

@ -48,7 +48,7 @@ public class LinkedList<T>
for(int i = 0; i<n; i++){ for(int i = 0; i<n; i++){
current = current.next; current = current.next;
} }
return current.zahl; return current.wert;
} }
public void loesche(int n){ public void loesche(int n){

View File

@ -48,7 +48,7 @@ public class List<T>
for(int i = 0; i<n; i++){ for(int i = 0; i<n; i++){
current = current.next; current = current.next;
} }
return current.zahl; return current.wert;
} }
public void loesche(int n){ public void loesche(int n){

View File

@ -8,12 +8,12 @@
public class Node<T> public class Node<T>
{ {
public T zahl; public T wert;
public Node<T> next; public Node<T> next;
public Node(T z){ public Node(T z){
zahl = z; wert = z;
} }
public void setNext(Node n){ public void setNext(Node n){
next = n; next = n;

71
Queue.java Normal file
View File

@ -0,0 +1,71 @@
/**
* Beschreiben Sie hier die Klasse Queue.
*
* @author (Ihr Name)
* @version (eine Versionsnummer oder ein Datum)
*/
public class Queue<T>
{
public Node<T> first;
public Queue(){
}
public boolean isEmpty(){
if(first == null){
return true;
}
return false;
}
public void enqueue (T neu) {
Node<T> n = new Node<T>(neu);//Neue Node mit Zahl "neu " anlegen
//Überprüfe ob die liste leer ist
if(first == null){
//setze neue node als erster Eintrag
first = n;
}
else {
Node<T> current = first;
while(current.next != null){
current = current.next;
}
current.setNext(n);
}
//current ist jetzt der letzte 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 dequeue(){
if(first == null){
return null;
}
else{
Node<T> current = first;
first = first.next;
return current.wert;
}
}
public T front(){
return first.wert;
}
}

68
Stack.java Normal file
View File

@ -0,0 +1,68 @@
/**
* Beschreiben Sie hier die Klasse Stack.
*
* @author (Ihr Name)
* @version (eine Versionsnummer oder ein Datum)
*/
public class Stack<T>
{
public Node<T> first;
public Stack(){
}
public boolean isEmpty(){
if(first == null){
return true;
}
return false;
}
public void push (T neu) {
Node<T> n = new Node<T>(neu);//Neue Node mit Zahl "neu " anlegen
//Überprüfe ob die liste leer ist
if(first == null){
//setze neue node als erster Eintrag
first = n;
}
else {
Node<T> current = first;
while(current.next != null){
current = current.next;
}
current.setNext(n);
}
//current ist jetzt der letzte 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 pop(){
if(first == null){
return null;
}
else{
Node<T> current = first;
first = first.next;
return current.wert;
}
}
public T top(){
return first.wert;
}
}

View File

@ -11,24 +11,36 @@ public class test
Node<String> test = new Node<String>("Hallo"); Node<String> test = new Node<String>("Hallo");
Node<Integer> test2 = new Node<Integer>(5); Node<Integer> test2 = new Node<Integer>(5);
List liste = new List<String>(); Queue<Integer> liste = new Queue<Integer>();
liste.einfuegen(5); /*liste.einfuegen(5);
liste.einfuegen(6); liste.einfuegen(6);
liste.einfuegen(8); liste.einfuegen(8);
liste.einfuegen(3); liste.einfuegen(3);
liste.einfuegen(7); liste.einfuegen(7);
liste.einfuegen(9); liste.einfuegen(9);
liste.loesche(2); liste.loesche(2);
liste.hinzufügen(4,"wirklich"); liste.hinzufügen(4,"wirklich");*/
liste.enqueue(7);
liste.enqueue(4);
liste.enqueue(8);
liste.enqueue(2);
liste.enqueue(0);
liste.enqueue(4);
int Selection = liste.dequeue();
System.out.println("Die Zahl " +Selection+" wurde entfernt.");
int Selection2 = liste.dequeue();
System.out.println("Die Zahl " +Selection2+" wurde entfernt.");
int Angabe = liste.front();
System.out.println("Aktuell ist die erste Zahl " +Angabe+".");
Node current = liste.first; Node current = liste.first;
while(current != null){ while(current != null){
System.out.println( current.zahl ); System.out.println( current.wert );
current = current.next; current = current.next;
} }
System.out.println("Die Liste hat "+liste.laenge()+" Einträge. "); System.out.println("Die Liste hat "+liste.laenge()+" Einträge. ");
} }