refactor: StackArray (#5349)

This commit is contained in:
Alex Klymenko
2024-08-20 12:10:18 +02:00
committed by GitHub
parent 8605220721
commit f5c0314111
3 changed files with 210 additions and 128 deletions

View File

@ -0,0 +1,51 @@
package com.thealgorithms.datastructures.stacks;
/**
* A generic interface for Stack data structures.
*
* @param <T> the type of elements in this stack
*/
public interface Stack<T> {
/**
* Adds an element to the top of the stack.
*
* @param value The element to add.
*/
void push(T value);
/**
* Removes the element at the top of this stack and returns it.
*
* @return The element popped from the stack.
* @throws IllegalStateException if the stack is empty.
*/
T pop();
/**
* Returns the element at the top of this stack without removing it.
*
* @return The element at the top of this stack.
* @throws IllegalStateException if the stack is empty.
*/
T peek();
/**
* Tests if this stack is empty.
*
* @return {@code true} if this stack is empty; {@code false} otherwise.
*/
boolean isEmpty();
/**
* Returns the size of this stack.
*
* @return The number of elements in this stack.
*/
int size();
/**
* Removes all elements from this stack.
*/
void makeEmpty();
}

View File

@ -3,170 +3,80 @@ package com.thealgorithms.datastructures.stacks;
/**
* 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 from the end of the array. In theory stack have no fixed size,
* but with an array implementation it does.
* @param <T> the type of elements in this stack
*/
public class StackArray {
public class StackArray<T> implements Stack<T> {
/**
* Driver Code
*/
public static void main(String[] args) {
// Declare a stack of maximum size 4
StackArray myStackArray = new StackArray(4);
assert myStackArray.isEmpty();
assert !myStackArray.isFull();
// Populate the stack
myStackArray.push(5);
myStackArray.push(8);
myStackArray.push(2);
myStackArray.push(9);
assert !myStackArray.isEmpty();
assert myStackArray.isFull();
assert myStackArray.peek() == 9;
assert myStackArray.pop() == 9;
assert myStackArray.peek() == 2;
assert myStackArray.size() == 3;
}
/**
* Default initial capacity.
*/
private static final int DEFAULT_CAPACITY = 10;
/**
* The max size of the Stack
*/
private int maxSize;
/**
* The array representation of the Stack
*/
private int[] stackArray;
/**
* The top of the stack
*/
private T[] stackArray;
private int top;
/**
* init Stack with DEFAULT_CAPACITY
*/
@SuppressWarnings("unchecked")
public StackArray() {
this(DEFAULT_CAPACITY);
}
/**
* Constructor
*
* @param size Size of the Stack
*/
@SuppressWarnings("unchecked")
public StackArray(int size) {
maxSize = size;
stackArray = new int[maxSize];
top = -1;
if (size <= 0) {
throw new IllegalArgumentException("Stack size must be greater than 0");
}
this.maxSize = size;
this.stackArray = (T[]) new Object[size];
this.top = -1;
}
/**
* Adds an element to the top of the stack
*
* @param value The element added
*/
public void push(int value) {
if (!isFull()) { // Checks for a full stack
top++;
stackArray[top] = value;
} else {
@Override
public void push(T value) {
if (isFull()) {
resize(maxSize * 2);
push(value); // don't forget push after resizing
}
stackArray[++top] = value;
}
/**
* Removes the top element of the stack and returns the value you've removed
*
* @return value popped off the Stack
*/
public int pop() {
if (!isEmpty()) { // Checks for an empty stack
return stackArray[top--];
@Override
public T pop() {
if (isEmpty()) {
throw new IllegalStateException("Stack is empty, cannot pop element");
}
if (top < maxSize / 4) {
T value = stackArray[top--];
if (top + 1 < maxSize / 4 && maxSize > DEFAULT_CAPACITY) {
resize(maxSize / 2);
return pop(); // don't forget pop after resizing
} else {
System.out.println("The stack is already empty");
return -1;
}
return value;
}
/**
* Returns the element at the top of the stack
*
* @return element at the top of the stack
*/
public int peek() {
if (!isEmpty()) { // Checks for an empty stack
return stackArray[top];
} else {
System.out.println("The stack is empty, cant peek");
return -1;
@Override
public T peek() {
if (isEmpty()) {
throw new IllegalStateException("Stack is empty, cannot peek element");
}
return stackArray[top];
}
private void resize(int newSize) {
int[] transferArray = new int[newSize];
for (int i = 0; i < stackArray.length; i++) {
transferArray[i] = stackArray[i];
}
// This reference change might be nice in here
stackArray = transferArray;
@SuppressWarnings("unchecked") T[] newArray = (T[]) new Object[newSize];
System.arraycopy(stackArray, 0, newArray, 0, top + 1);
stackArray = newArray;
maxSize = newSize;
}
/**
* Returns true if the stack is empty
*
* @return true if the stack is empty
*/
public boolean isEmpty() {
return (top == -1);
}
/**
* Returns true if the stack is full
*
* @return true if the stack is full
*/
public boolean isFull() {
return (top + 1 == maxSize);
return top + 1 == maxSize;
}
/**
* 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 values
*/
public void makeEmpty() { // Doesn't delete elements in the array but if you call
@Override
public boolean isEmpty() {
return top == -1;
}
@Override 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
}
/**
* Return size of stack
*
* @return size of stack
*/
@Override
public int size() {
return top + 1;
}