-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.java
39 lines (29 loc) · 688 Bytes
/
Stack.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.faisal;
import java.util.Arrays;
public class Stack {
private int[] items = new int[5];
private int count;
public void push(int item) {
if (count == items.length)
throw new StackOverflowError();
items[count++] = item;
}
public int pop() {
if (count == 0)
throw new IllegalStateException();
return items[--count];
}
public int peek() {
if (count == 0)
throw new IllegalStateException();
return items[count - 1];
}
public boolean isEmpty() {
return count == 0;
}
@Override
public String toString() {
var content = Arrays.copyOfRange(items, 0, count);
return Arrays.toString(content);
}
}