diff --git a/Java/DequeExample.java b/Java/DequeExample.java new file mode 100644 index 0000000..76fbdc7 --- /dev/null +++ b/Java/DequeExample.java @@ -0,0 +1,123 @@ +package Java; + +import java.util.Arrays; + +public class DequeExample { + + static class Deque { + + private Element first; + + private Element last; + + private int count; + + public void push(final String value) { + final Element element = new Element(last, null, value); + last = element; + count++; + if (count == 1) { + first = last; + } else { + last.prev.next = element; + } + + } + + public String pop() { + if (count == 0) { + throw new NullPointerException("Deque is empty"); + } + final Element result = last; + last = last.prev; + count--; + if (count == 0 || count == 1) { + first = last; + } else { + last.next = null; + } + return result.value; + } + + public void unshift(final String value) { + final Element element = new Element(null, first, value); + first = element; + count++; + if (count == 1) { + last = first; + } else { + first.next.prev = element; + } + } + + public String shift() { + if (count == 0) { + throw new NullPointerException("Deque is empty"); + } + final Element result = first; + first = first.next; + count--; + if (count == 0 || count == 1) { + last = first; + } else { + first.prev = null; + } + return result.value; + } + + public String[] asArray() { + final String[] result = new String[count]; + Element current = first; + for (int i = 0; i < count; i++) { + result[i] = current.value; + current = current.next; + } + return result; + } + + + private static class Element { + + private Element prev; + + private Element next; + + private final String value; + + private Element(final Element prev, + final Element next, + final String value) { + this.prev = prev; + this.next = next; + this.value = value; + } + } + } + + // Usage + public static void main(final String[] args) { + final Deque list = new Deque(); + list.push("first"); + list.push("second"); + list.push("third"); + + String[] array = list.asArray(); + System.out.println(Arrays.toString(array)); + + System.out.println(list.pop()); + System.out.println(list.pop()); + System.out.println(list.pop()); + + list.unshift("first"); + list.unshift("second"); + list.unshift("third"); + + array = list.asArray(); + System.out.println(Arrays.toString(array)); + + System.out.println(list.shift()); + System.out.println(list.shift()); + System.out.println(list.shift()); + //System.out.println(list.unshift()); + } +} diff --git a/Java/QueueExample.java b/Java/QueueExample.java new file mode 100644 index 0000000..7025189 --- /dev/null +++ b/Java/QueueExample.java @@ -0,0 +1,82 @@ +package Java; + +import java.util.Arrays; + +/** + * @author Michael Balakhon + * @link t.me/mibal_ua. + */ +public class QueueExample { + + static class Queue { + + private Element first; + + private Element last; + + private int count; + + public void put(final String value) { + final Element element = new Element(first, value); + if (count == 0) { + first = element; + } else { + last.next = element; + } + last = element; + count++; + } + + public String pick() { + if (count == 0) { + throw new NullPointerException("Queue is empty"); + } + final Element result = first; + first = first.next; + count--; + if (count == 0 || count == 1) { + last = first; + } + return result.value; + } + + public String[] asArray() { + final String[] result = new String[count]; + Element current = first; + for (int j = 0; j < count; j++) { + result[j] = current.value; + current = current.next; + } + return result; + } + + + private static class Element { + + private Element next; + + private final String value; + + public Element(final Element next, final String value) { + this.next = next; + this.value = value; + } + } + } + + // Usage + public static void main(final String[] args) { + final Queue queue = new Queue(); + queue.put("first"); + queue.put("second"); + queue.put("third"); + + String[] array = queue.asArray(); + System.out.println(Arrays.toString(array)); + + System.out.println(queue.pick()); + System.out.println(queue.pick()); + System.out.println(queue.pick()); + //System.out.println(queue.pick()); + } +} diff --git a/Java/StackExample.java b/Java/StackExample.java new file mode 100644 index 0000000..9585287 --- /dev/null +++ b/Java/StackExample.java @@ -0,0 +1,67 @@ +package Java; + +import java.util.Arrays; + +public class StackExample { + + static class Stack { + + private Element last; + + private int count; + + public void push(final String value) { + last = new Element(last, value); + count++; + } + + public String pop() { + if (count == 0) { + throw new NullPointerException("Stack is empty"); + } + final Element result = last; + last = last.prev; + count--; + return result.value; + } + + public String[] asArray() { + final String[] result = new String[count]; + Element current = last; + for (int i = count - 1; i >= 0; i--) { + result[i] = current.value; + current = current.prev; + } + return result; + } + + + private static class Element { + + private final Element prev; + + private final String value; + + public Element(final Element prev, final String value) { + this.prev = prev; + this.value = value; + } + } + } + + // Usage + public static void main(final String[] args) { + final Stack stack = new Stack(); + stack.push("first"); + stack.push("second"); + stack.push("third"); + + String[] array = stack.asArray(); + System.out.println(Arrays.toString(array)); + + System.out.println(stack.pop()); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + //System.out.println(stack.pop()); + } +}