Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 21.3k
Expand file tree
/
Copy pathStackArray.java
More file actions
Latest commit
160 lines (146 loc) · 4.23 KB
/
Copy pathStackArray.java
File metadata and controls
160 lines (146 loc) · 4.23 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
packagecom.thealgorithms.datastructures.stacks;
/**
* Implements a generic stack using an array.
*
* <p>This stack automatically resizes when necessary, growing to accommodate additional elements and
* shrinking to conserve memory when its size significantly decreases.
*
* <p>Elements are pushed and popped in LIFO (last-in, first-out) order, where the last element added
* is the first to be removed.
*
* @param <T> the type of elements in this stack
*/
publicclassStackArray<T> implementsStack<T> {
privatestaticfinalintDEFAULT_CAPACITY = 10;
privateintmaxSize;
privateT[] stackArray;
privateinttop;
/**
* Creates a stack with a default capacity.
*/
@SuppressWarnings("unchecked")
publicStackArray() {
this(DEFAULT_CAPACITY);
}
/**
* Creates a stack with a specified initial capacity.
*
* @param size the initial capacity of the stack, must be greater than 0
* @throws IllegalArgumentException if size is less than or equal to 0
*/
@SuppressWarnings("unchecked")
publicStackArray(intsize) {
if (size <= 0) {
thrownewIllegalArgumentException("Stack size must be greater than 0");
}
this.maxSize = size;
this.stackArray = (T[]) newObject[size];
this.top = -1;
}
/**
* Pushes an element onto the top of the stack. Resizes the stack if it is full.
*
* @param value the element to push
*/
@Override
publicvoidpush(Tvalue) {
if (isFull()) {
resize(maxSize * 2);
}
stackArray[++top] = value;
}
/**
* Removes and returns the element from the top of the stack. Shrinks the stack if
* its size is below a quarter of its capacity, but not below the default capacity.
*
* @return the element removed from the top of the stack
* @throws IllegalStateException if the stack is empty
*/
@Override
publicTpop() {
if (isEmpty()) {
thrownewIllegalStateException("Stack is empty, cannot pop element");
}
Tvalue = stackArray[top--];
if (top + 1 < maxSize / 4 && maxSize > DEFAULT_CAPACITY) {
resize(maxSize / 2);
}
returnvalue;
}
/**
* Returns the element at the top of the stack without removing it.
*
* @return the top element of the stack
* @throws IllegalStateException if the stack is empty
*/
@Override
publicTpeek() {
if (isEmpty()) {
thrownewIllegalStateException("Stack is empty, cannot peek element");
}
returnstackArray[top];
}
/**
* Resizes the internal array to a new capacity.
*
* @param newSize the new size of the stack array
*/
privatevoidresize(intnewSize) {
@SuppressWarnings("unchecked") T[] newArray = (T[]) newObject[newSize];
System.arraycopy(stackArray, 0, newArray, 0, top + 1);
stackArray = newArray;
maxSize = newSize;
}
/**
* Checks if the stack is full.
*
* @return true if the stack is full, false otherwise
*/
publicbooleanisFull() {
returntop + 1 == maxSize;
}
/**
* Checks if the stack is empty.
*
* @return true if the stack is empty, false otherwise
*/
@Override
publicbooleanisEmpty() {
returntop == -1;
}
/**
* Empties the stack, marking it as empty without deleting elements. Elements are
* overwritten on subsequent pushes.
*/
@Override
publicvoidmakeEmpty() {
top = -1;
}
/**
* Returns the number of elements currently in the stack.
*
* @return the size of the stack
*/
@Override
publicintsize() {
returntop + 1;
}
/**
* Returns a string representation of the stack.
*
* @return a string representation of the stack
*/
@Override
publicStringtoString() {
StringBuildersb = newStringBuilder();
sb.append("StackArray [");
for (inti = 0; i <= top; i++) {
sb.append(stackArray[i]);
if (i < top) {
sb.append(", ");
}
}
sb.append("]");
returnsb.toString();
}
}