-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
60 lines (51 loc) · 1.19 KB
/
Stack.java
File metadata and controls
60 lines (51 loc) · 1.19 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Stack implementation in Java built on the Array data structure
*
* Operations Supported:
* 1. Push (add an element to the top)
* 2. Pop (remove the top element)
* 3. Peek (view the top element without removing it)
* 4. Size (get the current size of the stack)
* 5. Display (view all elements in the stack)
*
*/
package Stack;
import java.util.Arrays;
public class Stack<T> {
private int size;
private int capacity;
private Object[] data;
public Stack() {
this.size = 0;
this.capacity = 16;
this.data = new Object[capacity];
}
public void push(T item) {
if (size == capacity) {
capacity *= 2;
data = Arrays.copyOf(data, capacity);
}
data[size++] = item;
}
public void pop() {
if (size == 0) {
System.out.println("Stack is empty");
return;
}
data[--size] = null;
}
@SuppressWarnings("unchecked")
public T peek() {
if (size == 0) {
System.out.println("Stack is empty");
return null;
}
return (T) data[size - 1];
}
public int size() {
return size;
}
public void display() {
System.out.println("Stack elements: " + Arrays.toString(Arrays.copyOfRange(data, 0, size)));
}
}