Datentypen

master
lazicmi 2022-02-23 11:02:40 +01:00
commit d843998f71
11 changed files with 514 additions and 0 deletions

10
Datentypen/.classpath Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>

1
Datentypen/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/bin/

17
Datentypen/.project Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Datentypen</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@ -0,0 +1,14 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=17
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=17

View File

@ -0,0 +1,130 @@
public class LinkedList<A> {
private Node<A> start; // Attribut start mit Node mit Genertic als Datentyp
private int size; // Attribut size
public LinkedList() { // Konstruktor der Liste
this.start = null; // Attribute von oben werden initilaisiert
this.size = 0;
this.size = this.size -1;
}
public boolean isEmpty() { // Klasse fragt ab ob die liste leer ist
if (size == 0) { // wenn l‰nge der liste 0 ist
return true; // gibt true zur¸ck weil es boolean ist
} else { // wenn oberes nicht erfÔøΩllt wird
return false; // gibt false zur¸ck wegen boolean
}
}
public void add(A wert) { // Methode f‰ngt hinten an die Liste etwas an
this.size++; // grˆfle wird hochgez‰hlt da liste um ein grˆfler werden muss
if (this.start == null) { // wenn start wert = 0 ist
this.start = new Node<A>(wert); // start wird zu neuem Knoten mit wert wert
} else { // wenn obere if bedingung nicht erf¸llt wird dann soll er volgendes ausgef¸hrt
Node<A> current = this.start; // current bekommt den wert der start zugewissen wurde
while (current.next != null) { // w‰hrend nachfolger des jetzigen Knotens ungleich null ist
current = current.next; // wird current dem n‰chsten werd zugewissen
}
current.next = new Node<A>(wert); // dem jetzigen Knoten wird als 2ter eintrag ein neuer Knoten zugewissen
}
}
public void addziwschen(A wert, int n) { // Methode um werte dazwischen einzuf¸gen
if (n < size) { // wenn die l‰nge der Liste grˆfler ist als Index in dem man etwas einf¸gen will
size++; // L‰nge wird eins Hochgez‰hlt
if (n < 2) { // wenn index kleiner als 2 ist, also wenn er bei der ersten stelle ist
Node<A> Neu = new Node<A>(wert); // neuer Knoten mit dem namen Nue wird angelegt und der wert "wert"
// wird ¸bergeben
Neu.next = start; // 2ter eintrag vom Knoten "Neu" wird als start festgelegt
start = Neu; // start wird zum ersten eintrage von neu
} else { // wenn obere if bedingung nicht erf¸llt wird
Node<A> Neu = new Node<A>(wert); // neuer Knoten namen neu wird erstellt und der wert "wert" wird dem
// zugewissen
Node<A> current = this.start; // neuer Knoten "current" wird erstellet und der wert von "this.start"
// wird ¸bergeben
int a = 0; // neue variable wird erstellt
while (a != n - 2) { // w‰hrend 0 ungleich dem index - 2 ist
current = current.next; // dann wird "current" gleich dem 2ten werd des Knotens gesetzt
a++; // a wird hochgez‰hlt um zum gew¸nschten Index zu gelangen
}
Neu.next = current.next; // 2ter werd von dem Knoten neu wird gleich dem 2ten werd von dem Knoten
// currentn gesetzt
current.next = Neu; // 2ter werd wird Knoten Neu zeugewissen
}
} else {
add(wert); //wenn grˆfler ist soll der den wert hinten einf¸gen
}
}
public A get(int n) { // Methode um auf index gespeicherten wert zu bekommen
Node<A> current = this.start; // neue Node current wird mit dem wert start "bef¸llt"
for (int i = 0; i < n ; i++) { // solange der index grˆfler als i ist geht er das in der SChleife durch
current = current.next; // solange wird der Knoten current gleich dem 2ten werd im Knoten gesetzt
}
A I =current.wert;
return I;
}
public boolean contains(A wert) { //gibt aus ob ein wert in der Liste ist
Node<A> current = this.start; // Knoten current wird der wert start zugewissen
while (current != null) { // solange current ungleich null ist wird das in der Schleife ausgefÔøΩhrt
if (current.wert == wert) { // wenn der der Knoten current = der wert ist
System.out.println(wert + " " + "ist in der Liste");
return true; // dann gibt es den eintrag also muss er true zurÔøΩckgeben
}
current = current.next; // n‰chster wert wird zum aktuellen
}
System.out.println(wert + " " + "ist nicht in der Liste");
return false; // sonst ist er nicht drin
}
public A remove(int n) { // Methode zum lˆschen eines Knotens
Node<A> current = this.start; // neuer Knoten current wird erstellt
if(n > size) {
return null;
}
if (n > 1) { // wenn index grˆfler 1 ist
int i = 1; //neue Variable
while (i != n -1) { // solange i ungleich dem index - 1 ist
current = current.next; // wird der jetzige wert dem 2ten zugewissen
i++; // wird hochgez‰hlt um den richtigen index zu erreichen
}
current.next = current.next.next; // 2ter wert wird dem ¸bergeordneten wert zugewissen
size--; //grˆfle wird ein runtergez‰hlt um es zu lˆschen
} else {
this.start = current.next; // sonst ist start der 2te eintrag
}
A I = current.wert;
return I;
}
public int grˆe() { // Methode um grˆfle auszugeben
System.out.print("Die Grˆfle der Liste ist: "); //ausgabe
System.out.println(size + "\n"); //ausgabe
return size; // gibt grˆfle zur¸ck
}
public Node<A> getStart() { // getter Mehode f¸r Start
return this.start;
}
public int size() { // getter Mehode f¸r grˆfle
return this.size;
}
}

