From 4bbc39f93b6f038182f2f4217e966d209f8cac62 Mon Sep 17 00:00:00 2001 From: Hayder Hassan Date: Fri, 4 Jan 2019 22:10:02 +0000 Subject: [PATCH 1/6] Move code for Stack Array from Stacks.java to StackArray.java --- DataStructures/Stacks/StackArray.java | 146 ++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 DataStructures/Stacks/StackArray.java diff --git a/DataStructures/Stacks/StackArray.java b/DataStructures/Stacks/StackArray.java new file mode 100644 index 00000000..795958e8 --- /dev/null +++ b/DataStructures/Stacks/StackArray.java @@ -0,0 +1,146 @@ +/** + * This class implements a Stack using a regular array. + * + * A stack is exactly what it sounds like. An element gets added to the top of + * the stack and only the element on the top may be removed. This is an example + * of an array implementation of a Stack. So an element can only be added/removed + * from the end of the array. In theory stack have no fixed size, but with an + * array implementation it does. + * + * @author Unknown + * + */ +public class StackArray{ + + /** + * Main method + * + * @param args Command line arguments + */ + public static void main(String[] args) { + StackArray myStackArray = new StackArray(4); //Declare a stack of maximum size 4 + //Populate the stack + myStackArray.push(5); + myStackArray.push(8); + myStackArray.push(2); + myStackArray.push(9); + + System.out.println("*********************Stack Array Implementation*********************"); + System.out.println(myStackArray.isEmpty()); //will print false + System.out.println(myStackArray.isFull()); //will print true + System.out.println(myStackArray.peek()); //will print 9 + System.out.println(myStackArray.pop()); //will print 9 + System.out.println(myStackArray.peek()); // will print 2 + } + + /** The max size of the Stack */ + private int maxSize; + + /** The array representation of the Stack */ + private int[] stackArray; + + /** The top of the stack */ + private int top; + + /** + * Constructor + * + * @param size Size of the Stack + */ + public StackArray(int size){ + maxSize = size; + stackArray = new int[maxSize]; + top = -1; + } + + /** + * Adds an element to the top of the stack + * + * @param value The element added + */ + public void push(int value){ + if(!isFull()){ //Checks for a full stack + top++; + stackArray[top] = value; + }else{ + resize(maxSize*2); + push(value);// don't forget push after resizing + } + } + + /** + * Removes the top element of the stack and returns the value you've removed + * + * @return value popped off the Stack + */ + public int pop(){ + if(!isEmpty()){ //Checks for an empty stack + return stackArray[top--]; + } + + if(top < maxSize/4){ + resize(maxSize/2); + return pop();// don't forget pop after resizing + } + else{ + System.out.println("The stack is already empty"); + return -1; + } + } + + /** + * Returns the element at the top of the stack + * + * @return element at the top of the stack + */ + public int peek(){ + if(!isEmpty()){ //Checks for an empty stack + return stackArray[top]; + }else{ + System.out.println("The stack is empty, cant peek"); + return -1; + } + } + + private void resize(int newSize){ + //private int[] transferArray = new int[newSize]; we can't put modifires here ! + int[] transferArray = new int[newSize]; + + //for(int i = 0; i < stackArray.length(); i++){ the length isn't a method . + for(int i = 0; i < stackArray.length; i++){ + transferArray[i] = stackArray[i]; + stackArray = transferArray; + } + maxSize = newSize; + } + + /** + * Returns true if the stack is empty + * + * @return true if the stack is empty + */ + public boolean isEmpty(){ + return(top == -1); + } + + /** + * Returns true if the stack is full + * + * @return true if the stack is full + */ + public boolean isFull(){ + return(top+1 == maxSize); + } + + /** + * Deletes everything in the Stack + * + * Doesn't delete elements in the array + * but if you call push method after calling + * makeEmpty it will overwrite previous + * values + */ + public void makeEmpty(){ //Doesn't delete elements in the array but if you call + top = -1; //push method after calling makeEmpty it will overwrite previous values + } +} \ No newline at end of file From bbbf63c9a1303aabbde7a5bd060b466d12349339 Mon Sep 17 00:00:00 2001 From: Hayder Hassan Date: Fri, 4 Jan 2019 22:22:08 +0000 Subject: [PATCH 2/6] Move code for Stack ArrayList from Stacks.java to StackArrayList.java --- DataStructures/Stacks/StackArrayList.java | 94 +++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 DataStructures/Stacks/StackArrayList.java diff --git a/DataStructures/Stacks/StackArrayList.java b/DataStructures/Stacks/StackArrayList.java new file mode 100644 index 00000000..5093b76f --- /dev/null +++ b/DataStructures/Stacks/StackArrayList.java @@ -0,0 +1,94 @@ +import java.util.ArrayList; + +/** + * This class implements a Stack using an ArrayList. + * + * A stack is exactly what it sounds like. An element gets added to the top of + * the stack and only the element on the top may be removed. + * + * 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 + */ + public static void main(String[] args) { + StackArrayList myStackArrayList = new StackArrayList(); //Declare a stack of maximum size 4 + //Populate the stack + myStackArrayList.push(5); + myStackArrayList.push(8); + myStackArrayList.push(2); + myStackArrayList.push(9); + + 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; + + /** + * Constructor + */ + public StackArrayList(){ + stackList = new ArrayList<>(); + } + + /** + * Adds value to the end of list which + * is the top for stack + * + * @param value value to be added + */ + public void push(int value){ + stackList.add(value); + } + + /** + * Pops last element of list which is indeed + * the top for Stack + * + * @return Element popped + */ + 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; + } + + System.out.print("The stack is already empty "); + return -1; + } + + /** + * Checks for empty Stack + * + * @return true if stack is empty + */ + public boolean isEmpty(){ + return stackList.isEmpty(); + } + + /** + * Top element of stack + * + * @return top element of stack + */ + public int peek(){ + return stackList.get(stackList.size()-1); + } +} \ No newline at end of file From 787a9fa0caab16e40ca4380abc6803b98d03f26b Mon Sep 17 00:00:00 2001 From: Hayder Hassan Date: Fri, 4 Jan 2019 22:29:37 +0000 Subject: [PATCH 3/6] Update link to StackArray and StackArrayList classes The Stacks link in the Korean readme takes user to the Stacks directory rather than a specific Java file. Will need a Korean translator to help with the new links. --- README-ko.md | 2 +- README.md | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README-ko.md b/README-ko.md index 0c15ded7..c53868cc 100644 --- a/README-ko.md +++ b/README-ko.md @@ -179,7 +179,7 @@ and much more...| and more... ------|-----| [노드 스택](DataStructures/Stacks/NodeStack.java)|[AVL 트리](DataStructures/Trees/AVLTree.java)| [연결리스트 스택](DataStructures/Stacks/StackOfLinkedList.java)|[이진 트리](DataStructures/Trees/BinaryTree.java)| -[스택](DataStructures/Stacks/Stacks.java)|And much more...| +[스택](DataStructures/Stacks)|And much more...| * [Bags](DataStructures/Bags/Bag.java) * [Buffer](DataStructures/Buffers/CircularBuffer.java) diff --git a/README.md b/README.md index a8eb9970..08c79b28 100644 --- a/README.md +++ b/README.md @@ -181,7 +181,8 @@ Stacks|Trees| ------|-----| [Node Stack](DataStructures/Stacks/NodeStack.java)|[AVL Tree](DataStructures/Trees/AVLTree.java)| [Stack of Linked List](DataStructures/Stacks/StackOfLinkedList.java)|[Binary Tree](DataStructures/Trees/BinaryTree.java)| -[Stacks](DataStructures/Stacks/Stacks.java)|And much more...| +[Array Stack](DataStructures/Stacks/StackArray.java)|And much more...| +[ArrayList Stack](DataStructures/Stacks/StackArrayList.java)|| * [Bags](DataStructures/Bags/Bag.java) * [Buffer](DataStructures/Buffers/CircularBuffer.java) From 29b7ad43d10c3ed63a1974a27bc2dc9b432ed8e1 Mon Sep 17 00:00:00 2001 From: Hayder Hassan Date: Fri, 4 Jan 2019 22:36:37 +0000 Subject: [PATCH 4/6] Remove Stacks.java which has been replaced by two new files StackArray.java & StackArrayList.java --- DataStructures/Stacks/Stacks.java | 240 ------------------------------ 1 file changed, 240 deletions(-) delete mode 100644 DataStructures/Stacks/Stacks.java diff --git a/DataStructures/Stacks/Stacks.java b/DataStructures/Stacks/Stacks.java deleted file mode 100644 index 2861ef5c..00000000 --- a/DataStructures/Stacks/Stacks.java +++ /dev/null @@ -1,240 +0,0 @@ -import java.util.ArrayList; - -/** - * This class implements a Stack using two different implementations. - * Stack is used with a regular array and Stack2 uses an ArrayList. - * - * A stack is exactly what it sounds like. An element gets added to the top of - * the stack and only the element on the top may be removed. This is an example - * of an array implementation of a Stack. So an element can only be added/removed - * from the end of the array. In theory stack have no fixed size, but with an - * array implementation it does. - * - * @author Unknown - * - */ -class Stack{ - /** The max size of the Stack */ - private int maxSize; - /** The array representation of the Stack */ - private int[] stackArray; - /** The top of the stack */ - private int top; - - /** - * Constructor - * - * @param size Size of the Stack - */ - public Stack(int size){ - maxSize = size; - stackArray = new int[maxSize]; - top = -1; - } - - /** - * Adds an element to the top of the stack - * - * @param value The element added - */ - public void push(int value){ - if(!isFull()){ //Checks for a full stack - top++; - stackArray[top] = value; - }else{ - resize(maxSize*2); - push(value);// don't forget push after resizing - } - } - - /** - * Removes the top element of the stack and returns the value you've removed - * - * @return value popped off the Stack - */ - public int pop(){ - if(!isEmpty()){ //Checks for an empty stack - return stackArray[top--]; - } - - if(top < maxSize/4){ - resize(maxSize/2); - return pop();// don't forget pop after resizing - } - else{ - System.out.println("The stack is already empty"); - return -1; - } - } - - /** - * Returns the element at the top of the stack - * - * @return element at the top of the stack - */ - public int peek(){ - if(!isEmpty()){ //Checks for an empty stack - return stackArray[top]; - }else{ - System.out.println("The stack is empty, cant peek"); - return -1; - } - } - - private void resize(int newSize){ - //private int[] transferArray = new int[newSize]; we can't put modifires here ! - int[] transferArray = new int[newSize]; - - //for(int i = 0; i < stackArray.length(); i++){ the length isn't a method . - for(int i = 0; i < stackArray.length; i++){ - transferArray[i] = stackArray[i]; - stackArray = transferArray; - } - maxSize = newSize; - } - - /** - * Returns true if the stack is empty - * - * @return true if the stack is empty - */ - public boolean isEmpty(){ - return(top == -1); - } - - /** - * Returns true if the stack is full - * - * @return true if the stack is full - */ - public boolean isFull(){ - return(top+1 == maxSize); - } - - /** - * Deletes everything in the Stack - * - * Doesn't delete elements in the array - * but if you call push method after calling - * makeEmpty it will overwrite previous - * values - */ - public void makeEmpty(){ //Doesn't delete elements in the array but if you call - top = -1; //push method after calling makeEmpty it will overwrite previous values - } -} - -/** - * This is an ArrayList Implementation of stack, Where size is not - * a problem we can extend the stack as much as we want. - * - * @author Unknown - * - */ -class Stack2{ - /** ArrayList representation of the stack */ - ArrayList stackList; - - /** - * Constructor - */ - Stack2(){ - 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 from the list - 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); - } - } - -/** - * This class implements the Stack and Stack2 created above - * - * @author Unknown - * - */ -public class Stacks{ - /** - * Main method - * - * @param args Command line arguments - */ - public static void main(String args[]){ - Stack myStack = new Stack(4); //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 Array Implementation*********************"); - System.out.println(myStack.isEmpty()); //will print false - System.out.println(myStack.isFull()); //will print true - System.out.println(myStack.peek()); //will print 9 - System.out.println(myStack.pop()); //will print 9 - System.out.println(myStack.peek()); // will print 2 - - Stack2 myStack2 = new Stack2(); //Declare a stack of maximum size 4 - //Populate the stack - myStack2.push(5); - myStack2.push(8); - myStack2.push(2); - myStack2.push(9); - - System.out.println("*********************Stack List Implementation*********************"); - System.out.println(myStack2.isEmpty()); //will print false - System.out.println(myStack2.peek()); //will print 9 - System.out.println(myStack2.pop()); //will print 9 - System.out.println(myStack2.peek()); // will print 2 - System.out.println(myStack2.pop()); //will print 2 - } -} From fc64d05b5c803d6b723d463e25dc600d9e9ede6e Mon Sep 17 00:00:00 2001 From: Libin Yang Date: Sun, 6 Jan 2019 08:56:51 +0800 Subject: [PATCH 5/6] Format code in StackArray --- DataStructures/Stacks/StackArray.java | 250 +++++++++++++------------- 1 file changed, 128 insertions(+), 122 deletions(-) diff --git a/DataStructures/Stacks/StackArray.java b/DataStructures/Stacks/StackArray.java index 795958e8..6e580918 100644 --- a/DataStructures/Stacks/StackArray.java +++ b/DataStructures/Stacks/StackArray.java @@ -1,6 +1,6 @@ /** * This class implements a Stack using a regular array. - * + *

* A stack is exactly what it sounds like. An element gets added to the top of * the stack and only the element on the top may be removed. This is an example * of an array implementation of a Stack. So an element can only be added/removed @@ -8,139 +8,145 @@ * array implementation it does. * * @author Unknown - * */ -public class StackArray{ +public class StackArray { - /** - * Main method - * - * @param args Command line arguments - */ - public static void main(String[] args) { - StackArray myStackArray = new StackArray(4); //Declare a stack of maximum size 4 - //Populate the stack - myStackArray.push(5); - myStackArray.push(8); - myStackArray.push(2); - myStackArray.push(9); + /** + * Main method + * + * @param args Command line arguments + */ + public static void main(String[] args) { + // Declare a stack of maximum size 4 + StackArray myStackArray = new StackArray(4); - System.out.println("*********************Stack Array Implementation*********************"); - System.out.println(myStackArray.isEmpty()); //will print false - System.out.println(myStackArray.isFull()); //will print true - System.out.println(myStackArray.peek()); //will print 9 - System.out.println(myStackArray.pop()); //will print 9 - System.out.println(myStackArray.peek()); // will print 2 - } + // Populate the stack + myStackArray.push(5); + myStackArray.push(8); + myStackArray.push(2); + myStackArray.push(9); - /** The max size of the Stack */ - private int maxSize; - - /** The array representation of the Stack */ - private int[] stackArray; - - /** The top of the stack */ - private int top; - - /** - * Constructor - * - * @param size Size of the Stack - */ - public StackArray(int size){ - maxSize = size; - stackArray = new int[maxSize]; - top = -1; - } - - /** - * Adds an element to the top of the stack - * - * @param value The element added - */ - public void push(int value){ - if(!isFull()){ //Checks for a full stack - top++; - stackArray[top] = value; - }else{ - resize(maxSize*2); - push(value);// don't forget push after resizing - } - } - - /** - * Removes the top element of the stack and returns the value you've removed - * - * @return value popped off the Stack - */ - public int pop(){ - if(!isEmpty()){ //Checks for an empty stack - return stackArray[top--]; + System.out.println("*********************Stack Array Implementation*********************"); + System.out.println(myStackArray.isEmpty()); // will print false + System.out.println(myStackArray.isFull()); // will print true + System.out.println(myStackArray.peek()); // will print 9 + System.out.println(myStackArray.pop()); // will print 9 + System.out.println(myStackArray.peek()); // will print 2 } - if(top < maxSize/4){ - resize(maxSize/2); - return pop();// don't forget pop after resizing + /** + * The max size of the Stack + */ + private int maxSize; + + /** + * The array representation of the Stack + */ + private int[] stackArray; + + /** + * The top of the stack + */ + private int top; + + /** + * Constructor + * + * @param size Size of the Stack + */ + public StackArray(int size) { + maxSize = size; + stackArray = new int[maxSize]; + top = -1; } - else{ - System.out.println("The stack is already empty"); - return -1; + + /** + * Adds an element to the top of the stack + * + * @param value The element added + */ + public void push(int value) { + if (!isFull()) { // Checks for a full stack + top++; + stackArray[top] = value; + } else { + resize(maxSize * 2); + push(value); // don't forget push after resizing + } } - } - /** - * Returns the element at the top of the stack - * - * @return element at the top of the stack - */ - public int peek(){ - if(!isEmpty()){ //Checks for an empty stack - return stackArray[top]; - }else{ - System.out.println("The stack is empty, cant peek"); - return -1; + /** + * Removes the top element of the stack and returns the value you've removed + * + * @return value popped off the Stack + */ + public int pop() { + if (!isEmpty()) { // Checks for an empty stack + return stackArray[top--]; + } + + if (top < maxSize / 4) { + resize(maxSize / 2); + return pop();// don't forget pop after resizing + } else { + System.out.println("The stack is already empty"); + return -1; + } } - } - private void resize(int newSize){ - //private int[] transferArray = new int[newSize]; we can't put modifires here ! - int[] transferArray = new int[newSize]; - - //for(int i = 0; i < stackArray.length(); i++){ the length isn't a method . - for(int i = 0; i < stackArray.length; i++){ - transferArray[i] = stackArray[i]; - stackArray = transferArray; + /** + * Returns the element at the top of the stack + * + * @return element at the top of the stack + */ + public int peek() { + if (!isEmpty()) { // Checks for an empty stack + return stackArray[top]; + } else { + System.out.println("The stack is empty, cant peek"); + return -1; + } } - maxSize = newSize; - } - /** - * Returns true if the stack is empty - * - * @return true if the stack is empty - */ - public boolean isEmpty(){ - return(top == -1); - } + private void resize(int newSize) { + // private int[] transferArray = new int[newSize]; we can't put modifiers here ! + int[] transferArray = new int[newSize]; - /** - * Returns true if the stack is full - * - * @return true if the stack is full - */ - public boolean isFull(){ - return(top+1 == maxSize); - } + // for(int i = 0; i < stackArray.length(); i++){ the length isn't a method . + for (int i = 0; i < stackArray.length; i++) { + transferArray[i] = stackArray[i]; + stackArray = transferArray; + } + maxSize = newSize; + } - /** - * Deletes everything in the Stack - * - * Doesn't delete elements in the array - * but if you call push method after calling - * makeEmpty it will overwrite previous - * values - */ - public void makeEmpty(){ //Doesn't delete elements in the array but if you call - top = -1; //push method after calling makeEmpty it will overwrite previous values - } -} \ No newline at end of file + /** + * Returns true if the stack is empty + * + * @return true if the stack is empty + */ + public boolean isEmpty() { + return (top == -1); + } + + /** + * Returns true if the stack is full + * + * @return true if the stack is full + */ + public boolean isFull() { + return (top + 1 == maxSize); + } + + /** + * Deletes everything in the Stack + *

+ * Doesn't delete elements in the array + * but if you call push method after calling + * makeEmpty it will overwrite previous + * values + */ + public void makeEmpty() { // Doesn't delete elements in the array but if you call + top = -1; // push method after calling makeEmpty it will overwrite previous values + } +} From 7c33d2e3132f7dfaa41dc1b2728df85926811c67 Mon Sep 17 00:00:00 2001 From: Libin Yang Date: Sun, 6 Jan 2019 09:01:33 +0800 Subject: [PATCH 6/6] Update StackArrayList.java --- DataStructures/Stacks/StackArrayList.java | 155 +++++++++++----------- 1 file changed, 78 insertions(+), 77 deletions(-) diff --git a/DataStructures/Stacks/StackArrayList.java b/DataStructures/Stacks/StackArrayList.java index 5093b76f..afc80444 100644 --- a/DataStructures/Stacks/StackArrayList.java +++ b/DataStructures/Stacks/StackArrayList.java @@ -2,93 +2,94 @@ import java.util.ArrayList; /** * This class implements a Stack using an ArrayList. - * + *

* A stack is exactly what it sounds like. An element gets added to the top of * the stack and only the element on the top may be removed. - * + *

* 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{ +public class StackArrayList { - /** - * Main method - * - * @param args Command line arguments - */ - public static void main(String[] args) { - StackArrayList myStackArrayList = new StackArrayList(); //Declare a stack of maximum size 4 - //Populate the stack - myStackArrayList.push(5); - myStackArrayList.push(8); - myStackArrayList.push(2); - myStackArrayList.push(9); + /** + * Main method + * + * @param args Command line arguments + */ + public static void main(String[] args) { + + StackArrayList myStackArrayList = new StackArrayList(); + + myStackArrayList.push(5); + myStackArrayList.push(8); + myStackArrayList.push(2); + myStackArrayList.push(9); - 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; - - /** - * Constructor - */ - public StackArrayList(){ - stackList = new ArrayList<>(); - } - - /** - * Adds value to the end of list which - * is the top for stack - * - * @param value value to be added - */ - public void push(int value){ - stackList.add(value); - } - - /** - * Pops last element of list which is indeed - * the top for Stack - * - * @return Element popped - */ - 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; + 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 } - System.out.print("The stack is already empty "); - return -1; - } + /** + * ArrayList representation of the stack + */ + private ArrayList stackList; - /** - * Checks for empty Stack - * - * @return true if stack is empty - */ - public boolean isEmpty(){ - return stackList.isEmpty(); - } + /** + * Constructor + */ + public StackArrayList() { + stackList = new ArrayList<>(); + } - /** - * Top element of stack - * - * @return top element of stack - */ - public int peek(){ - return stackList.get(stackList.size()-1); - } -} \ No newline at end of file + /** + * Adds value to the end of list which + * is the top for stack + * + * @param value value to be added + */ + public void push(int value) { + stackList.add(value); + } + + /** + * Pops last element of list which is indeed + * the top for Stack + * + * @return Element popped + */ + 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; + } + + System.out.print("The stack is already empty!"); + return -1; + } + + /** + * Checks for empty Stack + * + * @return true if stack is empty + */ + public boolean isEmpty() { + return stackList.isEmpty(); + } + + /** + * Top element of stack + * + * @return top element of stack + */ + public int peek() { + return stackList.get(stackList.size() - 1); + } +}