mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-13 07:13:37 +08:00
Format code in StackArray
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* This class implements a Stack using a regular array.
|
* 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
|
* 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
|
* 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
|
* of an array implementation of a Stack. So an element can only be added/removed
|
||||||
@ -8,7 +8,6 @@
|
|||||||
* array implementation it does.
|
* array implementation it does.
|
||||||
*
|
*
|
||||||
* @author Unknown
|
* @author Unknown
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class StackArray {
|
public class StackArray {
|
||||||
|
|
||||||
@ -18,7 +17,9 @@ public class StackArray{
|
|||||||
* @param args Command line arguments
|
* @param args Command line arguments
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
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
|
// Populate the stack
|
||||||
myStackArray.push(5);
|
myStackArray.push(5);
|
||||||
myStackArray.push(8);
|
myStackArray.push(8);
|
||||||
@ -33,13 +34,19 @@ public class StackArray{
|
|||||||
System.out.println(myStackArray.peek()); // will print 2
|
System.out.println(myStackArray.peek()); // will print 2
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The max size of the Stack */
|
/**
|
||||||
|
* The max size of the Stack
|
||||||
|
*/
|
||||||
private int maxSize;
|
private int maxSize;
|
||||||
|
|
||||||
/** The array representation of the Stack */
|
/**
|
||||||
|
* The array representation of the Stack
|
||||||
|
*/
|
||||||
private int[] stackArray;
|
private int[] stackArray;
|
||||||
|
|
||||||
/** The top of the stack */
|
/**
|
||||||
|
* The top of the stack
|
||||||
|
*/
|
||||||
private int top;
|
private int top;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -81,8 +88,7 @@ public class StackArray{
|
|||||||
if (top < maxSize / 4) {
|
if (top < maxSize / 4) {
|
||||||
resize(maxSize / 2);
|
resize(maxSize / 2);
|
||||||
return pop();// don't forget pop after resizing
|
return pop();// don't forget pop after resizing
|
||||||
}
|
} else {
|
||||||
else{
|
|
||||||
System.out.println("The stack is already empty");
|
System.out.println("The stack is already empty");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -103,7 +109,7 @@ public class StackArray{
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void resize(int newSize) {
|
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];
|
int[] transferArray = new int[newSize];
|
||||||
|
|
||||||
// for(int i = 0; i < stackArray.length(); i++){ the length isn't a method .
|
// 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
|
* Deletes everything in the Stack
|
||||||
*
|
* <p>
|
||||||
* Doesn't delete elements in the array
|
* Doesn't delete elements in the array
|
||||||
* but if you call push method after calling
|
* but if you call push method after calling
|
||||||
* makeEmpty it will overwrite previous
|
* makeEmpty it will overwrite previous
|
||||||
|
Reference in New Issue
Block a user