Rewrote Stack.java

The previous implementation would show unexpected behaviour when using the values returned by pop() and peek().
Generic type was needed but not used in implementation thus leading to redundancy.
push() had unnecessary return type.
This commit is contained in:
Inluminous 2020-03-07 17:19:52 +01:00
parent 573b1ce056
commit 2d4d7dd742

View File

@ -19,42 +19,38 @@ public class Stack<E> implements Serializable {
/** /**
* Position of tail in stack * Position of tail in stack
*/ */
private int tail = -1; private int tail = -1;
/** /**
* Size of stack at any given time * Size of stack at any given time
*/ */
private int size; private int size;
/** /**
* Uninitialized array to hold stack elements. * Uninitialized array to hold stack elements.
* WIll be initialized with initial capacity once the object is created * Will be initialized with initial capacity once the object is created
*/ */
private Object[] elements; private E[] elements;
/** /**
* No argument to create stack object with initial capacity * No argument to create stack object with initial capacity
*/ */
@SuppressWarnings("unchecked")
public Stack() { public Stack() {
elements = new Object[INITIAL_CAPACITY]; elements = (E[]) new Object[INITIAL_CAPACITY];
} }
/** /**
* Method to check if the given stack is empty or not * Method to check if the given stack is empty or not
*/ */
public boolean empty() { public boolean empty() {
return elements == null || size == 0; return elements == null || size == 0;
} }
/** /**
* Method to check the element on head without removing it * Method to check the element on head without removing it
*/ */
public E peek() {
public Object peek() {
if (empty()) { if (empty()) {
throw new EmptyStackException(); throw new EmptyStackException();
} }
@ -65,13 +61,12 @@ public class Stack<E> implements Serializable {
/** /**
* Method to remove the top element from stack * Method to remove the top element from stack
*/ */
public E pop() {
public Object pop() {
if (empty()) { if (empty()) {
throw new EmptyStackException(); throw new EmptyStackException();
} }
Object removedElement = elements[tail]; E removedElement = elements[tail];
tail--; tail--;
size--; size--;
return removedElement; return removedElement;
@ -80,29 +75,23 @@ public class Stack<E> implements Serializable {
/** /**
* Method to add element to stack * Method to add element to stack
*/ */
public Object push(Object e) { @SuppressWarnings("unchecked")
public void push(E e) {
boolean isSuccess = false; tail = tail + 1;
if (tail < (INITIAL_CAPACITY - 1)) { if (tail >= INITIAL_CAPACITY) {
tail++; E[] extendedElements = (E[]) new Object[INITIAL_CAPACITY + EXTENDED_CAPACITY];
elements[tail] = e; System.arraycopy(elements, 0, extendedElements, 0, tail);
} else {
Object[] extendedElements = new Object[INITIAL_CAPACITY + EXTENDED_CAPACITY];
System.arraycopy(elements, 0, extendedElements, 0, (tail + 1));
elements = extendedElements; elements = extendedElements;
tail++;
elements[tail] = e;
} }
size++; elements[tail] = e;
return e; size = size + 1;
} }
/** /**
* Method to search for an element in stack * Method to search for an element in stack
*/ */
public int search(E o) {
public int search(Object o) {
int index = -1; int index = -1;
boolean found = false; boolean found = false;
@ -111,7 +100,7 @@ public class Stack<E> implements Serializable {
} }
for (int i = 0; i < size(); i++) { for (int i = 0; i < size(); i++) {
if (elements[i] == o) { if (elements[i].equals(o)) {
index = i; index = i;
found = true; found = true;
break; break;