forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StackArray.java
158 lines (137 loc) · 3.7 KB
/
StackArray.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
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
package DataStructures.Stacks;
/**
* This class implements a Stack using a regular array.
*
* <p>A stack is exactly what it sounds like. An element gets added to the top of the stack and only
* the element on the top may be removed. This is an example of an array implementation of a Stack.
* So an element can only be added/removed from the end of the array. In theory stack have no fixed
* size, but with an array implementation it does.
*/
public class StackArray {
/** Driver Code */
public static void main(String[] args) {
// Declare a stack of maximum size 4
StackArray myStackArray = new StackArray(4);
assert myStackArray.isEmpty();
assert !myStackArray.isFull();
// Populate the stack
myStackArray.push(5);
myStackArray.push(8);
myStackArray.push(2);
myStackArray.push(9);
assert !myStackArray.isEmpty();
assert myStackArray.isFull();
assert myStackArray.peek() == 9;
assert myStackArray.pop() == 9;
assert myStackArray.peek() == 2;
assert myStackArray.size() == 3;
}
/** Default initial capacity. */
private static final int DEFAULT_CAPACITY = 10;
/** The max size of the Stack */
private int maxSize;
/** The array representation of the Stack */
private int[] stackArray;
/** The top of the stack */
private int top;
/** init Stack with DEFAULT_CAPACITY */
public StackArray() {
this(DEFAULT_CAPACITY);
}
/**
* Constructor
*
* @param size Size of the Stack
*/
public StackArray(int size) {
maxSize = size;
stackArray = new int[maxSize];
top = -1;
}
/**
* Adds an element to the top of the stack
*
* @param value The element added
*/
public void push(int value) {
if (!isFull()) { // Checks for a full stack
top++;
stackArray[top] = value;
} else {
resize(maxSize * 2);
push(value); // don't forget push after resizing
}
}
/**
* Removes the top element of the stack and returns the value you've removed
*
* @return value popped off the Stack
*/
public int pop() {
if (!isEmpty()) { // Checks for an empty stack
return stackArray[top--];
}
if (top < maxSize / 4) {
resize(maxSize / 2);
return pop(); // don't forget pop after resizing
} else {
System.out.println("The stack is already empty");
return -1;
}
}
/**
* Returns the element at the top of the stack
*
* @return element at the top of the stack
*/
public int peek() {
if (!isEmpty()) { // Checks for an empty stack
return stackArray[top];
} else {
System.out.println("The stack is empty, cant peek");
return -1;
}
}
private void resize(int newSize) {
int[] transferArray = new int[newSize];
for (int i = 0; i < stackArray.length; i++) {
transferArray[i] = stackArray[i];
}
// This reference change might be nice in here
stackArray = transferArray;
maxSize = newSize;
}
/**
* Returns true if the stack is empty
*
* @return true if the stack is empty
*/
public boolean isEmpty() {
return (top == -1);
}
/**
* Returns true if the stack is full
*
* @return true if the stack is full
*/
public boolean isFull() {
return (top + 1 == maxSize);
}
/**
* Deletes everything in the Stack
*
* <p>Doesn't delete elements in the array but if you call push method after calling makeEmpty it
* will overwrite previous values
*/
public void makeEmpty() { // Doesn't delete elements in the array but if you call
top = -1; // push method after calling makeEmpty it will overwrite previous values
}
/**
* Return size of stack
*
* @return size of stack
*/
public int size() {
return top + 1;
}
}