From a4fd3930e49bb75405a04da7bc7bf062284e5f84 Mon Sep 17 00:00:00 2001 From: johnChnia Date: Tue, 25 Apr 2017 02:47:23 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=AD=E7=BC=80?= =?UTF-8?q?=E8=A1=A8=E8=BE=BE=E5=BC=8F=E8=BD=AC=E5=90=8E=E7=BC=80=E8=A1=A8?= =?UTF-8?q?=E8=BE=BE=E5=BC=8F=EF=BC=8C=E4=BD=BF=E5=AE=83=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=8B=AC=E5=8F=B7=EF=BC=8C=E4=BF=AE=E6=94=B9arraylist=EF=BC=8C?= =?UTF-8?q?=E4=BD=BF=E5=AE=83=E6=94=AF=E6=8C=81=E8=BF=AD=E4=BB=A3=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../johnChnia/coding2017/basic/ArrayList.java | 30 +++++- .../com/johnChnia/coding2017/basic/List.java | 18 ++-- .../coding2017/basic/linklist/LinkedList.java | 8 +- .../basic/stack/expr/InfixExprTest.java | 84 ++++++++-------- .../basic/stack/expr/InfixToPostfix.java | 18 +++- .../coding2017/basic/stack/expr/Token.java | 95 +++++++++++-------- .../basic/stack/expr/TokenParser.java | 9 ++ .../coding2017/basic/ArrayListTest.java | 14 ++- 8 files changed, 182 insertions(+), 94 deletions(-) diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java index 80bb60298e..d9a03f49a7 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java @@ -1,11 +1,13 @@ package com.johnChnia.coding2017.basic; import java.util.Arrays; +import java.util.Iterator; +import java.util.NoSuchElementException; /** * Created by john on 2017/3/8. * - * @// TODO: 2017/4/1 实现Iterator 接口 + * @// TODO: 学会多线程后,实现Iterator 的 remove 方法 */ public class ArrayList implements List { @@ -138,8 +140,32 @@ public int size() { return size; } + @Override + public Iterator iterator() { + return new Itr(); + } - /** + + private class Itr implements Iterator { + int cursor = 0; + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public E next() { + int i = cursor; + if (i >= size) { + throw new NoSuchElementException(); + } + Object[] elementData = ArrayList.this.elementData; + cursor = i + 1; + return (E) elementData[i]; + } + } + + /** * Increases the capacity to ensure that it can hold at least the * number of elements specified by the double length of list. */ diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/List.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/List.java index 6d5a8b01df..940403ce98 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/List.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/List.java @@ -1,16 +1,22 @@ package com.johnChnia.coding2017.basic; +import java.util.Iterator; + /** * Created by john on 2017/3/12. */ -public interface List { - public void add(E o); +public interface List extends Iterable{ + void add(E o); + + void add(int index, E o); + + E get(int index); + + E remove(int index); - public void add(int index, E o); + int size(); - public E get(int index); + Iterator iterator(); - public E remove(int index); - public int size(); } diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/linklist/LinkedList.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/linklist/LinkedList.java index 10f7edd0a6..c12f92a044 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/linklist/LinkedList.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/linklist/LinkedList.java @@ -2,12 +2,13 @@ import com.johnChnia.coding2017.basic.List; +import java.util.Iterator; import java.util.NoSuchElementException; /** * Created by john on 2017/3/9. * - * @// TODO: 2017/4/1 支持Iterator + * @// TODO: 学会多线程后,实现Iterator 的 remove 方法 */ public class LinkedList implements List { @@ -223,6 +224,11 @@ public int size() { return size; } + @Override + public Iterator iterator() { + return null; + } + private void checkElementIndex(int index) { if (!isElementIndex(index)) { throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/expr/InfixExprTest.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/expr/InfixExprTest.java index e47a8841da..3eb4a38819 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/expr/InfixExprTest.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/expr/InfixExprTest.java @@ -8,45 +8,49 @@ public class InfixExprTest { - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testEvaluate() { - //InfixExpr expr = new InfixExpr("300*20+12*5-20/4"); - { - InfixExpr expr = new InfixExpr("2+3*4+5"); - Assert.assertEquals(19.0, expr.evaluate(), 0.001f); - } - { - InfixExpr expr = new InfixExpr("3*20+12*5-40/2"); - Assert.assertEquals(100.0, expr.evaluate(), 0.001f); - } - - { - InfixExpr expr = new InfixExpr("3*20/2"); - Assert.assertEquals(30, expr.evaluate(), 0.001f); - } - - { - InfixExpr expr = new InfixExpr("20/2*3"); - Assert.assertEquals(30, expr.evaluate(), 0.001f); - } - - { - InfixExpr expr = new InfixExpr("10-30+50"); - Assert.assertEquals(30, expr.evaluate(), 0.001f); - } - { - InfixExpr expr = new InfixExpr("10-2*3+50"); - Assert.assertEquals(54, expr.evaluate(), 0.001f); - } - - } + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + //InfixExpr expr = new InfixExpr("300*20+12*5-20/4"); + { + InfixExpr expr = new InfixExpr("2+3*4+5"); + Assert.assertEquals(19.0, expr.evaluate(), 0.001f); + } + { + InfixExpr expr = new InfixExpr("3*20+12*5-40/2"); + Assert.assertEquals(100.0, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("3*20/2"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("20/2*3"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("10-30+50"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + { + InfixExpr expr = new InfixExpr("10-2*3+50"); + Assert.assertEquals(54, expr.evaluate(), 0.001f); + } + { + InfixExpr expr = new InfixExpr("10-2*(3+50)"); + Assert.assertEquals(-96, expr.evaluate(), 0.001f); + } + + } } diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/expr/InfixToPostfix.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/expr/InfixToPostfix.java index e361815f02..1efa28721f 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/expr/InfixToPostfix.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/expr/InfixToPostfix.java @@ -8,6 +8,10 @@ /*** * Rule: + * 1、如果栈不为空且操作符的优先级比栈顶高且不是(、【、{,则弹出栈顶,并且放到Expr中 + * 2、如果为(、[、{ 则压入栈顶 + * 3、如果为)、]、}且栈不为空,且栈顶不为(、[、{,则弹出栈顶,并且放到Expr中,最后弹出(、[、{ + * 4、遍历剩下的operator,加入到Expr中 */ public class InfixToPostfix { @@ -21,11 +25,19 @@ public static List convert(String expr) { if (token.isNumber()) { list.add(token); } else if (token.isOperator()) { - while (!stack.empty() && !token.hasHigherPriority(stack.peek())) { - list.add(stack.pop()); + while (!stack.empty() + && !token.isOpeningParentheses() + && !token.hasHigherPriority(stack.peek())) { + list.add(stack.pop()); } stack.push(token); - + } else if (token.isOpeningParentheses()) { + stack.push(token); + } else if (token.isClosingParentheses()) { + while (!stack.empty() && !stack.peek().isOpeningParentheses()) { + list.add(stack.pop()); + } + stack.pop(); } } while (!stack.empty()) { diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/expr/Token.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/expr/Token.java index f87a210587..3f99cdf609 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/expr/Token.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/expr/Token.java @@ -6,45 +6,60 @@ import java.util.Map; class Token { - public static final List OPERATORS = Arrays.asList("+", "-", "*", "/"); - private static final Map priorities = new HashMap<>(); - static { - priorities.put("+", 1); - priorities.put("-", 1); - priorities.put("*", 2); - priorities.put("/", 2); - } - static final int OPERATOR = 1; - static final int NUMBER = 2; - String value; - int type; - public Token(int type, String value){ - this.type = type; - this.value = value; - } - - public boolean isNumber() { - return type == NUMBER; - } - - public boolean isOperator() { - return type == OPERATOR; - } - - public int getIntValue() { - return Integer.valueOf(value).intValue(); - } - public String toString(){ - return value; - } - - public boolean hasHigherPriority(Token t){ - if(!this.isOperator() && !t.isOperator()){ - throw new RuntimeException("numbers can't compare priority"); - } - return priorities.get(this.value) - priorities.get(t.value) > 0; - } - - + public static final List OPERATORS = Arrays.asList("+", "-", "*", "/"); + public static final List OPENINGPARENTHESES = Arrays.asList("(", "[", "{"); + public static final List CLOSINGPARENTHESES = Arrays.asList(")", "]", "}"); + private static final Map priorities = new HashMap<>(); + static { + priorities.put("+", 1); + priorities.put("-", 1); + priorities.put("*", 2); + priorities.put("/", 2); + priorities.put("(", 0); + priorities.put("[", 0); + priorities.put("{", 0); + } + + static final int OPERATOR = 1; + static final int NUMBER = 2; + static final int PARENTHESES = 3; + String value; + int type; + + public Token(int type, String value) { + this.type = type; + this.value = value; + } + + public boolean isNumber() { + return type == NUMBER; + } + + public boolean isOperator() { + return type == OPERATOR; + } + + public int getIntValue() { + return Integer.valueOf(value).intValue(); + } + + public String toString() { + return value; + } + + public boolean hasHigherPriority(Token t) { + if (!this.isOperator() && !t.isOperator()) { + throw new RuntimeException("numbers can't compare priority"); + } + return priorities.get(this.value) - priorities.get(t.value) > 0; + } + + public boolean isOpeningParentheses() { + return OPENINGPARENTHESES.contains(this.value); + } + + public boolean isClosingParentheses() { + return CLOSINGPARENTHESES.contains(this.value); + } } \ No newline at end of file diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/expr/TokenParser.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/expr/TokenParser.java index b9c8b80444..e66e4ff8c0 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/expr/TokenParser.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/expr/TokenParser.java @@ -30,6 +30,10 @@ public List parse(String expr) { tokens.add(t); i = nextOperatorIndex; + } else if (isParentheses(c)) { + Token t = new Token(Token.PARENTHESES, String.valueOf(c)); + tokens.add(t); + i++; } else { System.out.println("char :[" + c + "] is not number or operator,ignore"); i++; @@ -55,4 +59,9 @@ private boolean isOperator(char c) { String sc = String.valueOf(c); return Token.OPERATORS.contains(sc); } + + private boolean isParentheses(char c) { + String sc = String.valueOf(c); + return Token.OPENINGPARENTHESES.contains(sc) || Token.CLOSINGPARENTHESES.contains(sc); + } } diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/ArrayListTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/ArrayListTest.java index 4b8d986990..5109ea69ad 100644 --- a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/ArrayListTest.java +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/ArrayListTest.java @@ -15,7 +15,7 @@ public class ArrayListTest { private ArrayList arrayList2; private ArrayList arrayList3; private ArrayList arrayList4; - private ArrayList arrayList5; + private ArrayList arrayList5; @Before public void setUp() throws Exception { @@ -23,6 +23,7 @@ public void setUp() throws Exception { arrayList2 = new ArrayList<>(3); arrayList3 = new ArrayList<>(3); arrayList4 = new ArrayList<>(3); + arrayList5 = new ArrayList<>(); } @Test @@ -58,5 +59,14 @@ public void testRemoveElementByIndex() { assertThat(arrayList4.size(), equalTo(5)); } - + @Test + public void testIterator() { + for (int i = 0; i < 6; i++) { + arrayList5.add(i); + } + for (int i: + arrayList5) { + System.out.println(i); + } + } } From 9ca8c88f596ac409ad9b18b38f9150afd29a9539 Mon Sep 17 00:00:00 2001 From: johnChnia Date: Tue, 25 Apr 2017 23:02:44 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E5=AE=9E=E7=8E=B0Linklist=20iterator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coding2017/basic/linklist/LinkedList.java | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/linklist/LinkedList.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/linklist/LinkedList.java index c12f92a044..31acd087d2 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/linklist/LinkedList.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/linklist/LinkedList.java @@ -226,7 +226,31 @@ public int size() { @Override public Iterator iterator() { - return null; + return new Itr(); + } + + private class Itr implements Iterator { + /** + * Index of element to be returned by subsequent call to next. + */ + int cursor = 0; + + + @Override + public boolean hasNext() { + return cursor != size(); + } + + @Override + public E next() { + int i = cursor; + if (i >= size) { + throw new NoSuchElementException(); + } + E next = get(i); + cursor = i + 1; + return next; + } } private void checkElementIndex(int index) { From 2f5f2e759b090c3606318f882e9ac3c138f76451 Mon Sep 17 00:00:00 2001 From: johnChnia Date: Thu, 27 Apr 2017 23:12:58 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E5=AE=9E=E7=8E=B0arraylist=20=E7=9A=84=20c?= =?UTF-8?q?ontains=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../johnChnia/coding2017/basic/ArrayList.java | 19 +++++++++++++++++++ .../com/johnChnia/coding2017/basic/List.java | 1 + .../coding2017/basic/linklist/LinkedList.java | 5 +++++ 3 files changed, 25 insertions(+) diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java index d9a03f49a7..1d48b259f8 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java @@ -174,6 +174,25 @@ private void grow() { 2 * elementData.length); } + public boolean contains(Object o) { + Itr itr = new Itr(); + if (o == null) { + while (itr.hasNext()) { + if (itr.next() == null) { + return true; + } + } + + } else { + while (itr.hasNext()) { + if (itr.next().equals(o)) { + return true; + } + } + } + return false; + } + public String toString() { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("["); diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/List.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/List.java index 940403ce98..7a399de95f 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/List.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/List.java @@ -18,5 +18,6 @@ public interface List extends Iterable{ Iterator iterator(); + boolean contains(Object o); } diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/linklist/LinkedList.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/linklist/LinkedList.java index 31acd087d2..4359918e74 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/linklist/LinkedList.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/linklist/LinkedList.java @@ -229,6 +229,11 @@ public Iterator iterator() { return new Itr(); } + @Override + public boolean contains(Object o) { + return false; + } + private class Itr implements Iterator { /** * Index of element to be returned by subsequent call to next. From 0ae26092d90ea06baf4c28abf5ab3594bdcc23bb Mon Sep 17 00:00:00 2001 From: johnChnia Date: Thu, 27 Apr 2017 23:19:44 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9classload=E6=94=AF?= =?UTF-8?q?=E6=8C=81ioutil=E3=80=81stringutil=EF=BC=8C=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9EloadClassFile=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/jvm/loader/ClassFileLoader.java | 108 ++++++++++++++---- 1 file changed, 83 insertions(+), 25 deletions(-) diff --git a/group24/315863321/src/main/java/jvm/loader/ClassFileLoader.java b/group24/315863321/src/main/java/jvm/loader/ClassFileLoader.java index a300edafc7..47ac60b253 100644 --- a/group24/315863321/src/main/java/jvm/loader/ClassFileLoader.java +++ b/group24/315863321/src/main/java/jvm/loader/ClassFileLoader.java @@ -3,54 +3,112 @@ import com.johnChnia.coding2017.basic.ArrayList; import com.johnChnia.coding2017.basic.List; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; import java.io.*; /** - * @// TODO: 2017/4/20 改成 try... with...resource - * @// TODO: 2017/4/20 close inputstream - * @// TODO: 2017/4/20 修改TreeInfo直接返回File + * Created by john on 2017/4/27. */ public class ClassFileLoader { private List clzPaths = new ArrayList(); public byte[] readBinaryCode(String className) { - for (int i = 0; i < clzPaths.size(); i++) { - // 找到指定类文件 - Directory.TreeInfo treeInfo = Directory.walk(clzPaths.get(i), className); - if (treeInfo.files.size() > 0) { - try { - FileInputStream fis = new FileInputStream(treeInfo.files.get(0)); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); // 自动增长 - byte[] buff = new byte[1024]; - int len; - while ((len = fis.read(buff)) != -1) { - bos.write(buff, 0, len); - } - return bos.toByteArray(); - } catch (Exception e) { - e.printStackTrace(); - } + className = className.replace('.', File.separatorChar) + ".class"; + for (String path : this.clzPaths) { + + String clzFileName = path + File.separatorChar + className; + byte[] codes = loadClassFile(clzFileName); + if (codes != null) { + return codes; } } + return null; + } + + private byte[] loadClassFile(String clzFileName) { + File f = new File(clzFileName); + try { + + return IOUtils.toByteArray(new FileInputStream(f)); + } catch (IOException e) { + e.printStackTrace(); + return null; + } } + public void addClassPath(String path) { - clzPaths.add(path); + if(this.clzPaths.contains(path)){ + return; + } + + this.clzPaths.add(path); } public String getClassPath() { - StringBuilder sb = new StringBuilder(); - for (int index = 0; index < clzPaths.size(); index++) { - sb.append(clzPaths.get(index)); - sb.append(";"); + return StringUtils.join(this.clzPaths,";"); + + } + + // ------------------------------backup------------------------ + public String getClassPath_V1(){ + + StringBuffer buffer = new StringBuffer(); + for(int i=0;i Date: Sat, 6 May 2017 18:56:12 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=BE=AA=E7=8E=AF?= =?UTF-8?q?=E9=98=9F=E5=88=97=E5=8F=8A=E4=BD=BF=E7=94=A82=E4=B8=AA?= =?UTF-8?q?=E6=A0=88=E5=AE=9E=E7=8E=B0=E9=98=9F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coderising2017/array/ArrayUtil.java | 2 +- .../coding2017/basic/BinarySearchTree.java | 2 + .../coding2017/basic/queue/CircleQueue.java | 104 ++++++++++++++++++ .../coding2017/basic/{ => queue}/Queue.java | 4 +- .../basic/queue/QueueWithTwoStacks.java | 71 ++++++++++++ .../johnChnia/coding2017/basic/QueueTest.java | 1 + .../basic/queue/CircleQueueTest.java | 38 +++++++ .../basic/queue/QueueWithTwoStacksTest.java | 37 +++++++ 8 files changed, 257 insertions(+), 2 deletions(-) create mode 100644 group24/315863321/src/main/java/com/johnChnia/coding2017/basic/queue/CircleQueue.java rename group24/315863321/src/main/java/com/johnChnia/coding2017/basic/{ => queue}/Queue.java (95%) create mode 100644 group24/315863321/src/main/java/com/johnChnia/coding2017/basic/queue/QueueWithTwoStacks.java create mode 100644 group24/315863321/src/test/java/com/johnChnia/coding2017/basic/queue/CircleQueueTest.java create mode 100644 group24/315863321/src/test/java/com/johnChnia/coding2017/basic/queue/QueueWithTwoStacksTest.java diff --git a/group24/315863321/src/main/java/com/johnChnia/coderising2017/array/ArrayUtil.java b/group24/315863321/src/main/java/com/johnChnia/coderising2017/array/ArrayUtil.java index 3126dafc53..53a0305753 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coderising2017/array/ArrayUtil.java +++ b/group24/315863321/src/main/java/com/johnChnia/coderising2017/array/ArrayUtil.java @@ -1,6 +1,6 @@ package com.johnChnia.coderising2017.array; -import com.johnChnia.coding2017.basic.Queue; +import com.johnChnia.coding2017.basic.queue.Queue; import com.johnChnia.coding2017.basic.ArrayList; diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/BinarySearchTree.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/BinarySearchTree.java index 48d3e41b12..a574b04794 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/BinarySearchTree.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/BinarySearchTree.java @@ -1,5 +1,7 @@ package com.johnChnia.coding2017.basic; +import com.johnChnia.coding2017.basic.queue.Queue; + /** * Created by john on 2017/3/13. */ diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/queue/CircleQueue.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/queue/CircleQueue.java new file mode 100644 index 0000000000..4fbe2ee11e --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/queue/CircleQueue.java @@ -0,0 +1,104 @@ +package com.johnChnia.coding2017.basic.queue; + +/** + * Created by john on 2017/5/6. + * + * NOTE:循环队列下一个为(i+1)%N,上一个为(i+N-1)%N + */ +public class CircleQueue { + private int rear = -1; + private int front = -1; + private Object[] array; + private static final int DEFAULTCAPACITY = 10; + + + /** + * 构造一个空的循环队列 + */ + public CircleQueue() { + array = new Object[DEFAULTCAPACITY]; + } + + /** + * 根据传进来的容量构造循环队列 + * + * @param capacity 队列容量 + */ + public CircleQueue(int capacity) { + array = new Object[capacity]; + } + + /** + * 循环队列长度 + * + * @return 队列长度 + */ + public int size() { + return array.length; + } + + /** + * 判断队列是否已经满了 + * + * @return 如果队列满的话返回true,否则返回false + */ + public boolean isFull() { + if ((rear + 1) % size() == front) { + return true; + } + return false; + } + + /** + * 判断队列是否为空 + * + * @return 如果队列为空返回true,否则返回false + */ + public boolean isEmpty() { + if (rear == -1 && front == -1) { + return true; + } + return false; + } + + /** + * 入队操作 + */ + public void enQueue(int item) throws Exception { + if (isFull()) { + throw new Exception("队列已满"); + } else if (isEmpty()) { + rear++; + front = rear; + array[rear] = item; + } else { + rear = (rear + 1) % size(); + array[rear] = item; + } + } + + /** + * 出队操作 + */ + public void deQueue() throws Exception { + if (isEmpty()) { + throw new Exception("队列为空"); + } else if (front == rear) { + front = -1; + rear = -1; + } else { + front = (front + 1) % size(); + } + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("("); + for (int i = 0; i <= size() - 1; i++) { + sb.append(array[i]); + sb.append(", "); + } + sb.append(")"); + return sb.toString(); + } +} diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Queue.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/queue/Queue.java similarity index 95% rename from group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Queue.java rename to group24/315863321/src/main/java/com/johnChnia/coding2017/basic/queue/Queue.java index 4d8449a8eb..4d82ad0c92 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Queue.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/queue/Queue.java @@ -1,4 +1,6 @@ -package com.johnChnia.coding2017.basic; +package com.johnChnia.coding2017.basic.queue; + +import com.johnChnia.coding2017.basic.ArrayList; import java.util.NoSuchElementException; diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/queue/QueueWithTwoStacks.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/queue/QueueWithTwoStacks.java new file mode 100644 index 0000000000..3fa8bb82fa --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/queue/QueueWithTwoStacks.java @@ -0,0 +1,71 @@ +package com.johnChnia.coding2017.basic.queue; + + +import com.johnChnia.coding2017.basic.stack.Stack; + +/** + * Created by john on 2017/5/6. + * + * 参考:https://leetcode.com/articles/implement-queue-using-stacks/ + */ + +public class QueueWithTwoStacks { + private Stack stack1; + private Stack stack2; + + + public QueueWithTwoStacks() { + stack1 = new Stack(); // 队列 + stack2 = new Stack(); + } + + + public boolean isEmpty() { + if (stack1.empty()) { + return true; + } + return false; + } + + + public int size() { + if (stack1.empty()) { + return -1; + } + return stack1.size(); + } + + + /** + * 如果stack1为空,则直接加到stack1中,如果不为空,首先遍历stack1中元素全放入stack2中,再把item加入 + * stack1中,最后再遍历stack2中元素重新放回stack1中。 + * + * @param item 入队值 + */ + public void enQueue(E item) { + while (!stack1.empty()) { + stack2.push(stack1.pop()); + } + stack1.push(item); + while (!stack2.empty()) { + stack1.push(stack2.pop()); + } + } + + /** + * 出队列操作 + * + * @return 如果队列为空就返回null,否则根据先进先出原则返回元素。 + */ + public E deQueue() { + if (isEmpty()) { + return null; + } + return stack1.pop(); + } + + public String toString() { + return stack1.toString(); + } +} + diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/QueueTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/QueueTest.java index 191c6f568d..22f17c4327 100644 --- a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/QueueTest.java +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/QueueTest.java @@ -1,5 +1,6 @@ package com.johnChnia.coding2017.basic; +import com.johnChnia.coding2017.basic.queue.Queue; import org.junit.Before; import org.junit.Test; diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/queue/CircleQueueTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/queue/CircleQueueTest.java new file mode 100644 index 0000000000..98a15c5c06 --- /dev/null +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/queue/CircleQueueTest.java @@ -0,0 +1,38 @@ +package com.johnChnia.coding2017.basic.queue; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by john on 2017/5/6. + */ +public class CircleQueueTest { + + CircleQueue cq1; + + @Before + public void setUp() throws Exception { + cq1 = new CircleQueue(); + } + + @After + public void tearDown() throws Exception { + + + } + + @Test + public void testEnQueueAndDeQueue() throws Exception { + for (int i = 0; i < 10; i++) { + cq1.enQueue(i); + } + cq1.deQueue(); + cq1.deQueue(); + cq1.enQueue(100); + cq1.enQueue(120); + Assert.assertEquals("(100, 120, 2, 3, 4, 5, 6, 7, 8, 9, )", cq1.toString()); + } + +} \ No newline at end of file diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/queue/QueueWithTwoStacksTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/queue/QueueWithTwoStacksTest.java new file mode 100644 index 0000000000..9c9d482513 --- /dev/null +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/queue/QueueWithTwoStacksTest.java @@ -0,0 +1,37 @@ +package com.johnChnia.coding2017.basic.queue; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by john on 2017/5/6. + */ +public class QueueWithTwoStacksTest { + QueueWithTwoStacks qwts; + + @Before + public void setUp() throws Exception { + qwts = new QueueWithTwoStacks<>(); + } + + @After + public void tearDown() throws Exception { + + + } + + @Test + public void testEnQueueAndDeQueue() throws Exception { + for (int i = 0; i < 10; i++) { + qwts.enQueue(i); + } + qwts.deQueue(); + qwts.deQueue(); + qwts.deQueue(); + Assert.assertEquals("3→4→5→6→7→8→9", qwts.toString()); + } + + +} \ No newline at end of file From 8f2b216714f180129f49bac1635f81af9c4c0ef9 Mon Sep 17 00:00:00 2001 From: johnChnia Date: Sun, 7 May 2017 04:24:20 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=BA=A6=E7=91=9F?= =?UTF-8?q?=E5=A4=AB=E7=8E=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coding2017/basic/queue/Josephus.java | 31 +++++++++++++++++++ .../coding2017/basic/queue/JosephusTest.java | 27 ++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 group24/315863321/src/main/java/com/johnChnia/coding2017/basic/queue/Josephus.java create mode 100644 group24/315863321/src/main/java/com/johnChnia/coding2017/basic/queue/JosephusTest.java diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/queue/Josephus.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/queue/Josephus.java new file mode 100644 index 0000000000..942b11cf68 --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/queue/Josephus.java @@ -0,0 +1,31 @@ +package com.johnChnia.coding2017.basic.queue; + + +import com.johnChnia.coding2017.basic.ArrayList; +import com.johnChnia.coding2017.basic.List; + +/** + * 用Queue来实现Josephus问题 + * 在这个古老的问题当中, N个深陷绝境的人一致同意用这种方式减少生存人数: N个人围成一圈(位置记为0到N-1), 并且从第一个人报数, 报到M的人会被杀死, 直到最后一个人留下来 + * 该方法返回一个List, 包含了被杀死人的次序 + * 参考 http://interactivepython.org/courselib/static/pythonds/BasicDS/SimulationHotPotato.html + * + * @author john + */ +public class Josephus { + + public static List execute(int n, int m) { + ArrayList arrayList = new ArrayList<>(); + Queue queue = new Queue<>(); + for (int i = 0; i < n; i++) { + queue.add(i); + } + while (queue.size() > 1) { + for (int i = 0; i < m - 1; i++) { + queue.add(queue.remove()); + } + arrayList.add(queue.remove()); + } + return arrayList; + } +} diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/queue/JosephusTest.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/queue/JosephusTest.java new file mode 100644 index 0000000000..ec71728b3e --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/queue/JosephusTest.java @@ -0,0 +1,27 @@ +package com.johnChnia.coding2017.basic.queue; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class JosephusTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testExecute() { + + Assert.assertEquals("[1, 3, 5, 0, 4, 2, ]", Josephus.execute(7, 2).toString()); + + } + +} From c6ded702576270688a15c9e18f3d2b67deabd511 Mon Sep 17 00:00:00 2001 From: johnChnia Date: Sun, 7 May 2017 17:18:05 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=94=A82=E4=B8=AA?= =?UTF-8?q?=E9=98=9F=E5=88=97=E5=AE=9E=E7=8E=B0=E6=A0=88=E4=BB=A5=E5=8F=8A?= =?UTF-8?q?=E7=94=A8=E4=B8=80=E4=B8=AA=E6=95=B0=E7=BB=84=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?2=E4=B8=AA=E6=A0=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../basic/stack/StackWithTwoQueues.java | 33 +++++ .../basic/stack/TwoStackInOneArray.java | 121 ++++++++++++++++++ .../basic/stack/StackWithTwoQueuesTest.java | 34 +++++ .../basic/stack/TwoStackInOneArrayTest.java | 48 +++++++ 4 files changed, 236 insertions(+) create mode 100644 group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/StackWithTwoQueues.java create mode 100644 group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/TwoStackInOneArray.java create mode 100644 group24/315863321/src/test/java/com/johnChnia/coding2017/basic/stack/StackWithTwoQueuesTest.java create mode 100644 group24/315863321/src/test/java/com/johnChnia/coding2017/basic/stack/TwoStackInOneArrayTest.java diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/StackWithTwoQueues.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/StackWithTwoQueues.java new file mode 100644 index 0000000000..156d042158 --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/StackWithTwoQueues.java @@ -0,0 +1,33 @@ +package com.johnChnia.coding2017.basic.stack; + +import com.johnChnia.coding2017.basic.queue.Queue; + +/** + * Created by john on 2017/5/7. + */ +public class StackWithTwoQueues { + + Queue queue1 = new Queue<>(); + Queue queue2 = new Queue<>(); + + public void push(int data) { + while (!queue1.empty()) { + queue2.add(queue1.remove()); + } + queue1.add(data); + while (!queue2.empty()) { + queue1.add(queue2.remove()); + } + } + + public int pop() { + if (!queue1.empty()) { + return queue1.remove(); + } + return -1; + } + + public String toString() { + return queue1.toString(); + } +} diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/TwoStackInOneArray.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/TwoStackInOneArray.java new file mode 100644 index 0000000000..75e6d15207 --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/TwoStackInOneArray.java @@ -0,0 +1,121 @@ +package com.johnChnia.coding2017.basic.stack; + +/** + * Created by john on 2017/5/7. + * 用一个数组实现两个栈 + * 将数组的起始位置看作是第一个栈的栈底,将数组的尾部看作第二个栈的栈底,压栈时,栈顶指针分别向中间移动,直到两栈顶指针相遇,则扩容。 + * + * @author john + */ +public class TwoStackInOneArray { + private Object[] data = new Object[4]; + private int pointer1 = -1; + private int pointer2 = data.length; + + + /** + * 向第一个栈中压入元素 + * + * @param o + */ + public void push1(Object o) { + ensureCapacityInternal(); + data[++pointer1] = o; + } + + /** + * 从第一个栈中弹出元素 + * + * @return + */ + public Object pop1() throws Exception { + if (pointer1 == -1) { + throw new Exception("栈1中没有元素"); + } else { + return data[pointer1--]; + } + } + + /** + * 获取第一个栈的栈顶元素 + * + * @return + */ + + public Object peek1() throws Exception { + if (pointer1 == -1) { + throw new Exception("栈1中没有元素"); + } else { + return data[pointer1]; + } + } + + /* + * 向第二个栈压入元素 + */ + public void push2(Object o) { + ensureCapacityInternal(); + data[--pointer2] = o; + } + + /** + * 从第二个栈弹出元素 + * + * @return + */ + public Object pop2() { + if (pointer2 == data.length) { + return null; + } else { + return data[pointer2++]; + } + } + + /** + * 获取第二个栈的栈顶元素 + * + * @return + */ + + public Object peek2() { + if (pointer2 == data.length) { + return null; + } else { + return data[pointer2]; + } + } + + /** + * 如果栈1的指针和栈2的指针之差为1的话,那么就需要扩容。 + */ + private void ensureCapacityInternal() { + if (pointer2 - pointer1 == 1) { + grow(); + } + } + + private void grow() { + Object[] data2 = new Object[data.length * 2]; + System.arraycopy(data, 0, data2, 0, pointer1 + 1); //复制栈1值到data2 + int desPosForStack2 = data2.length - pointer2; + System.arraycopy(data, pointer2, data2, desPosForStack2, pointer2); // 复制栈2值到data2 + data = data2; + pointer2 = desPosForStack2; // 调整栈2指针位置 + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("("); + for (int i = 0; i <= pointer1; i++) { + sb.append(data[i]); + sb.append(", "); + } + for (int j = pointer2; j < data.length; j++) { + sb.append(data[j]); + sb.append(", "); + } + sb.append(")"); + return sb.toString(); + } + +} diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/stack/StackWithTwoQueuesTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/stack/StackWithTwoQueuesTest.java new file mode 100644 index 0000000000..d051b349d1 --- /dev/null +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/stack/StackWithTwoQueuesTest.java @@ -0,0 +1,34 @@ +package com.johnChnia.coding2017.basic.stack; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by john on 2017/5/7. + */ +public class StackWithTwoQueuesTest { + StackWithTwoQueues stq; + @Before + public void setUp() throws Exception { + stq = new StackWithTwoQueues(); + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void testPushAndPop() throws Exception { + for (int i = 0; i < 6; i++) { + stq.push(i); + } + Assert.assertEquals("[5, 4, 3, 2, 1, 0, ]", stq.toString()); + stq.pop(); + Assert.assertEquals("[4, 3, 2, 1, 0, ]", stq.toString()); + } + + +} \ No newline at end of file diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/stack/TwoStackInOneArrayTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/stack/TwoStackInOneArrayTest.java new file mode 100644 index 0000000000..115c610b59 --- /dev/null +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/stack/TwoStackInOneArrayTest.java @@ -0,0 +1,48 @@ +package com.johnChnia.coding2017.basic.stack; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by john on 2017/5/7. + */ +public class TwoStackInOneArrayTest { + TwoStackInOneArray twoStackInOneArray; + + @Before + public void setUp() throws Exception { + twoStackInOneArray = new TwoStackInOneArray(); + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void testPushAndPopAndPeek() throws Exception { + twoStackInOneArray.push1(0); + twoStackInOneArray.push1(1); + twoStackInOneArray.push2(2); + twoStackInOneArray.push2(3); + twoStackInOneArray.push1(4); + twoStackInOneArray.push1(5); + twoStackInOneArray.push2(6); + twoStackInOneArray.push2(7); + twoStackInOneArray.push2(8); + Assert.assertEquals("(0, 1, 4, 5, 8, 7, 6, 3, 2, )", + twoStackInOneArray.toString()); + twoStackInOneArray.pop1(); + twoStackInOneArray.pop2(); + Assert.assertEquals("(0, 1, 4, 7, 6, 3, 2, )", + twoStackInOneArray.toString()); + + Assert.assertEquals(4, + twoStackInOneArray.peek1()); + Assert.assertEquals(7, + twoStackInOneArray.peek2()); + } + +} \ No newline at end of file From 552e2905925ba0f5b0ab1bfc10d8cc6f9d4410ea Mon Sep 17 00:00:00 2001 From: johnChnia Date: Sun, 7 May 2017 18:09:42 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E5=AE=8C=E6=88=90jvm=E7=AC=AC=E4=B9=9D?= =?UTF-8?q?=E5=91=A8=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coding2017/basic/stack/QuickMinStack.java | 72 +++++++++++++++++++ .../basic/stack/QuickMinStackTest.java | 53 ++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/QuickMinStack.java create mode 100644 group24/315863321/src/test/java/com/johnChnia/coding2017/basic/stack/QuickMinStackTest.java diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/QuickMinStack.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/QuickMinStack.java new file mode 100644 index 0000000000..a7031bf81e --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/stack/QuickMinStack.java @@ -0,0 +1,72 @@ +package com.johnChnia.coding2017.basic.stack; + +/** + * Created by john on 2017/5/7. + * 设计一个栈,支持栈的push和pop操作,以及第三种操作findMin, 它返回改数据结构中的最小元素 + * finMin操作最坏的情形下时间复杂度应该是O(1) , 简单来讲,操作一次就可以得到最小值 + * 参考:http://www.programcreek.com/2014/02/leetcode-min-stack-java/ + * + * @author john + */ + + +public class QuickMinStack { + + private static class Elem { + int value; + int min; + Elem next; + + Elem(int value, int min) { + this.value = value; + this.min = min; + } + } + + private Elem top; + + + public QuickMinStack() { + + } + + + public void push(int data) { + if (top == null) { + Elem e = new Elem(data, data); + top = e; + } else { + Elem temp = new Elem(data, Math.min(data, top.min)); + temp.next = top; + top = temp; + } + } + + public int pop() { + if (top == null) { + return -1; + } else { + Elem temp = top.next; + top.next = null; + top = temp; + return top.value; + } + } + + public int findMin() { + if (top == null) { + return -1; + } + return top.min; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + while (top != null) { + sb.append(top.value); + sb.append(","); + top = top.next; + } + return sb.toString(); + } +} diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/stack/QuickMinStackTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/stack/QuickMinStackTest.java new file mode 100644 index 0000000000..232f988db2 --- /dev/null +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/stack/QuickMinStackTest.java @@ -0,0 +1,53 @@ +package com.johnChnia.coding2017.basic.stack; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by john on 2017/5/7. + */ +public class QuickMinStackTest { + QuickMinStack quickMinStack1; + QuickMinStack quickMinStack2; + QuickMinStack quickMinStack3; + @Before + public void setUp() throws Exception { + quickMinStack1 = new QuickMinStack(); + quickMinStack2 = new QuickMinStack(); + quickMinStack3 = new QuickMinStack(); + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void testPush() throws Exception { + for (int i = 0; i < 4; i++) { + quickMinStack1.push(i); + } + Assert.assertEquals("3,2,1,0,", quickMinStack1.toString()); + } + + @Test + public void testPop() throws Exception { + for (int i = 0; i < 4; i++) { + quickMinStack2.push(i); + } + quickMinStack2.pop(); + Assert.assertEquals("2,1,0,", quickMinStack2.toString()); + } + + @Test + public void testFindMin() throws Exception { + for (int i = 0; i < 4; i++) { + quickMinStack3.push(i); + } + quickMinStack3.pop(); + Assert.assertEquals(0, quickMinStack3.findMin()); + } + +} \ No newline at end of file