Marmitt Dynamic Strukturen

master
ifembanefo 2023-05-30 16:32:12 +02:00
parent bb5113390b
commit 93b8c42c6d
32 changed files with 624 additions and 0 deletions

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-19">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>DynamicArray</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,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8

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=19
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=19
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=19

Binary file not shown.

View File

@ -0,0 +1,88 @@
import java.lang.reflect.Array;
public class DynamicArray<E> {
Class<E> typeOfElement;
private E array[];
private int count;
private int capacity;
private int iterator;
public DynamicArray(Class<E> typeOfElement)
{
this(typeOfElement, 10);
}
@SuppressWarnings("unchecked")
public DynamicArray(Class<E> typeOfElement, int capacity)
{
this.typeOfElement = typeOfElement;
count = 0;
this.capacity = capacity;
array = (E[]) Array.newInstance(typeOfElement, this.capacity);
}
public void iteratorInit()
{
iterator = 0;
}
public boolean hasNext()
{
return iterator < count;
}
public E next()
{
E e = array[iterator];
iterator++;
return e;
}
public int count()
{
return count;
}
public E get(int index)
{
return array[index];
}
@SuppressWarnings("unchecked")
public void add(E e)
{
if(count == capacity - 1)
{
int newCapacity = capacity * 2;
E newArray[] = (E[]) Array.newInstance(typeOfElement, this.capacity);
for(int i = 0; i < capacity; i++)
{
newArray[i] = array[i];
}
array = newArray;
capacity = newCapacity;
}
array[count] = e;
count++;
}
public E remove(E e)
{
for(int i = 0; i < count; i++)
{
if(e == array[i])
{
int j = i + 1;
for(; j < count; j++)
array[j - 1] = array[j];
array[j - 1] = null;
count--;
}
}
return e;
}
}

View File

@ -0,0 +1,19 @@
public class DynamicArrayDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
DynamicArray<String> myArray = new DynamicArray<String>(String.class);
myArray.add("Test1");
myArray.add("Test2");
myArray.add("Test3");
myArray.remove("Test2");
myArray.iteratorInit();
while(myArray.hasNext()) {
System.out.println(myArray.next());
}
}
}

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-19">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>DynamicHashTable</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,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8

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=19
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=19
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=19

View File

@ -0,0 +1,88 @@
import java.lang.reflect.Array;
public class DynamicArray<E> {
Class<E> typeOfElement;
private E array[];
private int count;
private int capacity;
private int iterator;
public DynamicArray(Class<E> typeOfElement)
{
this(typeOfElement, 10);
}
@SuppressWarnings("unchecked")
public DynamicArray(Class<E> typeOfElement, int capacity)
{
this.typeOfElement = typeOfElement;
count = 0;
this.capacity = capacity;
array = (E[]) Array.newInstance(typeOfElement, this.capacity);
}
public void iteratorInit()
{
iterator = 0;
}
public boolean hasNext()
{
return iterator < count;
}
public E next()
{
E e = array[iterator];
iterator++;
return e;
}
public int count()
{
return count;
}
public E get(int index)
{
return array[index];
}
@SuppressWarnings("unchecked")
public void add(E e)
{
if(count == capacity - 1)
{
int newCapacity = capacity * 2;
E newArray[] = (E[]) Array.newInstance(typeOfElement, this.capacity);
for(int i = 0; i < capacity; i++)
{
newArray[i] = array[i];
}
array = newArray;
capacity = newCapacity;
}
array[count] = e;
count++;
}
public E remove(E e)
{
for(int i = 0; i < count; i++)
{
if(e == array[i])
{
int j = i + 1;
for(; j < count; j++)
array[j - 1] = array[j];
array[j - 1] = null;
count--;
}
}
return e;
}
}

View File

@ -0,0 +1,48 @@
import java.lang.reflect.Array;
public class DynamicExtendableHashTable<E> {
private Class<E> typeOfElement;
private int capacity;
private DynamicList<E>[] hashTable;
@SuppressWarnings("unchecked")
public DynamicExtendableHashTable(Class<E> typeOfElement, int capacity)
{
this.typeOfElement = typeOfElement;
this.capacity = capacity;
hashTable = (DynamicList<E>[]) Array.newInstance(DynamicList.class, this.capacity);
}
public DynamicExtendableHashTable(Class<E> typeOfElement)
{
this(typeOfElement, 100);
}
public void add(int key, E e)
{
if(hashTable[hashFunction(key)] == null) {
hashTable[hashFunction(key)] = new DynamicList<E>();
}
hashTable[hashFunction(key)].add(e);
}
public DynamicArray<E> get(int key)
{
DynamicArray<E> array = new DynamicArray<E>(typeOfElement);
hashTable[hashFunction(key)].iteratorInit();
while(hashTable[hashFunction(key)].hasNext()) {
array.add(hashTable[hashFunction(key)].next());
}
return array;
}
private int hashFunction(int key)
{
return key % capacity;
}
}

View File

@ -0,0 +1,37 @@
public class DynamicHashTable {
private int capacity;
private Object[] hashTable;
public DynamicHashTable(int capacity)
{
this.capacity = capacity;
hashTable = new Object[capacity];
}
public DynamicHashTable()
{
this(100);
}
public boolean add(int key, Object o)
{
if(hashTable[hashFunction(key)] == null) {
hashTable[hashFunction(key)] = o;
return true;
}
return false;
}
public Object get(int key)
{
return hashTable[hashFunction(key)];
}
private int hashFunction(int key)
{
return key % capacity;
}
}

View File

@ -0,0 +1,43 @@
public class DynamicHashTableDemo {
public static void main(String[] args) {
DynamicHashTable dynTable = new DynamicHashTable(5);
boolean a1 = dynTable.add(0, "Test1");
boolean a2 = dynTable.add(10, "Test2");
boolean a3 = dynTable.add(20, "Test3");
System.out.println("DynamicHashTable");
System.out.println(a1 + " " + dynTable.get(0));
System.out.println(a2 + " " + dynTable.get(10));
System.out.println(a3 + " " + dynTable.get(20));
DynamicExtendableHashTable<String> dynTable2 = new DynamicExtendableHashTable<String>(String.class, 5);
dynTable2.add(0, "Test1");
dynTable2.add(10, "Test2");
dynTable2.add(20, "Test3");
System.out.println("DynamicExtendableHashTable");
int keys[] = { 0, 10, 20 };
for(int i = 0; i < 3; i++)
{
DynamicArray<String> a = new DynamicArray<String>(String.class);
a = dynTable2.get(keys[i]);
a.iteratorInit();
while(a.hasNext()) {
System.out.print(a.next() + " ");
}
System.out.println();
}
}
}

View File

@ -0,0 +1,76 @@
public class DynamicList<E> {
private class ListElement<E>
{
E content;
ListElement<E> pred;
ListElement<E> succ;
}
private ListElement<E> head;
private ListElement<E> tail;
private ListElement<E> iterator;
public DynamicList()
{
head = null;
tail = null;
}
public void iteratorInit()
{
iterator = head;
}
public boolean hasNext()
{
return iterator != null;
}
public E next()
{
E e = iterator.content;
iterator = iterator.succ;
return e;
}
public void add(E element)
{
ListElement<E> le = new ListElement<>();
le.content = element;
le.pred = null;
le.succ = null;
if(head == null)
{
head = le;
tail = le;
}
else
{
le.pred = tail;
tail.succ = le;
tail = le;
}
}
public E remove(E element)
{
ListElement<E> i = head;
do
{
if(i.content == element)
{
i.pred.succ = i.succ;
i.succ.pred = i.pred;
break;
}
i = i.succ;
}
while(i != tail);
return element;
}
}

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-19">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>DynamicList</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,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8

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=19
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=19
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=19

Binary file not shown.

View File

@ -0,0 +1,76 @@
public class DynamicList<E> {
private class ListElement<E>
{
E content;
ListElement<E> pred;
ListElement<E> succ;
}
private ListElement<E> head;
private ListElement<E> tail;
private ListElement<E> iterator;
public DynamicList()
{
head = null;
tail = null;
}
public void iteratorInit()
{
iterator = head;
}
public boolean hasNext()
{
return iterator != null;
}
public E next()
{
E e = iterator.content;
iterator = iterator.succ;
return e;
}
public void add(E element)
{
ListElement<E> le = new ListElement<>();
le.content = element;
le.pred = null;
le.succ = null;
if(head == null)
{
head = le;
tail = le;
}
else
{
le.pred = tail;
tail.succ = le;
tail = le;
}
}
public E remove(E element)
{
ListElement<E> i = head;
do
{
if(i.content == element)
{
i.pred.succ = i.succ;
i.succ.pred = i.pred;
break;
}
i = i.succ;
}
while(i != tail);
return element;
}
}

View File

@ -0,0 +1,20 @@
public class DynamicListDemo {
public static void main(String[] args) {
DynamicList<String> dynList = new DynamicList<String>();
dynList.add("Test1");
dynList.add("Test2");
dynList.add("Test3");
dynList.remove("Test2");
dynList.iteratorInit();
while(dynList.hasNext()) {
System.out.println(dynList.next());
}
}
}