Unlike the implementation of a double queue, a stack is a collection of items kept in order such that the item last inserted into the stack is the first one removed.
MyStack.java
This stack is an implementation uses char‘s. A stack such as this one would be useful in looking for patterns in strings, like checking if a string is a palindrome.
public class MyStack {
private char[] _stack = new char[25]; // The stack.
private int _currentIndex = 0; // The current index.
/**
* Push the character to the stack.
*
* @param c
*/
public void push(char c)
{
// Add to stack.
_stack[_currentIndex++] = c;
}
/**
* Pop the last character from the stack.
*
* @return
*/
public char pop()
{
// Remove from stack.
_currentIndex--; // Decrement before the access since it should be minus 1 the current index anyway.
return _stack[_currentIndex];
}
/**
* Check if the stack is empty.
*
* @return
*/
public boolean empty()
{
return _currentIndex == 0;
}
/**
* Return the size of the stack.
*
* @return
*/
public int size()
{
return _currentIndex + 1;
}
}