11
Datentypen/src/Node.java Normal file
View File

@ -0,0 +1,11 @@
public class Node<A> { //Node wird erstellt
public A wert; // wert wird mit Datentyp a initialisiert
public Node<A> next; // next wird mit datentyp Node<A> initilisiert
public Node(A z) { //node Konstruktor
wert = z; //wert wir z zugweisen
}
}

66
Datentypen/src/Queue.java Normal file
View File

@ -0,0 +1,66 @@
public class Queue<Q> {
private Node<Q> start;
private int size;
public Queue() { //Konstruktor f¸r Queue
this.start = null;
this.size = 0;
}
public boolean isEmpty() { // Klasse fragt ab ob die liste leer ist
if (size == 0) { // wenn l‰nge der liste 0 ist
return true; // gibt true zur¸ck weil es boolean ist
} else { // wenn oberes nicht erf¸llt wird
return false; // gibt false zur¸ck wegen boolean
}
}
public void enqueue(Q wert) { // siehe Kommentare bei LinkedList Methode add
this.size++;
if (this.start == null) {
this.start = new Node<Q>(wert);
} else {
Node<Q> current = this.start;
while (current.next != null) {
current = current.next;
}
current.next = new Node<Q>(wert);
}
}
public Q dequeue() { //siehe Stack Mehode pop
Node<Q> current = this.start;
if (isEmpty() == true) {
return null;
} else {
Q I = current.wert;
this.start = current.next;
return I;
}
}
public Q front() { //siehe Stack Methode top
Node<Q> current = this.start;
if (isEmpty() == true) {
return null;
} else {
Q I = current.wert;
return I ;
}
}
public Node<Q> getStart() { //getter Methode f¸rStart
return this.start;
}
public int size() { //getter Methode f¸r grˆfle
return this.size;
}
}

View File

@ -0,0 +1,67 @@
import java.util.Scanner;
public class QueueTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //Kommentare gleich wie in Stack Klasse aufler mit MEthoden aus queue Klasse
System.out.println("Welcher Datentyp : \n 1:String \n 2:Int ");
int a = sc.nextInt();
if (a == 1) {
Queue<String> queue = new Queue<String>();
System.out.println("Bitte Wˆrter eingeben :");
Scanner sb = new Scanner(System.in);
String b = sb.nextLine();
String c = sb.nextLine();
String d = sb.nextLine();
String e = sb.nextLine();
queue.enqueue(b);
queue.enqueue(c);
queue.enqueue(d);
queue.enqueue(e);
System.out.println("Dequeue : " + queue.dequeue());
System.out.println("Front : " + queue.front());
queue.isEmpty();
if(true) {
System.out.println("Ist nicht Leer");
}else {System.out.println("Ist Leer");}
System.out.println("\n");
System.out.println("Zahlen in der Queue : ");
Node<String> current = queue.getStart(); //siehe Test
while (current != null) {
System.out.println(current.wert);
current = current.next;
}
} else {
if (a == 2) {
Queue<Integer> queue1 = new Queue<Integer>();
System.out.println("Bitte Zahlen eingeben :");
Scanner sg = new Scanner(System.in);
int a1 = sg.nextInt();
int b = sg.nextInt();
int c = sg.nextInt();
int d = sg.nextInt();
queue1.enqueue(a1);
queue1.enqueue(b);
queue1.enqueue(c);
queue1.enqueue(d);
queue1.isEmpty();
System.out.println("Dequeue : " + queue1.dequeue());
System.out.println("Front : " + queue1.front());
queue1.isEmpty();
if(true) {
System.out.println("Ist nicht Leer");
}else {System.out.println("Ist Leer");}
System.out.println("\n");
System.out.println("Zahlen in der Queue : ");
Node<Integer> current = queue1.getStart(); // siehe Test
while (current != null) {
System.out.println(current.wert);
current = current.next;
}
}
}
}
}

