In our couple of previous articles, we have seen a different implementation of Stack such as
- Stack Implementation using Array in Java
- Dynamic Stack Implementation using Array in Java
- Stack Implementation using Linked List in Java
Stack Implementation using Array List
This is an ArrayList implementation of a Stack, Where size is not a problem we can extend the stack as much as we want.
Let's write a program to demonstrate implementation of Stack using ArrayList.
import java.util.ArrayList;
import java.util.List;
/**
* This is an ArrayList Implementation of stack, Where size is not a problem we
* can extend the stack as much as we want.
*
* @author Ramesh Fadatare
*
*/
public class StackUsingArrayList {
/** ArrayList representation of the stack */
List<Integer> stackList;
/**
* Constructor
*/
StackUsingArrayList() {
stackList = new ArrayList<>();
}
/**
* Adds value to the end of list which is the top for stack
*
* @param value
* value to be added
*/
void push(int value) {
stackList.add(value);
}
/**
* Pops last element of list which is indeed the top for Stack
*
* @return Element popped
*/
int pop() {
if (!isEmpty()) { // checks for an empty Stack
int popValue = stackList.get(stackList.size() - 1);
stackList.remove(stackList.size() - 1); // removes the poped element
return popValue;
} else {
System.out.print("The stack is already empty ");
return -1;
}
}
/**
* Checks for empty Stack
*
* @return true if stack is empty
*/
boolean isEmpty() {
if (stackList.isEmpty()){
return true;
} else {
return false;
}
}
/**
* Top element of stack
*
* @return top element of stack
*/
int peek() {
return stackList.get(stackList.size() - 1);
}
public static void main(String[] args) {
StackUsingArrayList myStack = new StackUsingArrayList(); // Declare a stack of maximum size 4
// Populate the stack
myStack.push(5);
myStack.push(8);
myStack.push(2);
myStack.push(9);
System.out.println("*********************Stack ArrayList Implementation*********************");
System.out.println(myStack.isEmpty()); // will print false
System.out.println(myStack.peek()); // will print 9
System.out.println(myStack.pop()); // will print 9
System.out.println(myStack.peek()); // will print 2
System.out.println(myStack.pop()); // will print 2
}
}
Output:
*********************Stack ArrayList Implementation*********************
false
9
9
2
2
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course