Skip to content
Merged
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
71 changes: 71 additions & 0 deletions contents/stacks_and_queues/code/java/Queue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import java.util.List;
import java.util.ArrayList;

public class QueueTest {

public static void main(String[] args) {
IQueue<Integer> intQueue = new Queue<>();

intQueue.enqueue(4);
intQueue.enqueue(5);
intQueue.enqueue(9);

System.out.println(intQueue.dequeue());
System.out.println(intQueue.size());
System.out.println(intQueue.front());
}

}


interface IQueue<T> {

/*
* 'dequeue' removes the first element from the queue and returns it
*/
T dequeue();

/*
* 'enqueue' adds an element at the end of the queue and returns the new size
*/
int enqueue(T element);


/*
* 'size' returns the size of the queue
*/
int size();

/*
* 'front' returns the first element of the queue without removing it
*/
T front();
}


public class Queue<T> implements IQueue<T> {

private List<T> list;

public Queue() {
this.list = new ArrayList<>();
}

public T dequeue() {
return this.list.remove(0);
}

public int enqueue(T element) {
this.list.add(element);
return this.size();
}

public int size() {
return this.list.size();
}

public T front() {
return this.list.get(0);
}

}
72 changes: 72 additions & 0 deletions contents/stacks_and_queues/code/java/Stack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import java.util.List;
import java.util.ArrayList;


public class StackTest {

public static void main(String[] args) {
IStack<Integer> intStack = new Stack<>();

intStack.push(4);
intStack.push(5);
intStack.push(9);

System.out.println(intStack.pop());
System.out.println(intStack.size());
System.out.println(intStack.top());
}

}


interface IStack<T> {
/*
* 'pop' removed the last element from the stack and returns it
*/
T pop();

/*
* 'push' adds an element to at the end of the stack and returns the new size
*/
int push(T element);

/*
* 'size' returns the length of the stack
*/
int size();

/*
* 'top' returns the first element of the stack
*/
T top();
}


public class Stack<T> implements IStack<T> {

private List<T> list;

public Stack() {
this.list = new ArrayList<>();
}

public T pop() {
return this.list.remove(this.size() - 1);
}

public int push(T element) {
this.list.add(element);
return this.size();
}

public int size() {
return this.list.size();
}

public T top() {
return this.list.get(this.size() - 1);
}

}


4 changes: 4 additions & 0 deletions contents/stacks_and_queues/stacks_and_queues.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ Here is a simple implementation of a stack:
{% method %}
{% sample lang="ts" %}
[import, lang:"typescript"](code/typescript/stack.ts)
{% sample lang="java" %}
[import, lang:"java"](code/java/Stack.java)
{% endmethod %}

Here is a simple implementation of a queue:
{% method %}
{% sample lang="ts" %}
[import, lang:"typescript"](code/typescript/queue.ts)
{% sample lang="java" %}
[import, lang:"java" ](code/java/Queue.java)
{% endmethod %}


Expand Down