Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- /**
- * Write a Java program to implement Stack in Java
- */
- public class StackImplementation {
- private ArrayList<Object> container = new ArrayList<>();
- private static int counter = -1;
- /**
- * Pushes items into the stack
- * @param object is the pushable object
- */
- public void push(Object object){
- container.add(object);
- counter++;
- }
- /**
- * Pops the last item from the stack
- * @return the last item from the stack
- */
- public Object pop(){
- if (!container.isEmpty()) {
- Object temp = container.get(counter);
- container.remove(counter);
- counter--;
- return temp;
- }
- return null;
- }
- /**
- * Checks if the stack is empty or not
- * @return true if the stack is empty, false if not
- */
- public boolean isEmpty(){
- return counter == -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement