forked from TheAlgorithms/Java
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackArray.java
More file actions
Latest commit
152 lines (136 loc) · 4.1 KB
/
Copy pathStackArray.java
File metadata and controls
152 lines (136 loc) · 4.1 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
/**
* 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.
*
* @author Unknown
*/
publicclassStackArray {
/**
* Main method
*
* @param args Command line arguments
*/
publicstaticvoidmain(String[] args) {
// Declare a stack of maximum size 4
StackArraymyStackArray = newStackArray(4);
// Populate the stack
myStackArray.push(5);
myStackArray.push(8);
myStackArray.push(2);
myStackArray.push(9);
System.out.println("*********************Stack Array Implementation*********************");
System.out.println(myStackArray.isEmpty()); // will print false
System.out.println(myStackArray.isFull()); // will print true
System.out.println(myStackArray.peek()); // will print 9
System.out.println(myStackArray.pop()); // will print 9
System.out.println(myStackArray.peek()); // will print 2
}
/**
* The max size of the Stack
*/
privateintmaxSize;
/**
* The array representation of the Stack
*/
privateint[] stackArray;
/**
* The top of the stack
*/
privateinttop;
/**
* Constructor
*
* @param size Size of the Stack
*/
publicStackArray(intsize) {
maxSize = size;
stackArray = newint[maxSize];
top = -1;
}
/**
* Adds an element to the top of the stack
*
* @param value The element added
*/
publicvoidpush(intvalue) {
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
*/
publicintpop() {
if (!isEmpty()) { // Checks for an empty stack
returnstackArray[top--];
}
if (top < maxSize / 4) {
resize(maxSize / 2);
returnpop();// 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
*/
publicintpeek() {
if (!isEmpty()) { // Checks for an empty stack
returnstackArray[top];
} else {
System.out.println("The stack is empty, cant peek");
return -1;
}
}
privatevoidresize(intnewSize) {
// private int[] transferArray = new int[newSize]; we can't put modifiers here !
int[] transferArray = newint[newSize];
// for(int i = 0; i < stackArray.length(); i++){ the length isn't a method .
for (inti = 0; i < stackArray.length; i++) {
transferArray[i] = stackArray[i];
stackArray = transferArray;
}
maxSize = newSize;
}
/**
* Returns true if the stack is empty
*
* @return true if the stack is empty
*/
publicbooleanisEmpty() {
return (top == -1);
}
/**
* Returns true if the stack is full
*
* @return true if the stack is full
*/
publicbooleanisFull() {
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
*/
publicvoidmakeEmpty() { // Doesn't delete elements in the array but if you call
top = -1; // push method after calling makeEmpty it will overwrite previous values
}
}