From 5f2257017fc8719ba14d62a344e2e03c637cf476 Mon Sep 17 00:00:00 2001 From: asri71 Date: Sat, 16 Mar 2019 12:19:07 +0530 Subject: [PATCH 1/3] Adding Stack implementation along with test class --- src/main/java/com/dataStructures/Stack.java | 148 ++++++++++++++++++ .../java/com/dataStructures/StackTest.java | 119 ++++++++++++++ 2 files changed, 267 insertions(+) create mode 100644 src/main/java/com/dataStructures/Stack.java create mode 100644 src/test/java/com/dataStructures/StackTest.java diff --git a/src/main/java/com/dataStructures/Stack.java b/src/main/java/com/dataStructures/Stack.java new file mode 100644 index 00000000..5298d652 --- /dev/null +++ b/src/main/java/com/dataStructures/Stack.java @@ -0,0 +1,148 @@ +package src.main.java.com.dataStructures; + +import java.io.Serializable; +import java.util.EmptyStackException; +import java.util.Vector; + +public class Stack extends Vector implements Serializable { + + /** + * Inital capacity alloted to stack on object creation + */ + private final int INITIAL_CAPACITY = 10; + + /** + * Increment in memory space once stack is out of space + */ + private final int EXNTEDED_CAPACITY = 10; + + + /** + * Position of tail in stack + */ + + private int tail = -1; + + /** + * Size of stack at any given time + */ + + private int size; + + /** + * Uninitialized array to hold stack elements. + * WIll be intialized with inital capacity once the object is created + */ + private Object[] elements; + + /** + * No argument to create stack object with inital capacity + */ + public Stack() { + + elements = new Object[INITIAL_CAPACITY]; + } + + /** + * Method to check if the given stack is empty or not + */ + + public boolean empty() { + + if(null == elements) { + return true; + } + + if(size == 0){ + return true; + } + + return false; + } + + + /** + * Method to check the element on head without removing it + */ + + public Object peek() { + + if(empty()) { + throw new EmptyStackException(); + } + + return elements[tail]; + } + + /** + * Method to remove the top element from stack + */ + + public Object pop() { + + if(empty()) { + throw new EmptyStackException(); + } + + Object removedElement = elements[tail]; + tail--; + size--; + return removedElement; + } + + /** + * Method to add element to stack + */ + public Object push(Object e) { + + boolean isSuccess = false; + if(tail < (INITIAL_CAPACITY - 1)){ + tail++; + elements[tail] = e; + }else{ + Object[] extendedElements = new Object[INITIAL_CAPACITY + EXNTEDED_CAPACITY]; + System.arraycopy(elements, 0, extendedElements, 0, (tail+1)); + elements = extendedElements; + tail++; + elements[tail] = e; + } + size++; + return e; + + } + + /** + * Method to search for an element in stack + */ + + public int search(Object o) { + + int index = -1; + boolean found = false; + if(empty()) { + return -1; + } + + for(int i=0; i myStack = new Stack<>(); + boolean isEmpty = myStack.empty(); + Assert.assertTrue(isEmpty); + + myStack.push(10); + isEmpty = myStack.empty(); + Assert.assertFalse(isEmpty); + } + + @Test(expected = EmptyStackException.class) + public void testPeekWithoutElements() { + + Stack myStack = new Stack<>(); + myStack.peek(); + } + + @Test + public void testPeekWithElements() { + + Stack myStack = new Stack<>(); + myStack.push(10); + myStack.push(20); + myStack.push(30); + myStack.push(40); + + Assert.assertEquals(40, myStack.peek()); + } + + @Test(expected = EmptyStackException.class) + public void testPopWithoutElements() { + + Stack myStack = new Stack<>(); + myStack.pop(); + + } + + @Test + public void testPopWithElements() { + + Stack myStack = new Stack<>(); + myStack.push(10); + myStack.push(20); + myStack.push(30); + myStack.push(40); + myStack.push(50); + + Assert.assertEquals(50, myStack.pop()); + + } + + @Test + public void testPushWithinInitialCapacity() { + + Stack myStack = new Stack<>(); + myStack.push(10); + myStack.push(20); + myStack.push(30); + myStack.push(40); + myStack.push(50); + myStack.push(60); + myStack.push(70); + myStack.push(80); + myStack.push(90); + myStack.push(100); + Assert.assertEquals(10, myStack.size()); + } + + @Test + public void testPushOutsideInitialCapacity() { + + Stack myStack = new Stack<>(); + myStack.push(10); + myStack.push(20); + myStack.push(30); + myStack.push(40); + myStack.push(50); + myStack.push(60); + myStack.push(70); + myStack.push(80); + myStack.push(90); + myStack.push(100); + myStack.push(110); + Assert.assertEquals(11, myStack.size()); + } + + @Test + public void testSearchWithObjectUnavailable() { + + Stack myStack = new Stack<>(); + myStack.push(10); + myStack.push(20); + myStack.push(30); + Assert.assertEquals(-1,myStack.search(50)); + } + + @Test + public void testSearchWithObjectAvailable() { + + Stack myStack = new Stack<>(); + myStack.push(10); + myStack.push(20); + myStack.push(30); + Assert.assertEquals(3,myStack.search(10)); + + } +} From 27753bb8d1bfe1b1e7167eecfacc504c8894c549 Mon Sep 17 00:00:00 2001 From: asri71 Date: Sat, 16 Mar 2019 12:23:10 +0530 Subject: [PATCH 2/3] Removing inheritance from Vector class --- src/main/java/com/dataStructures/Stack.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/dataStructures/Stack.java b/src/main/java/com/dataStructures/Stack.java index 5298d652..aed57bd9 100644 --- a/src/main/java/com/dataStructures/Stack.java +++ b/src/main/java/com/dataStructures/Stack.java @@ -2,9 +2,8 @@ package src.main.java.com.dataStructures; import java.io.Serializable; import java.util.EmptyStackException; -import java.util.Vector; -public class Stack extends Vector implements Serializable { +public class Stack implements Serializable { /** * Inital capacity alloted to stack on object creation From 46df09ab86e06de0d0a556f8f4c55da5c86ccf90 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 16 Mar 2019 15:08:57 +0800 Subject: [PATCH 3/3] Update Stack.java Format code --- src/main/java/com/dataStructures/Stack.java | 51 ++++++++------------- 1 file changed, 19 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/dataStructures/Stack.java b/src/main/java/com/dataStructures/Stack.java index aed57bd9..0349a522 100644 --- a/src/main/java/com/dataStructures/Stack.java +++ b/src/main/java/com/dataStructures/Stack.java @@ -3,17 +3,17 @@ package src.main.java.com.dataStructures; import java.io.Serializable; import java.util.EmptyStackException; -public class Stack implements Serializable { +public class Stack implements Serializable { /** - * Inital capacity alloted to stack on object creation + * Initial capacity allocated to stack on object creation */ private final int INITIAL_CAPACITY = 10; /** * Increment in memory space once stack is out of space */ - private final int EXNTEDED_CAPACITY = 10; + private final int EXTENDED_CAPACITY = 10; /** @@ -30,15 +30,14 @@ public class Stack implements Serializable { /** * Uninitialized array to hold stack elements. - * WIll be intialized with inital capacity once the object is created + * WIll be initialized with initial capacity once the object is created */ private Object[] elements; /** - * No argument to create stack object with inital capacity + * No argument to create stack object with initial capacity */ public Stack() { - elements = new Object[INITIAL_CAPACITY]; } @@ -47,16 +46,7 @@ public class Stack implements Serializable { */ public boolean empty() { - - if(null == elements) { - return true; - } - - if(size == 0){ - return true; - } - - return false; + return elements == null || size == 0; } @@ -65,12 +55,11 @@ public class Stack implements Serializable { */ public Object peek() { + if (empty()) { + throw new EmptyStackException(); + } - if(empty()) { - throw new EmptyStackException(); - } - - return elements[tail]; + return elements[tail]; } /** @@ -78,8 +67,7 @@ public class Stack implements Serializable { */ public Object pop() { - - if(empty()) { + if (empty()) { throw new EmptyStackException(); } @@ -95,12 +83,12 @@ public class Stack implements Serializable { public Object push(Object e) { boolean isSuccess = false; - if(tail < (INITIAL_CAPACITY - 1)){ + if (tail < (INITIAL_CAPACITY - 1)) { tail++; elements[tail] = e; - }else{ - Object[] extendedElements = new Object[INITIAL_CAPACITY + EXNTEDED_CAPACITY]; - System.arraycopy(elements, 0, extendedElements, 0, (tail+1)); + } else { + Object[] extendedElements = new Object[INITIAL_CAPACITY + EXTENDED_CAPACITY]; + System.arraycopy(elements, 0, extendedElements, 0, (tail + 1)); elements = extendedElements; tail++; elements[tail] = e; @@ -118,19 +106,19 @@ public class Stack implements Serializable { int index = -1; boolean found = false; - if(empty()) { + if (empty()) { return -1; } - for(int i=0; i implements Serializable { public int size() { return size; } - }