-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
110 lines (100 loc) · 3.17 KB
/
Stack.java
File metadata and controls
110 lines (100 loc) · 3.17 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.util.*;
/**
* A generic Stack implementation using Java's built-in {@code ArrayList<T>} collection
* The right-end of the internal ArrayList represents the top of the stack (LIFO).
* @author Taskin Saadman
*/
public class Stack<T> implements Iterable<T> {
private List<T> stack;
/**
* Constructs an empty stack.
*/
public Stack() {
stack = new ArrayList<T>();
}
/**
* Pushes an element onto the top of the stack.
*
* @param element the element to be pushed onto the stack
*/
public void push(T element) {
stack.add(element);
}
/**
* Removes and returns the element at the top of the stack.
*
* @return the element at the top of the stack
* @throws EmptyStackException if the stack is empty
*/
public T pop() throws EmptyStackException {
if (stack.size() == 0) throw new EmptyStackException();
T retVal = stack.get(stack.size() - 1);
stack.remove(stack.size() - 1);
return retVal;
}
/**
* Returns the number of elements in the stack.
*
* @return the number of elements in the stack
*/
public int size() {
return stack.size();
}
/**
* Tests if the stack is empty.
*
* @return {@code true} if the stack contains no elements; {@code false} otherwise
*/
public boolean isEmpty() {
return stack.size() == 0;
}
/**
* Tests if the specified element is contained in the stack.
*
* @param element the element to search for
* @return {@code true} if the element is found in the stack; {@code false} otherwise
*/
public boolean contains(T element) {
return stack.contains(element);
}
/**
* Returns the element at the top of the stack without removing it.
*
* @return the element at the top of the stack
* @throws EmptyStackException if the stack is empty
*/
public T peek() throws EmptyStackException {
if (stack.size() == 0) throw new EmptyStackException();
return stack.get(stack.size() - 1);
}
/**
* Returns an array containing all elements in the stack.
* The order of elements in the array matches the internal ArrayList representation.
*
* @return an array containing all elements in the stack
*/
@SuppressWarnings("unchecked")
public T[] toArray() {
return (T[]) stack.toArray();
}
/**
* Returns a string representation of the stack with each element on a new line.
* Elements are displayed from top to bottom (LIFO order).
*
* @return a string representation of the stack
*/
public String toString() {
StringBuilder sb = new StringBuilder();
for (T item : this) sb.append(item).append("\n");
return sb.toString();
}
/**
* Returns an iterator over the elements in the stack in LIFO order (top to bottom).
* The iterator starts from the top of the stack and moves toward the bottom.
*
* @return an iterator over the elements in the stack
*/
@Override
public Iterator<T> iterator() {
return stack.iterator(); //use List's built-in iterator()
}