Char stack (#6248)

* Corrected Package name

* Char stack

* Changed file names
This commit is contained in:
Dhananjay Singh 2019-02-11 09:00:35 +05:30 committed by maibin
parent e6fc0bd94d
commit f894b40fe6
9 changed files with 265 additions and 3 deletions

View File

@ -0,0 +1,37 @@
package com.baeldung.charstack;
import java.util.Iterator;
import java.util.LinkedList;
public class CharStack {
private LinkedList<Character> items;
public CharStack() {
this.items = new LinkedList<Character>();
}
public void push(Character item) {
items.push(item);
}
public Character peek() {
return items.getFirst();
}
public Character pop() {
Iterator<Character> iter = items.iterator();
Character item = iter.next();
if (item != null) {
iter.remove();
return item;
}
return null;
}
public int size() {
return items.size();
}
}

View File

@ -0,0 +1,48 @@
package com.baeldung.charstack;
public class CharStackWithArray {
private char[] elements;
private int size;
public CharStackWithArray() {
size = 0;
elements = new char[4];
}
public int size() {
return size;
}
public char peek() {
if (size == 0) {
throw new EmptyStackException();
}
return elements[size - 1];
}
public char pop() {
if (size == 0) {
throw new EmptyStackException();
}
return elements[--size];
}
public void push(char item) {
ensureCapacity(size + 1);
elements[size] = item;
size++;
}
private void ensureCapacity(int newSize) {
char newBiggerArray[];
if (elements.length < newSize) {
newBiggerArray = new char[elements.length * 2];
System.arraycopy(elements, 0, newBiggerArray, 0, size);
elements = newBiggerArray;
}
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.charstack;
public class EmptyStackException extends RuntimeException {
public EmptyStackException() {
super("Stack is empty");
}
}

View File

@ -1,4 +1,4 @@
package com.baeldung.queueinterface;
package com.baeldung.queueInterface;
import java.util.AbstractQueue;
import java.util.Iterator;

View File

@ -0,0 +1,50 @@
package com.baeldung.charstack;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.Test;
public class CharStackUnitTest {
@Test
public void whenCharStackIsCreated_thenItHasSize0() {
CharStack charStack = new CharStack();
assertEquals(0, charStack.size());
}
@Test
public void givenEmptyCharStack_whenElementIsPushed_thenStackSizeisIncreased() {
CharStack charStack = new CharStack();
charStack.push('A');
assertEquals(1, charStack.size());
}
@Test
public void givenCharStack_whenElementIsPoppedFromStack_thenElementIsRemovedAndSizeChanges() {
CharStack charStack = new CharStack();
charStack.push('A');
char element = charStack.pop();
assertEquals('A', element);
assertEquals(0, charStack.size());
}
@Test
public void givenCharStack_whenElementIsPeeked_thenElementIsNotRemovedAndSizeDoesNotChange() {
CharStack charStack = new CharStack();
charStack.push('A');
char element = charStack.peek();
assertEquals('A', element);
assertEquals(1, charStack.size());
}
}

View File

@ -0,0 +1,53 @@
package com.baeldung.charstack;
import static org.junit.Assert.assertEquals;
import java.util.Stack;
import org.junit.jupiter.api.Test;
public class CharStackUsingJavaUnitTest {
@Test
public void whenCharStackIsCreated_thenItHasSize0() {
Stack<Character> charStack = new Stack<>();
assertEquals(0, charStack.size());
}
@Test
public void givenEmptyCharStack_whenElementIsPushed_thenStackSizeisIncreased() {
Stack<Character> charStack = new Stack<>();
charStack.push('A');
assertEquals(1, charStack.size());
}
@Test
public void givenCharStack_whenElementIsPoppedFromStack_thenElementIsRemovedAndSizeChanges() {
Stack<Character> charStack = new Stack<>();
charStack.push('A');
char element = charStack.pop();
assertEquals('A', element);
assertEquals(0, charStack.size());
}
@Test
public void givenCharStack_whenElementIsPeeked_thenElementIsNotRemovedAndSizeDoesNotChange() {
Stack<Character> charStack = new Stack<>();
charStack.push('A');
char element = charStack.peek();
assertEquals('A', element);
assertEquals(1, charStack.size());
}
}

View File

@ -0,0 +1,65 @@
package com.baeldung.charstack;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.Test;
public class CharStackWithArrayUnitTest {
@Test
public void whenCharStackIsCreated_thenItHasSize0() {
CharStackWithArray charStack = new CharStackWithArray();
assertEquals(0, charStack.size());
}
@Test
public void givenEmptyCharStack_whenElementIsPushed_thenStackSizeisIncreased() {
CharStackWithArray charStack = new CharStackWithArray();
charStack.push('A');
assertEquals(1, charStack.size());
}
@Test
public void givenEmptyCharStack_when5ElementIsPushed_thenStackSizeis() {
CharStackWithArray charStack = new CharStackWithArray();
charStack.push('A');
charStack.push('B');
charStack.push('C');
charStack.push('D');
charStack.push('E');
assertEquals(5, charStack.size());
}
@Test
public void givenCharStack_whenElementIsPoppedFromStack_thenElementIsRemovedAndSizeChanges() {
CharStackWithArray charStack = new CharStackWithArray();
charStack.push('A');
char element = charStack.pop();
assertEquals('A', element);
assertEquals(0, charStack.size());
}
@Test
public void givenCharStack_whenElementIsPeeked_thenElementIsNotRemovedAndSizeDoesNotChange() {
CharStackWithArray charStack = new CharStackWithArray();
charStack.push('A');
char element = charStack.peek();
assertEquals('A', element);
assertEquals(1, charStack.size());
}
}

View File

@ -1,4 +1,4 @@
package com.baeldung.queueinterface;
package com.baeldung.queueInterface;
import org.junit.Before;
import org.junit.Test;

View File

@ -1,4 +1,4 @@
package com.baeldung.queueinterface;
package com.baeldung.queueInterface;
import org.junit.Before;
import org.junit.Test;