Skip to content

Commit 47fe0bd

Browse files
committed
Added an implementation for a stack/queue in Java.
Because @joyhsu0504 would probably find this helpful. - Added Java .class files to .gitignore. - Added Stack and related tests. - Added Queue and related tests.
1 parent ad7cbb6 commit 47fe0bd

File tree

5 files changed

+151
-0
lines changed

5 files changed

+151
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
*.o
2+
*.class

java/Queue/Queue.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.util.ArrayList;
2+
3+
class EmptyQueueException extends IndexOutOfBoundsException {
4+
public EmptyQueueException() {
5+
super("Queue is empty.");
6+
}
7+
}
8+
9+
class Queue<T> {
10+
private ArrayList<T> list;
11+
12+
public Queue() {
13+
this.list = new ArrayList<T>();
14+
}
15+
16+
public void enqueue(T item) {
17+
this.list.add(item);
18+
}
19+
20+
public T dequeue() {
21+
if (this.size() == 0) {
22+
throw new EmptyQueueException();
23+
}
24+
25+
return this.list.remove(0);
26+
}
27+
28+
public T peek() {
29+
if (this.size() == 0) {
30+
throw new EmptyQueueException();
31+
}
32+
33+
return this.list.get(0);
34+
}
35+
36+
public boolean isEmpty() {
37+
return this.size() == 0;
38+
}
39+
40+
public int size() {
41+
return this.list.size();
42+
}
43+
}

java/Queue/QueueTester.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class QueueTester {
2+
public static void main(String[] args) {
3+
Queue<Integer> queue = new Queue<Integer>();
4+
5+
for (int i = 5; i >= 0; i--) {
6+
queue.enqueue(i);
7+
System.out.println("Enqueued " + i);
8+
}
9+
10+
System.out.println("Size: " + queue.size());
11+
System.out.println("Top: " + queue.peek());
12+
13+
while (queue.size() > 0) {
14+
System.out.println(queue.dequeue());
15+
}
16+
17+
System.out.println("Size: " + queue.size());
18+
System.out.print("Top: ");
19+
20+
try {
21+
System.out.println(queue.peek());
22+
} catch (EmptyQueueException e) {
23+
System.out.println(e.toString());
24+
}
25+
26+
try {
27+
System.out.println(queue.dequeue());
28+
} catch (EmptyQueueException e) {
29+
System.out.println(e.toString());
30+
}
31+
}
32+
}

java/Stack/Stack.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.util.ArrayList;
2+
3+
class EmptyStackException extends IndexOutOfBoundsException {
4+
public EmptyStackException() {
5+
super("Stack is empty.");
6+
}
7+
}
8+
9+
class Stack<T> {
10+
private ArrayList<T> list;
11+
12+
public Stack() {
13+
this.list = new ArrayList<T>();
14+
}
15+
16+
public void push(T item) {
17+
this.list.add(item);
18+
}
19+
20+
public T pop() {
21+
if (this.size() == 0) {
22+
throw new EmptyStackException();
23+
}
24+
25+
return this.list.remove(this.size() - 1);
26+
}
27+
28+
public T peek() {
29+
if (this.size() == 0) {
30+
throw new EmptyStackException();
31+
}
32+
33+
return this.list.get(0);
34+
}
35+
36+
public boolean isEmpty() {
37+
return this.size() == 0;
38+
}
39+
40+
public int size() {
41+
return this.list.size();
42+
}
43+
}

java/Stack/StackTester.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class StackTester {
2+
public static void main(String[] args) {
3+
Stack<Integer> stack = new Stack<Integer>();
4+
5+
for (int i = 5; i >= 0; i--) {
6+
stack.push(i);
7+
System.out.println("Pushed " + i);
8+
}
9+
10+
System.out.println("Size: " + stack.size());
11+
System.out.println("Top: " + stack.peek());
12+
13+
while (stack.size() > 0) {
14+
System.out.println(stack.pop());
15+
}
16+
17+
System.out.println("Size: " + stack.size());
18+
System.out.print("Top: ");
19+
20+
try {
21+
System.out.println(stack.peek());
22+
} catch (EmptyStackException e) {
23+
System.out.println(e.toString());
24+
}
25+
26+
try {
27+
System.out.println(stack.pop());
28+
} catch (EmptyStackException e) {
29+
System.out.println(e.toString());
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)