Update Stack.java

Format code
This commit is contained in:
yanglbme 2019-03-16 15:08:57 +08:00 committed by GitHub
parent 27753bb8d1
commit 46df09ab86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,17 +3,17 @@ package src.main.java.com.dataStructures;
import java.io.Serializable; import java.io.Serializable;
import java.util.EmptyStackException; import java.util.EmptyStackException;
public class Stack<E> implements Serializable { public class Stack<E> implements Serializable {
/** /**
* Inital capacity alloted to stack on object creation * Initial capacity allocated to stack on object creation
*/ */
private final int INITIAL_CAPACITY = 10; private final int INITIAL_CAPACITY = 10;
/** /**
* Increment in memory space once stack is out of space * Increment in memory space once stack is out of space
*/ */
private final int EXNTEDED_CAPACITY = 10; private final int EXTENDED_CAPACITY = 10;
/** /**
@ -30,15 +30,14 @@ public class Stack<E> implements Serializable {
/** /**
* Uninitialized array to hold stack elements. * Uninitialized array to hold stack elements.
* WIll be intialized with inital capacity once the object is created * WIll be initialized with initial capacity once the object is created
*/ */
private Object[] elements; private Object[] elements;
/** /**
* No argument to create stack object with inital capacity * No argument to create stack object with initial capacity
*/ */
public Stack() { public Stack() {
elements = new Object[INITIAL_CAPACITY]; elements = new Object[INITIAL_CAPACITY];
} }
@ -47,16 +46,7 @@ public class Stack<E> implements Serializable {
*/ */
public boolean empty() { public boolean empty() {
return elements == null || size == 0;
if(null == elements) {
return true;
}
if(size == 0){
return true;
}
return false;
} }
@ -65,12 +55,11 @@ public class Stack<E> implements Serializable {
*/ */
public Object peek() { public Object peek() {
if (empty()) {
throw new EmptyStackException();
}
if(empty()) { return elements[tail];
throw new EmptyStackException();
}
return elements[tail];
} }
/** /**
@ -78,8 +67,7 @@ public class Stack<E> implements Serializable {
*/ */
public Object pop() { public Object pop() {
if (empty()) {
if(empty()) {
throw new EmptyStackException(); throw new EmptyStackException();
} }
@ -95,12 +83,12 @@ public class Stack<E> implements Serializable {
public Object push(Object e) { public Object push(Object e) {
boolean isSuccess = false; boolean isSuccess = false;
if(tail < (INITIAL_CAPACITY - 1)){ if (tail < (INITIAL_CAPACITY - 1)) {
tail++; tail++;
elements[tail] = e; elements[tail] = e;
}else{ } else {
Object[] extendedElements = new Object[INITIAL_CAPACITY + EXNTEDED_CAPACITY]; Object[] extendedElements = new Object[INITIAL_CAPACITY + EXTENDED_CAPACITY];
System.arraycopy(elements, 0, extendedElements, 0, (tail+1)); System.arraycopy(elements, 0, extendedElements, 0, (tail + 1));
elements = extendedElements; elements = extendedElements;
tail++; tail++;
elements[tail] = e; elements[tail] = e;
@ -118,19 +106,19 @@ public class Stack<E> implements Serializable {
int index = -1; int index = -1;
boolean found = false; boolean found = false;
if(empty()) { if (empty()) {
return -1; return -1;
} }
for(int i=0; i<size();i++) { for (int i = 0; i < size(); i++) {
if(elements[i] == o) { if (elements[i] == o) {
index = i; index = i;
found = true; found = true;
break; break;
} }
} }
if(found) { if (found) {
index = tail - index + 1; index = tail - index + 1;
} }
@ -143,5 +131,4 @@ public class Stack<E> implements Serializable {
public int size() { public int size() {
return size; return size;
} }
} }