Format code in StackArray

This commit is contained in:
Libin Yang
2019-01-06 08:56:51 +08:00
committed by GitHub
parent 29b7ad43d1
commit fc64d05b5c

View File

@ -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,9 +8,8 @@
* array implementation it does. * array implementation it does.
* *
* @author Unknown * @author Unknown
*
*/ */
public class StackArray{ public class StackArray {
/** /**
* Main method * Main method
@ -18,28 +17,36 @@ 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
//Populate the stack StackArray myStackArray = new StackArray(4);
// Populate the stack
myStackArray.push(5); myStackArray.push(5);
myStackArray.push(8); myStackArray.push(8);
myStackArray.push(2); myStackArray.push(2);
myStackArray.push(9); myStackArray.push(9);
System.out.println("*********************Stack Array Implementation*********************"); System.out.println("*********************Stack Array Implementation*********************");
System.out.println(myStackArray.isEmpty()); //will print false System.out.println(myStackArray.isEmpty()); // will print false
System.out.println(myStackArray.isFull()); //will print true System.out.println(myStackArray.isFull()); // will print true
System.out.println(myStackArray.peek()); //will print 9 System.out.println(myStackArray.peek()); // will print 9
System.out.println(myStackArray.pop()); //will print 9 System.out.println(myStackArray.pop()); // will print 9
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;
/** /**
@ -47,7 +54,7 @@ public class StackArray{
* *
* @param size Size of the Stack * @param size Size of the Stack
*/ */
public StackArray(int size){ public StackArray(int size) {
maxSize = size; maxSize = size;
stackArray = new int[maxSize]; stackArray = new int[maxSize];
top = -1; top = -1;
@ -58,13 +65,13 @@ public class StackArray{
* *
* @param value The element added * @param value The element added
*/ */
public void push(int value){ public void push(int value) {
if(!isFull()){ //Checks for a full stack if (!isFull()) { // Checks for a full stack
top++; top++;
stackArray[top] = value; stackArray[top] = value;
}else{ } else {
resize(maxSize*2); resize(maxSize * 2);
push(value);// don't forget push after resizing push(value); // don't forget push after resizing
} }
} }
@ -73,16 +80,15 @@ public class StackArray{
* *
* @return value popped off the Stack * @return value popped off the Stack
*/ */
public int pop(){ public int pop() {
if(!isEmpty()){ //Checks for an empty stack if (!isEmpty()) { // Checks for an empty stack
return stackArray[top--]; return stackArray[top--];
} }
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;
} }
@ -93,21 +99,21 @@ public class StackArray{
* *
* @return element at the top of the stack * @return element at the top of the stack
*/ */
public int peek(){ public int peek() {
if(!isEmpty()){ //Checks for an empty stack if (!isEmpty()) { // Checks for an empty stack
return stackArray[top]; return stackArray[top];
}else{ } else {
System.out.println("The stack is empty, cant peek"); System.out.println("The stack is empty, cant peek");
return -1; return -1;
} }
} }
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 .
for(int i = 0; i < stackArray.length; i++){ for (int i = 0; i < stackArray.length; i++) {
transferArray[i] = stackArray[i]; transferArray[i] = stackArray[i];
stackArray = transferArray; stackArray = transferArray;
} }
@ -119,8 +125,8 @@ public class StackArray{
* *
* @return true if the stack is empty * @return true if the stack is empty
*/ */
public boolean isEmpty(){ public boolean isEmpty() {
return(top == -1); return (top == -1);
} }
/** /**
@ -128,19 +134,19 @@ public class StackArray{
* *
* @return true if the stack is full * @return true if the stack is full
*/ */
public boolean isFull(){ public boolean isFull() {
return(top+1 == maxSize); return (top + 1 == maxSize);
} }
/** /**
* 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
* values * values
*/ */
public void makeEmpty(){ //Doesn't delete elements in the array but if you call 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 top = -1; // push method after calling makeEmpty it will overwrite previous values
} }
} }