Skip to content

Instantly share code, notes, and snippets.

@kindlich
Created July 16, 2019 06:45
Show Gist options
  • Save kindlich/3a46138c1eb489d6d5f12e110daeb12b to your computer and use it in GitHub Desktop.
Save kindlich/3a46138c1eb489d6d5f12e110daeb12b to your computer and use it in GitHub Desktop.
package de.dhbwka.java.exercise.datastructures;
public class KellerArray<T> {
private final int maxSize;
private final Object[] data;
private int position;
public KellerArray(int maxSize) {
this.data = new Object[maxSize];
this.maxSize = maxSize;
this.position = -1;
}
public static void main(String[] args) {
final KellerArray<Integer> integerKeller = new KellerArray<>(10);
for(int i = 0; i < 10; i++) {
integerKeller.add(i);
}
System.out.println("List: " + integerKeller);
System.out.println("KellerLinked: " + integerKeller.size());
System.out.println();
System.out.println();
for(int i = 0; i < 5; i++) {
System.out.println(integerKeller.pop());
}
for(int i = 10; i < 15; i++) {
integerKeller.add(i);
}
for(int i = 0; i < 10; i++) {
System.out.println(integerKeller.pop());
}
System.out.println("List: " + integerKeller);
}
public void add(T e) {
if(position + 1 == maxSize) {
throw new IllegalStateException("KellerLinked Full");
}
position++;
data[position] = e;
}
@SuppressWarnings("unchecked")
public T pop() {
if(size() == 0) {
throw new IllegalStateException("KellerLinked Empty");
}
return (T) data[position--];
}
@SuppressWarnings("unchecked")
public T peek() {
if(size() == 0) {
throw new IllegalStateException("KellerLinked Empty");
}
return (T) data[position];
}
public int size() {
return position + 1;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder(position == -1 ? "*[" : "[");
for(int i = 0; i < data.length; i++) {
final boolean remainder = i > position;
if(remainder)
sb.append("(");
if(i == position)
sb.append("*");
sb.append(data[i]);
if(remainder)
sb.append(")");
if(i < data.length - 1)
sb.append(", ");
}
return sb.append("]").toString();
}
}
package de.dhbwka.java.exercise.datastructures;
import java.util.*;
public class KellerLinked<T> {
private KellerElement<T> first = null;
public static void main(String[] args) {
final KellerLinked<Integer> integerKeller = new KellerLinked<>();
for(int i = 0; i < 10; i++) {
integerKeller.add(i);
}
System.out.println("List: " + integerKeller);
System.out.println("KellerLinked: " + integerKeller.size());
System.out.println();
System.out.println();
for(int i = 0; i < 5; i++) {
System.out.println(integerKeller.pop());
}
for(int i = 10; i < 15; i++) {
integerKeller.add(i);
}
for(int i = 0; i < 10; i++) {
System.out.println(integerKeller.pop());
}
System.out.println("List: " + integerKeller);
}
@Override
public String toString() {
return "[" + first + "]";
}
public T peek() {
if(first == null)
throw new IllegalStateException("KellerLinked Empty");
return first.data;
}
public T pop() {
if(first == null)
throw new IllegalStateException("KellerLinked empty");
final T e = first.data;
first = first.next;
return e;
}
public void add(T e) {
final KellerElement<T> kellerElement = new KellerElement<>(e);
kellerElement.next = first;
first = kellerElement;
}
public int size() {
KellerElement<T> kellerElement = first;
int size = 0;
while(kellerElement != null) {
size++;
kellerElement = kellerElement.next;
}
return size;
}
private static class KellerElement<T> {
final T data;
KellerElement<T> next = null;
private KellerElement(T data) {
this.data = data;
}
@Override
public String toString() {
//return String.format(Locale.ENGLISH, "[Data: %s, next -> %s]", data, next);
//return String.format(Locale.ENGLISH, "[%s:%s]", data, next);
return String.format(Locale.ENGLISH, "%s:%s", data, next);
}
}
}
package de.dhbwka.java.exercise.datastructures;
public class SchlangeArray<T> {
private final Object[] data;
private final int maxSize;
private int startPosition;
private int currentSize;
public SchlangeArray(int maxSize) {
this.maxSize = maxSize;
this.data = new Object[maxSize];
this.startPosition = 0;
this.currentSize = 0;
}
public static void main(String[] args) {
final SchlangeArray<Integer> intSchlange = new SchlangeArray<>(10);
for(int i = 0; i < 10; i++) {
intSchlange.add(i);
}
System.out.println("List: " + intSchlange);
System.out.println("KellerLinked: " + intSchlange.size());
System.out.println();
System.out.println();
for(int i = 0; i < 5; i++) {
System.out.println(intSchlange.pop());
}
for(int i = 10; i < 15; i++) {
intSchlange.add(i);
}
for(int i = 0; i < 10; i++) {
System.out.println(intSchlange.pop());
}
System.out.println("List: " + intSchlange);
final SchlangeArray<String> schlangeArray = new SchlangeArray<>(5);
schlangeArray.add("A");
System.out.println(schlangeArray);
schlangeArray.add("B");
System.out.println(schlangeArray);
schlangeArray.add("C");
System.out.println(schlangeArray);
schlangeArray.add("D");
System.out.println(schlangeArray);
schlangeArray.pop();
System.out.println(schlangeArray);
schlangeArray.pop();
System.out.println(schlangeArray);
schlangeArray.add("E");
System.out.println(schlangeArray);
schlangeArray.add("F");
System.out.println(schlangeArray);
}
public void add(T e) {
final int arrayPosition = ((startPosition + currentSize) % maxSize);
if(arrayPosition == startPosition && currentSize != 0) {
throw new IllegalStateException("Schlange full!");
}
data[arrayPosition] = e;
currentSize++;
}
@SuppressWarnings("unchecked")
public T pop() {
if(this.currentSize == 0)
throw new IllegalStateException("Schlange empty!");
this.currentSize--;
final int pos = startPosition % maxSize;
this.startPosition = ++startPosition % maxSize;
return (T) this.data[pos];
}
@SuppressWarnings("unchecked")
public T peek() {
if(currentSize == 0)
throw new IllegalStateException("Schlange empty!");
return (T) data[startPosition];
}
public int size() {
return this.currentSize;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("[");
for(int i = 0; i < this.data.length; i++) {
final boolean remainder = i < startPosition && (startPosition + currentSize <= maxSize || i > (startPosition + currentSize - 1) % maxSize) || (i >= (startPosition + currentSize));
if(i == this.startPosition)
sb.append("*");
if(remainder)
sb.append("(");
sb.append(this.data[i]);
if(remainder)
sb.append(")");
if(i < this.data.length - 1)
sb.append(", ");
}
return sb.append("], size ").append(currentSize).toString();
}
}
package de.dhbwka.java.exercise.datastructures;
import java.util.*;
public class SchlangeLinked<T> {
private SchlangenElement<T> first, last;
public static void main(String[] args) {
final SchlangeLinked<Integer> schlangeLinked = new SchlangeLinked<>();
for(int i = 0; i < 10; i++) {
schlangeLinked.add(i);
}
System.out.println("List: " + schlangeLinked);
System.out.println("KellerLinked: " + schlangeLinked.size());
System.out.println();
System.out.println();
for(int i = 0; i < 5; i++) {
System.out.println(schlangeLinked.pop());
}
System.out.println(schlangeLinked);
for(int i = 10; i < 15; i++) {
schlangeLinked.add(i);
}
System.out.println(schlangeLinked);
for(int i = 0; i < 10; i++) {
System.out.println(schlangeLinked.pop());
}
System.out.println("List: " + schlangeLinked);
}
public void add(T e) {
final SchlangenElement<T> element = new SchlangenElement<>(e);
if(last == null) {
first = last = element;
return;
}
last.next = element;
last = element;
}
public T pop() {
if(first == null)
throw new IllegalStateException("Schlange empty");
final T data = first.data;
first = first.next;
if(first == null)
last = null;
return data;
}
public T peek() {
if(first == null)
throw new IllegalStateException("Schlange empty");
return first.data;
}
@Override
public String toString() {
return "[" + first + "]";
}
public int size() {
SchlangenElement<T> element = first;
int size = 0;
while(element != null) {
size++;
element = element.next;
}
return size;
}
private static class SchlangenElement<T> {
private final T data;
private SchlangenElement<T> next;
private SchlangenElement(T data) {
this.data = data;
this.next = null;
}
@Override
public String toString() {
return String.format(Locale.ENGLISH, "%s:%s", data, next);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment