From d843998f7106c28eb4be0dd17c8cfd4fbd0f3ceb Mon Sep 17 00:00:00 2001 From: lazicmi Date: Wed, 23 Feb 2022 11:02:40 +0100 Subject: [PATCH] Datentypen --- Datentypen/.classpath | 10 ++ Datentypen/.gitignore | 1 + Datentypen/.project | 17 +++ .../.settings/org.eclipse.jdt.core.prefs | 14 ++ Datentypen/src/LinkedList.java | 130 ++++++++++++++++++ Datentypen/src/Node.java | 11 ++ Datentypen/src/Queue.java | 66 +++++++++ Datentypen/src/QueueTest.java | 67 +++++++++ Datentypen/src/Stack.java | 61 ++++++++ Datentypen/src/StackTest.java | 66 +++++++++ Datentypen/src/Test.java | 71 ++++++++++ 11 files changed, 514 insertions(+) create mode 100644 Datentypen/.classpath create mode 100644 Datentypen/.gitignore create mode 100644 Datentypen/.project create mode 100644 Datentypen/.settings/org.eclipse.jdt.core.prefs create mode 100644 Datentypen/src/LinkedList.java create mode 100644 Datentypen/src/Node.java create mode 100644 Datentypen/src/Queue.java create mode 100644 Datentypen/src/QueueTest.java create mode 100644 Datentypen/src/Stack.java create mode 100644 Datentypen/src/StackTest.java create mode 100644 Datentypen/src/Test.java diff --git a/Datentypen/.classpath b/Datentypen/.classpath new file mode 100644 index 0000000..57bca72 --- /dev/null +++ b/Datentypen/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/Datentypen/.gitignore b/Datentypen/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/Datentypen/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/Datentypen/.project b/Datentypen/.project new file mode 100644 index 0000000..7574289 --- /dev/null +++ b/Datentypen/.project @@ -0,0 +1,17 @@ + + + Datentypen + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/Datentypen/.settings/org.eclipse.jdt.core.prefs b/Datentypen/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..8c9943d --- /dev/null +++ b/Datentypen/.settings/org.eclipse.jdt.core.prefs @@ -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 diff --git a/Datentypen/src/LinkedList.java b/Datentypen/src/LinkedList.java new file mode 100644 index 0000000..4bc571c --- /dev/null +++ b/Datentypen/src/LinkedList.java @@ -0,0 +1,130 @@ +public class LinkedList { + + private Node 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(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 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(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 Neu = new Node(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 Neu = new Node(wert); // neuer Knoten namen neu wird erstellt und der wert "wert" wird dem + // zugewissen + Node 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 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 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 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ˆfle() { // 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 getStart() { // getter Mehode f¸r Start + return this.start; + } + + public int size() { // getter Mehode f¸r grˆfle + return this.size; + } + +} \ No newline at end of file diff --git a/Datentypen/src/Node.java b/Datentypen/src/Node.java new file mode 100644 index 0000000..e773289 --- /dev/null +++ b/Datentypen/src/Node.java @@ -0,0 +1,11 @@ + public class Node { //Node wird erstellt + + public A wert; // wert wird mit Datentyp a initialisiert + public Node next; // next wird mit datentyp Node initilisiert + + public Node(A z) { //node Konstruktor + wert = z; //wert wir z zugweisen + } + + } + diff --git a/Datentypen/src/Queue.java b/Datentypen/src/Queue.java new file mode 100644 index 0000000..79245ae --- /dev/null +++ b/Datentypen/src/Queue.java @@ -0,0 +1,66 @@ + +public class Queue { + private Node 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(wert); + } else { + Node current = this.start; + while (current.next != null) { + current = current.next; + } + current.next = new Node(wert); + } + } + + public Q dequeue() { //siehe Stack Mehode pop + Node 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 current = this.start; + if (isEmpty() == true) { + return null; + } else { + Q I = current.wert; + return I ; + } + } + + public Node getStart() { //getter Methode f¸rStart + return this.start; + } + + public int size() { //getter Methode f¸r grˆfle + return this.size; + } + +} \ No newline at end of file diff --git a/Datentypen/src/QueueTest.java b/Datentypen/src/QueueTest.java new file mode 100644 index 0000000..009eb6b --- /dev/null +++ b/Datentypen/src/QueueTest.java @@ -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 queue = new Queue(); + 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 current = queue.getStart(); //siehe Test + while (current != null) { + System.out.println(current.wert); + current = current.next; + + } + + } else { + if (a == 2) { + Queue queue1 = new Queue(); + 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 current = queue1.getStart(); // siehe Test + while (current != null) { + System.out.println(current.wert); + current = current.next; + + } + } + } + } +} \ No newline at end of file diff --git a/Datentypen/src/Stack.java b/Datentypen/src/Stack.java new file mode 100644 index 0000000..a3661c5 --- /dev/null +++ b/Datentypen/src/Stack.java @@ -0,0 +1,61 @@ + +public class Stack { + private Node 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 Neu = new Node(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 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 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 getStart() { // getter Mehode f¸r Start + return this.start; + } + + public int size() { // getter Mehode f¸r grˆfle + return this.size; + } + +} \ No newline at end of file diff --git a/Datentypen/src/StackTest.java b/Datentypen/src/StackTest.java new file mode 100644 index 0000000..4598596 --- /dev/null +++ b/Datentypen/src/StackTest.java @@ -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 stack = new Stack(); // 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 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 stack1 = new Stack(); //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 current = stack1.getStart(); //siehe Test + while (current != null) { + System.out.println(current.wert); + current = current.next; + + } + } + + } +} diff --git a/Datentypen/src/Test.java b/Datentypen/src/Test.java new file mode 100644 index 0000000..fff32fb --- /dev/null +++ b/Datentypen/src/Test.java @@ -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 liste = new LinkedList(); //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 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 liste1 = new LinkedList(); //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 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 + + } + + } + } +} \ No newline at end of file