61
Datentypen/src/Stack.java Normal file
View File

@ -0,0 +1,61 @@
public class Stack<S> {
private Node<S> start; //Attribut
private int size; //Attribute
public Stack() { // Konstruktor von Stack
this.start = null; //start wert wird null gesetzt
this.size = 0; // wert der grˆfle wird 0 gesetzt
}
public boolean isEmpty() { // Klasse fragt ab ob die liste leer ist
if (size == 0) { // wenn l‰nge der liste 0 ist
return true; // gibt true zur¸ck weil es boolean ist
} else { // wenn oberes nicht erf¸llt wird
return false; // gibt false zur¸ck wegen boolean
}
}
public void push(S wert) { //f¸gt vorne an die Liste etwas an (legt oben drauf)
size++; //z‰hlt hoch
Node<S> Neu = new Node<S>(wert); //neuer Knoten wird erstellt mit dme wert "wert"
Neu.next = start; // 2ter werd von dem Knoten neu wird gleich dem 2ten werd von dem Knoten current gesetzt
start = Neu; // 2ter werd wird Knoten Neu zeugewissen
}
public S pop() { //soll von oben wieder runter nehmen
Node<S> current = this.start; // neuer Knoten current wird der wert Start zugweiesen
if (isEmpty() == true) { //wenn die Methode is Empty wahr ist
return null; //soll es zur¸ck geben
} else {
S I = current.wert; //ausgabe von current.wert
this.start = current.next; //start wird zweitem wert von current zugewiesen
return I;
}
}
public S top() { // Methode um letze eingef¸gte zahl zu ermitteln
Node<S> current = this.start; // neuer Knoten current wird erstellet und mit wert von start bef¸llt
if (isEmpty() == true) { //wenn Methode is Empty wahr ist soll er zur¸ckgeben
return null ;
} else {
S I = current.wert;
return I;
}
}
public Node<S> getStart() { // getter Mehode f¸r Start
return this.start;
}
public int size() { // getter Mehode f¸r grˆfle
return this.size;
}
}

View File

@ -0,0 +1,66 @@
import java.util.Scanner;
public class StackTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //neuer Scanner wird erstellt
System.out.println("Welcher Datentyp : \n 1:String \n 2:Int "); //ausgabe
int a = sc.nextInt(); //a wird auf den n‰chsten eingegebenen wert gesetzt
if(a == 1) {Stack<String> stack = new Stack<String>(); // wenn a 1 ist wird ein neuer Stack erstellt mit datentyp String
System.out.println("Bitte Wˆrter eingeben :"); //ausgabe
Scanner sb = new Scanner(System.in); //neuer Scanner wird erstellt
String b = sb.nextLine(); //Neuer String wird von der n‰chsten Linie gelesen
String c = sb.nextLine();
String d = sb.nextLine();
String e = sb.nextLine();
stack.push(b); //Methode push wird aufgerufen und mit dem wert b ausgegeben
stack.push(c);
stack.push(d);
stack.push(e);
System.out.println("Pop : " + stack.pop()); //ausgabe
System.out.println("Top : " + stack.top());
stack.isEmpty(); //Methode wird aufgerufen
if(true) { //wenn obere Methode war ist
System.out.println("Ist nicht Leer"); //ausgabe
}else {System.out.println("Ist Leer");} //wenn obere Methode nicht war ist
System.out.println("\n"); //ausgabe
System.out.println("Zahlen in dem Stack : "); //ausgabe
Node<String> current = stack.getStart(); //siehe Test
while (current != null) { //solange current ungleich null ist
System.out.println(current.wert); //ausgabe
current = current.next; //current wird auf den 2ten wert des Knotens gesetzt
}
}if(a == 2) {Stack<Integer> stack1 = new Stack<Integer>(); //Kommentare sind gleich wie oben nur hier anstatt String Integer
System.out.println("Bitte Zahlen eingeben :");
Scanner sg = new Scanner(System.in);
int a1 = sg.nextInt();
int b = sg.nextInt();
int c = sg.nextInt();
int d = sg.nextInt();
stack1.push(a1);
stack1.push(b);
stack1.push(c);
stack1.push(d);
System.out.println("Pop : " + stack1.pop());
System.out.println("Top : " + stack1.top());
stack1.isEmpty();
if(true) {
System.out.println("Ist nicht Leer");
}else {System.out.println("Ist Leer");}
System.out.println("\n");
System.out.println("Zahlen in dem Stack : ");
Node<Integer> current = stack1.getStart(); //siehe Test
while (current != null) {
System.out.println(current.wert);
current = current.next;
}
}
}
}

