Format code in StackArray
This commit is contained in:
parent
29b7ad43d1
commit
fc64d05b5c
@ -1,6 +1,6 @@
|
||||
/**
|
||||
* 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
|
||||
@ -8,7 +8,6 @@
|
||||
* array implementation it does.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
public class StackArray {
|
||||
|
||||
@ -18,7 +17,9 @@ public class StackArray{
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
StackArray myStackArray = new StackArray(4); //Declare a stack of maximum size 4
|
||||
// Declare a stack of maximum size 4
|
||||
StackArray myStackArray = new StackArray(4);
|
||||
|
||||
// Populate the stack
|
||||
myStackArray.push(5);
|
||||
myStackArray.push(8);
|
||||
@ -33,13 +34,19 @@ public class StackArray{
|
||||
System.out.println(myStackArray.peek()); // will print 2
|
||||
}
|
||||
|
||||
/** The max size of the Stack */
|
||||
/**
|
||||
* The max size of the Stack
|
||||
*/
|
||||
private int maxSize;
|
||||
|
||||
/** The array representation of the Stack */
|
||||
/**
|
||||
* The array representation of the Stack
|
||||
*/
|
||||
private int[] stackArray;
|
||||
|
||||
/** The top of the stack */
|
||||
/**
|
||||
* The top of the stack
|
||||
*/
|
||||
private int top;
|
||||
|
||||
/**
|
||||
@ -81,8 +88,7 @@ public class StackArray{
|
||||
if (top < maxSize / 4) {
|
||||
resize(maxSize / 2);
|
||||
return pop();// don't forget pop after resizing
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
System.out.println("The stack is already empty");
|
||||
return -1;
|
||||
}
|
||||
@ -103,7 +109,7 @@ public class StackArray{
|
||||
}
|
||||
|
||||
private void resize(int newSize) {
|
||||
//private int[] transferArray = new int[newSize]; we can't put modifires here !
|
||||
// private int[] transferArray = new int[newSize]; we can't put modifiers here !
|
||||
int[] transferArray = new int[newSize];
|
||||
|
||||
// for(int i = 0; i < stackArray.length(); i++){ the length isn't a method .
|
||||
@ -134,7 +140,7 @@ public class StackArray{
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
Loading…
Reference in New Issue
Block a user