Description:
An array-based
Stack
class. Since this class is designed using an array the size of theStack
is limited to whatever the program gives you. The key reason was not to use any outside help likeArrays.copyOf(array, size)
to increase size etc. (In future,LinkedStack
will solve size problem).MAX_SIZE
is made public to let a user know aboutMAX_SIZE
andsize()
returns current size of theStack
. Return valueint
is used to make sure all push/insertions are successful onStack
.
Right now I'm just looking for suggestions to improve my code before I push it to my GitHub account.
package Stack;
public class StackArray
{
public static final int MAX_SIZE = 1040; // MAX size of a Stack
private int topLook; // current element on Stack
private int stackElements[]; // array to store Stack elements
public StackArray() // default constructor creates a Stack with capacity of 50
{
topLook = -1; // initially no elements on the Stack
stackElements = new int[MAX_SIZE]; // an array of MAX_SIZE gets created
}
public int size() // returns current size of the Stack
{
return (topLook + 1);
}
public boolean emptyCheck() // returns true if this Stack has no elements in it, false otherwise
{
return (topLook == -1);
}
public boolean fullCheck() // returns true if this Stack is full, false otherwise
{
return (topLook == MAX_SIZE - 1);
}
public int top() // returns top element of the Stack, -1 otherwise
{
if (!emptyCheck())
{
return stackElements[topLook];
}
return -1;
}
public int push(int value) // push passed argument on the Stack. 1 indicates insertion was successful, -1 otherwise
{
if (!fullCheck())
{
stackElements[++topLook] = value;
return 1;
}
return -1;
}
public int pop() // returns top element of the Stack and remove it, -1 otherwise
{
if (!emptyCheck())
{
return stackElements[topLook--];
}
return -1;
}
}