Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Java queue, stack, deque examples wits usage #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions Java/DequeExample.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
82 changes: 82 additions & 0 deletions Java/QueueExample.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
67 changes: 67 additions & 0 deletions Java/StackExample.java
Original file line number Diff line number Diff line change
@@ -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());
}
}