71
Datentypen/src/Test.java Normal file
View File

@ -0,0 +1,71 @@
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //scanner wird erstellt
System.out.println("Welcher Datentyp : \n 1:String \n 2:Int "); //Ausgabe
int a = sc.nextInt(); // a wird auf den n‰chsten eingegebenen int gelegt
if (a == 1) { //wenn a 1 ist
LinkedList<String> liste = new LinkedList<String>(); //neue LinkedList "liste" wird erstellt
System.out.println("Bitte Wˆrter eingeben :"); //ausgabe
Scanner sb = new Scanner(System.in); // neuer Scanner wird erstellt
String a2 = sb.next(); // auslesen des n‰chsten eingegebenen worts
String a3 = sb.next();
String a4 = sb.next();
String a5 = sb.next();
String a6 = sb.next();
String a7 = sb.next();
liste.add(a2); //oberer strings werden ans ende der Liste gesetzt
liste.add(a3);
liste.add(a4);
liste.add(a5);
liste.add(a6);
liste.add(a7);
liste.remove(4); //Methode wird aufgerufen
liste.addziwschen("Ok", 2); //Methode wird aufgerufen
liste.isEmpty(); //Methode wird aufgerufen
if(true) {
System.out.println("Ist nicht Leer");
}else {System.out.println("Ist Leer");} //Methode wird aufgerufen
System.out.println("\n"); //ausgabe
System.out.println("Zahlen in der Liste : "); //ausgabe
Node<String> current = liste.getStart(); //Neue Node wird current bennant und mit den werten von start gef¸llt
while (current != null) { //w‰hrend current gleich null ist
System.out.println(current.wert); //ausgabe
current = current.next; //current wird auf die 2te stelle des Knotens gesetzt
}
}if (a == 2) { //wenn a gleich 2 ist
LinkedList<Integer> liste1 = new LinkedList<Integer>(); //neue Linked List wird erstellt
System.out.println("Bitte Zahlen eingeben :"); //ausgabe
Scanner sg = new Scanner(System.in); //neuer Scanner wird erstellt
int a1 = sg.nextInt(); //auslesen des n‰chsten eingegebenen int
int b = sg.nextInt();
int c = sg.nextInt();
int d = sg.nextInt();
liste1.add(a1); //neuer wert wird hinten an die Liste hinzugef¸gt
liste1.add(b);
liste1.add(c);
liste1.add(d);
System.out.println("\n");
liste1.contains(3); //Methode wird aufgerufen
liste1.remove(2); //Methode wird aufgerufen
liste1.addziwschen(8, 4); //Methode wird aufgerufen
liste1.isEmpty(); //Methode wird aufgerufen
if(true) {
System.out.println("Ist nicht Leer");
}else {System.out.println("Ist Leer");}
System.out.println("\n"); //ausgabe
System.out.println("Zahlen in dem Stack : "); //ausgabe
Node<Integer> current = liste1.getStart(); //Neue Node wird current bennant und mit den werten von start gef¸llt
while (current != null) { //w‰hrend current gleich null ist
System.out.println(current.wert); //ausgabe
current = current.next; //current wird auf die 2te stelle des Knotens gesetzt
}
}
}
}