added ws202425 courses
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,88 @@
|
||||
|
||||
|
||||
public class List<T>{
|
||||
private ListElement<T> head;
|
||||
|
||||
public List(ListElement<T> initLe){
|
||||
head = initLe;
|
||||
}
|
||||
|
||||
|
||||
public void insert(ListElement<T> newLe){
|
||||
newLe.setNext(head);
|
||||
this.head = newLe;
|
||||
}
|
||||
|
||||
|
||||
public boolean delete(){
|
||||
if (this.isEmpty()){
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
this.head = head.getNext();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean isEmpty(){
|
||||
if (head == null){
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public int length(){
|
||||
int count = 0;
|
||||
|
||||
ListElement<T> curr = head;
|
||||
while (curr != null){
|
||||
count += 1;
|
||||
curr = curr.getNext();
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
public void show(){
|
||||
ListElement<T> curr = head;
|
||||
while (curr != null){
|
||||
if (curr.getNext() == null){
|
||||
System.out.println(""+ curr.getValue());
|
||||
}
|
||||
else {
|
||||
System.out.print(curr.getValue() + ", ");
|
||||
}
|
||||
curr = curr.getNext();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean insertIndex(ListElement<T> newElem, int i) {
|
||||
if (i == 0){
|
||||
insert(newElem);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
ListElement<T> tmp = head;
|
||||
ListElement<T> prev = null;
|
||||
int l = 0;
|
||||
// Aufsuchen des i-ten Elements
|
||||
while (l < i) {
|
||||
prev = tmp;
|
||||
tmp = prev.getNext();
|
||||
l++;
|
||||
if (l > length()){
|
||||
return false;
|
||||
}
|
||||
} // l == i ist erreicht
|
||||
prev.setNext(newElem);
|
||||
newElem.setNext(tmp);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,38 @@
|
||||
|
||||
/*
|
||||
* class ListElement
|
||||
*
|
||||
*/
|
||||
|
||||
public class ListElement<T>{
|
||||
private T value;
|
||||
private ListElement<T> next;
|
||||
|
||||
ListElement(T initValue){
|
||||
this.value = initValue;
|
||||
this.next = null;
|
||||
}
|
||||
|
||||
ListElement(T initValue, ListElement<T> initNext){
|
||||
this.value = initValue;
|
||||
this.next = initNext;
|
||||
}
|
||||
|
||||
public void setNext(ListElement<T> nextLe){
|
||||
next = nextLe;
|
||||
}
|
||||
|
||||
public ListElement<T> getNext(){
|
||||
return this.next;
|
||||
}
|
||||
|
||||
public void setValue(T newValue){
|
||||
value = newValue;
|
||||
|
||||
}
|
||||
|
||||
public T getValue(){
|
||||
return this.value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
|
||||
public class ListIndexOutOfBoundsException extends IndexOutOfBoundsException{
|
||||
|
||||
int faulty_index;
|
||||
|
||||
ListIndexOutOfBoundsException(){
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,54 @@
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
||||
public class UseList {
|
||||
|
||||
public static void main(String [] args){
|
||||
ListElement<Integer> le1 = new ListElement<Integer>(10, null);
|
||||
List<Integer> list = new List<Integer>(le1);
|
||||
ListElement<Integer> le2 = new ListElement<Integer>(20, null);
|
||||
ListElement<Integer> le3 = new ListElement<Integer>(30, null);
|
||||
ListElement<Integer> le4 = new ListElement<Integer>(40, null);
|
||||
|
||||
list.insert(le2);
|
||||
list.insert(le3);
|
||||
|
||||
list.show();
|
||||
|
||||
//System.out.println("Laenge: " + list.length());
|
||||
//list.delete();
|
||||
//list.show();
|
||||
|
||||
//list.insertIndex(le4, 1);
|
||||
//list.show();
|
||||
|
||||
Scanner scan = new Scanner(System.in);
|
||||
|
||||
while(true){
|
||||
try{
|
||||
System.out.print("Gebe eine positive Zahl ein: ");
|
||||
int index = Integer.parseInt(scan.next());
|
||||
list.insertIndex(le4, index);
|
||||
list.show();
|
||||
break;
|
||||
}
|
||||
|
||||
catch(NumberFormatException e){
|
||||
System.err.println("Keine ganze Zahl eingegeben!");
|
||||
continue;
|
||||
}
|
||||
|
||||
catch(InputMismatchException e){
|
||||
//bei parser nutzen
|
||||
System.err.println("Keine ganze Zahl eingegeben!");
|
||||
continue;
|
||||
}
|
||||
|
||||
catch(NullPointerException e){
|
||||
System.err.println("index blöd sein");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
public void insert(ListElement<T> newElem, int i) {
|
||||
if (i == 0)
|
||||
insert(newElem);
|
||||
else {
|
||||
ListElement<T> tmp = head;
|
||||
ListElement<T> prev = null;
|
||||
int l = 0;
|
||||
// Aufsuchen des i-ten Elements
|
||||
while (l < i) {
|
||||
prev = tmp;
|
||||
tmp = prev.next;
|
||||
l++;
|
||||
} // l == i ist erreicht
|
||||
prev.next = newElem;
|
||||
newElem.next = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user