1. Overview
In this tutorial, we’ll discuss how to create a char stack in Java. We’ll first see how we can do this by using the Java API, and then we’ll look at some custom implementations.
Stack is a data structure that follows the LIFO (Last In First Out) principle. Some of its common methods are:
- push(E item) – pushes an item to the top of the stack
- pop() – removes and returns the object at the top of the stack
- peek() – returns the object at the top of the stack without removing it
2. Char Stack Using Java API
Java has a built-in API named java.util.Stack. Since char is a primitive datatype, which cannot be used in generics, we have to use the wrapper class of java.lang.Character to create a Stack:
Stack<Character> charStack = new Stack<>();
Now, we can use the push, pop, and peek methods with our Stack.
On the other hand, we may be asked to build a custom implementation of a stack. Therefore, we’ll be looking into a couple of different approaches.
3. Custom Implementation Using LinkedList
Let’s implement a char stack using a LinkedList as our back-end data structure:
public class CharStack { private LinkedList<Character> items; public CharStack() { this.items = new LinkedList<Character>(); } }
We created an items variable that gets initialized in the constructor.
Now, we have to provide an implementation of the push, peek, and pop methods:
public void push(Character item) { items.push(item); } public Character peek() { return items.getFirst(); } public Character pop() { Iterator<Character> iter = items.iterator(); Character item = iter.next(); if (item != null) { iter.remove(); return item; } return null; }
The push and peek methods are using the built-in methods of a LinkedList. For pop, we first used an Iterator to check if there’s an item on the top or not. If it’s there, we remove the item from the list by calling the remove method.
4. Custom Implementation Using an Array
We can also use an array for our data structure:
public class CharStackWithArray { private char[] elements; private int size; public CharStackWithArray() { size = 0; elements = new char[4]; } }
Above, we create a char array, which we initialize in the constructor with an initial capacity of 4. Additionally, we have a size variable to keep track of how many records are present in our stack.
Now, let’s implement the push method:
public void push(char item) { ensureCapacity(size + 1); elements[size] = item; size++; } private void ensureCapacity(int newSize) { char newBiggerArray[]; if (elements.length < newSize) { newBiggerArray = new char[elements.length * 2]; System.arraycopy(elements, 0, newBiggerArray, 0, size); elements = newBiggerArray; } }
While pushing an item to the stack, we first need to check if our array has the capacity to store it. If not, we create a new array and double its size. We then copy the old elements to the newly created array and assign it to our elements variable.
Note: for an explanation of why we want to double the size of the array, rather than simply increase the size by one, please refer to this StackOverflow post.
Finally, let’s implement the peek and pop methods:
public char peek() { if (size == 0) { throw new EmptyStackException(); } return elements[size - 1]; } public char pop() { if (size == 0) { throw new EmptyStackException(); } return elements[--size]; }
For both methods, after validating that the stack is not empty, we return the element at position size – 1. For pop, in addition to returning the element, we decrement the size by 1.
5. Conclusion
In this article, we learned how to make a char stack using the Java API, and we saw a couple of custom implementation.
The code presented in this article is available over on GitHub.