From 89ae3d5dec9370f2540aa7ad3c32d25456b8fd3c Mon Sep 17 00:00:00 2001 From: shellhub Date: Mon, 14 Sep 2020 20:18:54 +0800 Subject: [PATCH] Updated StackArrayList --- DataStructures/Stacks/StackArrayList.java | 90 +++++++++++++---------- 1 file changed, 53 insertions(+), 37 deletions(-) diff --git a/DataStructures/Stacks/StackArrayList.java b/DataStructures/Stacks/StackArrayList.java index 666b9ea9..5f7168c6 100644 --- a/DataStructures/Stacks/StackArrayList.java +++ b/DataStructures/Stacks/StackArrayList.java @@ -1,6 +1,7 @@ package DataStructures.Stacks; import java.util.ArrayList; +import java.util.EmptyStackException; /** * This class implements a Stack using an ArrayList. @@ -10,43 +11,49 @@ import java.util.ArrayList; *

* This is an ArrayList Implementation of a stack, where size is not * a problem we can extend the stack as much as we want. - * - * @author Unknown */ public class StackArrayList { /** - * Main method - * - * @param args Command line arguments + * Driver Code */ public static void main(String[] args) { - - StackArrayList myStackArrayList = new StackArrayList(); - - myStackArrayList.push(5); - myStackArrayList.push(8); - myStackArrayList.push(2); - myStackArrayList.push(9); + StackArrayList stack = new StackArrayList(); + assert stack.isEmpty(); + + for (int i = 1; i <= 5; ++i) { + stack.push(i); + assert stack.size() == i; + } + + assert stack.size() == 5; + assert stack.peek() == 5 && stack.pop() == 5 && stack.peek() == 4; + + /* pop elements at the top of this stack one by one */ + while (!stack.isEmpty()) { + stack.pop(); + } + assert stack.isEmpty(); + + try { + stack.pop(); + assert false; /* this should not happen */ + } catch (EmptyStackException e) { + assert true; /* this should happen */ + } - System.out.println("*********************Stack List Implementation*********************"); - System.out.println(myStackArrayList.isEmpty()); // will print false - System.out.println(myStackArrayList.peek()); // will print 9 - System.out.println(myStackArrayList.pop()); // will print 9 - System.out.println(myStackArrayList.peek()); // will print 2 - System.out.println(myStackArrayList.pop()); // will print 2 } /** * ArrayList representation of the stack */ - private ArrayList stackList; + private ArrayList stack; /** * Constructor */ public StackArrayList() { - stackList = new ArrayList<>(); + stack = new ArrayList<>(); } /** @@ -56,42 +63,51 @@ public class StackArrayList { * @param value value to be added */ public void push(int value) { - stackList.add(value); + stack.add(value); } /** - * Pops last element of list which is indeed - * the top for Stack + * Removes the element at the top of this stack and returns * * @return Element popped + * @throws EmptyStackException if the stack is empty. */ public 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 from the list - return popValue; + if (isEmpty()) { + throw new EmptyStackException(); } - System.out.print("The stack is already empty!"); - return -1; + /* remove the element on the top of the stack */ + return stack.remove(stack.size() - 1); } /** - * Checks for empty Stack + * Test if the stack is empty. * - * @return true if stack is empty + * @return {@code true} if this stack is empty, {@code false} otherwise. */ public boolean isEmpty() { - return stackList.isEmpty(); + return stack.isEmpty(); } /** - * Top element of stack + * Return the element at the top of this stack without removing it from the stack. * - * @return top element of stack + * @return the element at the top of this stack. */ public int peek() { - return stackList.get(stackList.size() - 1); + if (isEmpty()) { + throw new EmptyStackException(); + } + return stack.get(stack.size() - 1); } -} + + /** + * Return size of this stack. + * + * @return size of this stack. + */ + public int size() { + return stack.size(); + } +} \ No newline at end of file