diff --git a/2Semester/DynamicArray/.classpath b/2Semester/DynamicArray/.classpath new file mode 100644 index 0000000..f029363 --- /dev/null +++ b/2Semester/DynamicArray/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/2Semester/DynamicArray/.project b/2Semester/DynamicArray/.project new file mode 100644 index 0000000..950e723 --- /dev/null +++ b/2Semester/DynamicArray/.project @@ -0,0 +1,17 @@ + + + DynamicArray + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/2Semester/DynamicArray/.settings/org.eclipse.core.resources.prefs b/2Semester/DynamicArray/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/2Semester/DynamicArray/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/2Semester/DynamicArray/.settings/org.eclipse.jdt.core.prefs b/2Semester/DynamicArray/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..062ed70 --- /dev/null +++ b/2Semester/DynamicArray/.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=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 diff --git a/2Semester/DynamicArray/bin/DynamicArray.class b/2Semester/DynamicArray/bin/DynamicArray.class new file mode 100644 index 0000000..359b6a6 Binary files /dev/null and b/2Semester/DynamicArray/bin/DynamicArray.class differ diff --git a/2Semester/DynamicArray/bin/DynamicArrayDemo.class b/2Semester/DynamicArray/bin/DynamicArrayDemo.class new file mode 100644 index 0000000..793a714 Binary files /dev/null and b/2Semester/DynamicArray/bin/DynamicArrayDemo.class differ diff --git a/2Semester/DynamicArray/src/DynamicArray.java b/2Semester/DynamicArray/src/DynamicArray.java new file mode 100644 index 0000000..cea877a --- /dev/null +++ b/2Semester/DynamicArray/src/DynamicArray.java @@ -0,0 +1,88 @@ +import java.lang.reflect.Array; + +public class DynamicArray { + Class typeOfElement; + private E array[]; + private int count; + private int capacity; + private int iterator; + + public DynamicArray(Class typeOfElement) + { + this(typeOfElement, 10); + } + + @SuppressWarnings("unchecked") + public DynamicArray(Class 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; + } +} diff --git a/2Semester/DynamicArray/src/DynamicArrayDemo.java b/2Semester/DynamicArray/src/DynamicArrayDemo.java new file mode 100644 index 0000000..1841f65 --- /dev/null +++ b/2Semester/DynamicArray/src/DynamicArrayDemo.java @@ -0,0 +1,19 @@ + +public class DynamicArrayDemo { + public static void main(String[] args) { + // TODO Auto-generated method stub + DynamicArray myArray = new DynamicArray(String.class); + + myArray.add("Test1"); + myArray.add("Test2"); + myArray.add("Test3"); + + myArray.remove("Test2"); + + myArray.iteratorInit(); + + while(myArray.hasNext()) { + System.out.println(myArray.next()); + } + } +} diff --git a/2Semester/DynamicHashTable/.classpath b/2Semester/DynamicHashTable/.classpath new file mode 100644 index 0000000..f029363 --- /dev/null +++ b/2Semester/DynamicHashTable/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/2Semester/DynamicHashTable/.project b/2Semester/DynamicHashTable/.project new file mode 100644 index 0000000..aecb398 --- /dev/null +++ b/2Semester/DynamicHashTable/.project @@ -0,0 +1,17 @@ + + + DynamicHashTable + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/2Semester/DynamicHashTable/.settings/org.eclipse.core.resources.prefs b/2Semester/DynamicHashTable/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/2Semester/DynamicHashTable/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/2Semester/DynamicHashTable/.settings/org.eclipse.jdt.core.prefs b/2Semester/DynamicHashTable/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..062ed70 --- /dev/null +++ b/2Semester/DynamicHashTable/.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=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 diff --git a/2Semester/DynamicHashTable/bin/DynamicArray.class b/2Semester/DynamicHashTable/bin/DynamicArray.class new file mode 100644 index 0000000..359b6a6 Binary files /dev/null and b/2Semester/DynamicHashTable/bin/DynamicArray.class differ diff --git a/2Semester/DynamicHashTable/bin/DynamicExtendableHashTable.class b/2Semester/DynamicHashTable/bin/DynamicExtendableHashTable.class new file mode 100644 index 0000000..6e22433 Binary files /dev/null and b/2Semester/DynamicHashTable/bin/DynamicExtendableHashTable.class differ diff --git a/2Semester/DynamicHashTable/bin/DynamicHashTable.class b/2Semester/DynamicHashTable/bin/DynamicHashTable.class new file mode 100644 index 0000000..77e429e Binary files /dev/null and b/2Semester/DynamicHashTable/bin/DynamicHashTable.class differ diff --git a/2Semester/DynamicHashTable/bin/DynamicHashTableDemo.class b/2Semester/DynamicHashTable/bin/DynamicHashTableDemo.class new file mode 100644 index 0000000..4847531 Binary files /dev/null and b/2Semester/DynamicHashTable/bin/DynamicHashTableDemo.class differ diff --git a/2Semester/DynamicHashTable/bin/DynamicList$ListElement.class b/2Semester/DynamicHashTable/bin/DynamicList$ListElement.class new file mode 100644 index 0000000..64464a1 Binary files /dev/null and b/2Semester/DynamicHashTable/bin/DynamicList$ListElement.class differ diff --git a/2Semester/DynamicHashTable/bin/DynamicList.class b/2Semester/DynamicHashTable/bin/DynamicList.class new file mode 100644 index 0000000..b22505d Binary files /dev/null and b/2Semester/DynamicHashTable/bin/DynamicList.class differ diff --git a/2Semester/DynamicHashTable/src/DynamicArray.java b/2Semester/DynamicHashTable/src/DynamicArray.java new file mode 100644 index 0000000..cea877a --- /dev/null +++ b/2Semester/DynamicHashTable/src/DynamicArray.java @@ -0,0 +1,88 @@ +import java.lang.reflect.Array; + +public class DynamicArray { + Class typeOfElement; + private E array[]; + private int count; + private int capacity; + private int iterator; + + public DynamicArray(Class typeOfElement) + { + this(typeOfElement, 10); + } + + @SuppressWarnings("unchecked") + public DynamicArray(Class 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; + } +} diff --git a/2Semester/DynamicHashTable/src/DynamicExtendableHashTable.java b/2Semester/DynamicHashTable/src/DynamicExtendableHashTable.java new file mode 100644 index 0000000..aa5f303 --- /dev/null +++ b/2Semester/DynamicHashTable/src/DynamicExtendableHashTable.java @@ -0,0 +1,48 @@ +import java.lang.reflect.Array; + +public class DynamicExtendableHashTable { + private Class typeOfElement; + private int capacity; + private DynamicList[] hashTable; + + @SuppressWarnings("unchecked") + public DynamicExtendableHashTable(Class typeOfElement, int capacity) + { + this.typeOfElement = typeOfElement; + this.capacity = capacity; + hashTable = (DynamicList[]) Array.newInstance(DynamicList.class, this.capacity); + } + + public DynamicExtendableHashTable(Class typeOfElement) + { + this(typeOfElement, 100); + } + + public void add(int key, E e) + { + if(hashTable[hashFunction(key)] == null) { + + hashTable[hashFunction(key)] = new DynamicList(); + } + + hashTable[hashFunction(key)].add(e); + } + + public DynamicArray get(int key) + { + DynamicArray array = new DynamicArray(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; + } +} diff --git a/2Semester/DynamicHashTable/src/DynamicHashTable.java b/2Semester/DynamicHashTable/src/DynamicHashTable.java new file mode 100644 index 0000000..f1a3e3c --- /dev/null +++ b/2Semester/DynamicHashTable/src/DynamicHashTable.java @@ -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; + } +} diff --git a/2Semester/DynamicHashTable/src/DynamicHashTableDemo.java b/2Semester/DynamicHashTable/src/DynamicHashTableDemo.java new file mode 100644 index 0000000..8acd02b --- /dev/null +++ b/2Semester/DynamicHashTable/src/DynamicHashTableDemo.java @@ -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 dynTable2 = new DynamicExtendableHashTable(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 a = new DynamicArray(String.class); + + a = dynTable2.get(keys[i]); + + a.iteratorInit(); + + while(a.hasNext()) { + System.out.print(a.next() + " "); + } + + System.out.println(); + } + + } + +} diff --git a/2Semester/DynamicHashTable/src/DynamicList.java b/2Semester/DynamicHashTable/src/DynamicList.java new file mode 100644 index 0000000..65d4301 --- /dev/null +++ b/2Semester/DynamicHashTable/src/DynamicList.java @@ -0,0 +1,76 @@ + +public class DynamicList { + private class ListElement + { + E content; + ListElement pred; + ListElement succ; + } + private ListElement head; + private ListElement tail; + private ListElement 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 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 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; + } +} diff --git a/2Semester/DynamicList/.classpath b/2Semester/DynamicList/.classpath new file mode 100644 index 0000000..f029363 --- /dev/null +++ b/2Semester/DynamicList/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/2Semester/DynamicList/.project b/2Semester/DynamicList/.project new file mode 100644 index 0000000..2f97904 --- /dev/null +++ b/2Semester/DynamicList/.project @@ -0,0 +1,17 @@ + + + DynamicList + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/2Semester/DynamicList/.settings/org.eclipse.core.resources.prefs b/2Semester/DynamicList/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/2Semester/DynamicList/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/2Semester/DynamicList/.settings/org.eclipse.jdt.core.prefs b/2Semester/DynamicList/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..062ed70 --- /dev/null +++ b/2Semester/DynamicList/.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=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 diff --git a/2Semester/DynamicList/bin/DynamicList$ListElement.class b/2Semester/DynamicList/bin/DynamicList$ListElement.class new file mode 100644 index 0000000..64464a1 Binary files /dev/null and b/2Semester/DynamicList/bin/DynamicList$ListElement.class differ diff --git a/2Semester/DynamicList/bin/DynamicList.class b/2Semester/DynamicList/bin/DynamicList.class new file mode 100644 index 0000000..b22505d Binary files /dev/null and b/2Semester/DynamicList/bin/DynamicList.class differ diff --git a/2Semester/DynamicList/bin/DynamicListDemo.class b/2Semester/DynamicList/bin/DynamicListDemo.class new file mode 100644 index 0000000..04e11d1 Binary files /dev/null and b/2Semester/DynamicList/bin/DynamicListDemo.class differ diff --git a/2Semester/DynamicList/src/DynamicList.java b/2Semester/DynamicList/src/DynamicList.java new file mode 100644 index 0000000..65d4301 --- /dev/null +++ b/2Semester/DynamicList/src/DynamicList.java @@ -0,0 +1,76 @@ + +public class DynamicList { + private class ListElement + { + E content; + ListElement pred; + ListElement succ; + } + private ListElement head; + private ListElement tail; + private ListElement 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 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 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; + } +} diff --git a/2Semester/DynamicList/src/DynamicListDemo.java b/2Semester/DynamicList/src/DynamicListDemo.java new file mode 100644 index 0000000..f550f02 --- /dev/null +++ b/2Semester/DynamicList/src/DynamicListDemo.java @@ -0,0 +1,20 @@ + +public class DynamicListDemo { + + public static void main(String[] args) { + DynamicList dynList = new DynamicList(); + + dynList.add("Test1"); + dynList.add("Test2"); + dynList.add("Test3"); + + dynList.remove("Test2"); + + dynList.iteratorInit(); + + while(dynList.hasNext()) { + System.out.println(dynList.next()); + } + } + +}