From 487b4567d99a9b3130eaad1654d45183489aa621 Mon Sep 17 00:00:00 2001 From: macvis_qq75939388 Date: Thu, 4 May 2017 10:49:34 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=9A=82=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/miniJVM/engine/ExecutionResult.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/group24/75939388/learning2017/src/main/java/miniJVM/engine/ExecutionResult.java b/group24/75939388/learning2017/src/main/java/miniJVM/engine/ExecutionResult.java index d105e630de..8ec03090f0 100644 --- a/group24/75939388/learning2017/src/main/java/miniJVM/engine/ExecutionResult.java +++ b/group24/75939388/learning2017/src/main/java/miniJVM/engine/ExecutionResult.java @@ -49,9 +49,4 @@ public int getNextCmdOffset() { public void setNextCmdOffset(int nextCmdOffset) { this.nextCmdOffset = nextCmdOffset; } - - - - - } From 23c3b298f1c771f1bbb11d30abbbbb6daba766ec Mon Sep 17 00:00:00 2001 From: macvis_qq75939388 Date: Fri, 5 May 2017 14:09:00 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E7=AC=AC=E4=B9=9D=E5=91=A8=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=BB=93=E6=9E=84=E4=BD=9C=E4=B8=9A=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group24/.gitignore | 2 +- .../dataStructure/stack/QuickMinStack.java | 32 +++++ .../stack/StackWithTwoQueues.java | 50 +++++++ .../stack/TwoStackInOneArray.java | 130 ++++++++++++++++++ .../java/data_structure/stack/StackTest.java | 54 +++++++- 5 files changed, 265 insertions(+), 3 deletions(-) create mode 100644 group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/QuickMinStack.java create mode 100644 group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/StackWithTwoQueues.java create mode 100644 group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/TwoStackInOneArray.java diff --git a/group24/.gitignore b/group24/.gitignore index 2a5296f902..313344c8f2 100644 --- a/group24/.gitignore +++ b/group24/.gitignore @@ -1,4 +1,4 @@ -*.class +#*.class # Mobile Tools for Java (J2ME) .mtj.tmp/ diff --git a/group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/QuickMinStack.java b/group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/QuickMinStack.java new file mode 100644 index 0000000000..9827a6afa5 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/QuickMinStack.java @@ -0,0 +1,32 @@ +package basic.dataStructure.stack; + +/** + * 设计一个栈,支持栈的push和pop操作,以及第三种操作findMin, 它返回改数据结构中的最小元素 + * finMin操作最坏的情形下时间复杂度应该是O(1) , 简单来讲,操作一次就可以得到最小值 + * @author liuxin + * + */ +public class QuickMinStack { + + Stack s = null; + + private int min = -1; + + public QuickMinStack(){ + s = new Stack(); + } + + public void push(int data){ + if(s.isEmpty()){ + this.min = data; + }else this.min = data < min ? data : min; + + s.push(data); + } + public int pop(){ + return (Integer)s.pop(); + } + public int findMin(){ + return this.min; + } +} diff --git a/group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/StackWithTwoQueues.java b/group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/StackWithTwoQueues.java new file mode 100644 index 0000000000..6d7b46b930 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/StackWithTwoQueues.java @@ -0,0 +1,50 @@ +package basic.dataStructure.stack; + +import basic.dataStructure.queue.Queue; + +/** + * 用两个队列实现一个栈 + */ +public class StackWithTwoQueues { + + Queue queue1 = null; + Queue queue2 = null; + + private int data = -1; + + public StackWithTwoQueues(){ + queue1 = new Queue(); + queue2 = new Queue(); + } + public void push(int data) { + queue1.enQueue(data); + } + + public int pop() { + move(); + return this.data; + } + + + /** + * queue1是数据保存队列, queue2是缓存队列 + * 每次取之前将queue1的n-1个数据移动至queue2 + * 取出queue的最后一个数据即可 + */ + private void move(){ + if(queue2.isNotEmpty()){ + throw new RuntimeException("queue2 is not empty, operation aborted"); + } + + int size = queue1.size(); + for(int i = 0; i < size - 1; i++){ + queue2.enQueue(queue1.deQueue()); + } + + data = (Integer)queue1.deQueue(); + + for(int i = 0; i < size - 1; i++){ + queue1.enQueue(queue2.deQueue()); + } + } +} diff --git a/group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/TwoStackInOneArray.java b/group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/TwoStackInOneArray.java new file mode 100644 index 0000000000..a07ad44f5a --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/TwoStackInOneArray.java @@ -0,0 +1,130 @@ +package basic.dataStructure.stack; + +import java.util.Arrays; + +/** + * 用一个数组实现两个栈 + * 将数组的起始位置看作是第一个栈的栈底,将数组的尾部看作第二个栈的栈底, + * 压栈时,栈顶指针分别向中间移动,直到两栈顶指针相遇,则扩容。 + * @author liuxin + * + */ +public class TwoStackInOneArray { + Object[] data = new Object[10]; + + private int index1 = 0; + private int index2 = 9; + + private int size1 = 0; + private int size2 = 0; + + /** + * 向第一个栈中压入元素 + * @param o + */ + public void push1(Object o){ + data[index1] = o; + this.index1 ++; + this.size1 += 1; + + if(size1 + size2 >= data.length) extend(); + } + /** + * 从第一个栈中弹出元素 + * @return + */ + public Object pop1(){ + int index = index1 - 1; + Object obj = data[index]; + //直接置空 + data[index] = null; + this.index1 = index; + this.size1 --; + return obj; + } + + /** + * 获取第一个栈的栈顶元素 + * @return + */ + + public Object peek1(){ + int index = index1 - 1; + Object obj = data[index]; + + return obj; + } + /* + * 向第二个栈压入元素 + */ + public void push2(Object o){ + data[index2] = o; + index2 --; + this.size2 += 1; + + if(size1 + size2 >= data.length) extend(); + } + /** + * 从第二个栈弹出元素 + * @return + */ + public Object pop2(){ + int index = index2 + 1; + Object obj = data[index]; + + data[index] = null; + this.index2 = index; + this.size2--; + return obj; + } + /** + * 获取第二个栈的栈顶元素 + * @return + */ + + public Object peek2(){ + int index = index2 + 1; + Object obj = data[index]; + return obj; + } + + public int size1(){ + return this.size1; + } + + public int size2(){ + return this.size2; + } + + /** + * 扩容 + */ + private void extend(){ + int size = data.length; + int extendSize = 10; + Object[] extended = new Object[size + extendSize]; + + //stack1 + //因为指针在数据添加后已经加上了,所以要减回去 + for(int i = 0; i < index1; i++){ + extended[i] = data[i]; + } + + //stack2 + //因为指针在添加后已经减掉了1,所以要加回去 + for(int i = index2 + 1; i <= size - 1; i++){ + int index = i + extendSize; + extended[index] = data[i]; + } + this.data = extended; + this.index2 += extendSize; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(Arrays.toString(this.data)); + + return sb.toString(); + } +} \ No newline at end of file diff --git a/group24/75939388/learning2017/src/test/java/data_structure/stack/StackTest.java b/group24/75939388/learning2017/src/test/java/data_structure/stack/StackTest.java index 9c08fb031e..cbb1fbbe5e 100644 --- a/group24/75939388/learning2017/src/test/java/data_structure/stack/StackTest.java +++ b/group24/75939388/learning2017/src/test/java/data_structure/stack/StackTest.java @@ -1,7 +1,6 @@ package data_structure.stack; -import basic.dataStructure.stack.Stack; -import basic.dataStructure.stack.StackUtil; +import basic.dataStructure.stack.*; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -39,4 +38,55 @@ public void test2(){ } + /** + * QuickMinStack + * + * StackWithTwoQueues + * + * TwoStackInOneArray + */ + @Test + public void test3(){ + //QuickMinStack + { + QuickMinStack qms = new QuickMinStack(); + qms.push(0); + qms.push(-2); + qms.push(10); + Assert.assertEquals(-2, qms.findMin()); + } + //StackWithTwoQueues + { + StackWithTwoQueues stack = new StackWithTwoQueues(); + stack.push(0); + stack.push(2); + stack.push(5); + stack.push(10); + Assert.assertEquals(10, stack.pop()); + } + //TwoStackInOneArray + { + TwoStackInOneArray stack = new TwoStackInOneArray(); + for(int i = 0; i < 7; i++){ + stack.push1(i); + } + + for(int i = 0; i < 8; i++){ + stack.push2(i); + } + System.out.println("size1 ->" + stack.size1()); + System.out.println("size2 ->" + stack.size2()); + System.out.println("stack -> " + stack.toString()); + + Assert.assertEquals(stack.peek1(), 6); + Assert.assertEquals(stack.peek2(), 7); + + Assert.assertEquals(stack.pop1(), 6); + Assert.assertEquals(stack.size1(), 6); + + Assert.assertEquals(stack.pop2(), 7); + Assert.assertEquals(stack.size2(), 7); + } + } + } From 6ea81186e7de85dcdeea7bd7e3666b67e21ecfc1 Mon Sep 17 00:00:00 2001 From: macvis_qq75939388 Date: Fri, 5 May 2017 14:09:20 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=BB=93=E6=9E=84?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group24/75939388/.gitignore | 2 +- .../com/coderising/jvm/test/EmployeeV1.class | Bin 0 -> 1056 bytes 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 group24/75939388/learning2017/resources/classes/com/coderising/jvm/test/EmployeeV1.class diff --git a/group24/75939388/.gitignore b/group24/75939388/.gitignore index 596bd7346d..4923f59d8c 100644 --- a/group24/75939388/.gitignore +++ b/group24/75939388/.gitignore @@ -1,4 +1,4 @@ -*.class +#*.class target/ *.iml .idea/ \ No newline at end of file diff --git a/group24/75939388/learning2017/resources/classes/com/coderising/jvm/test/EmployeeV1.class b/group24/75939388/learning2017/resources/classes/com/coderising/jvm/test/EmployeeV1.class new file mode 100644 index 0000000000000000000000000000000000000000..c6c3c4a41546ae14b237a06a8f0868a61dc42cf5 GIT binary patch literal 1056 zcmah|?M@Rx6g^W)Kb93rYikjZ1yTEftSIUa3<)tBNGh6Onh<{tWrEx6cAM^&q>rTu zkZ9rq_)x|((;~rQ%x-4yow@hibI!Cse|`G_U=uIWNC>R7!oY2X$ExQ=UgyMZp9XHM zqS$>Cbp3FkRC6PZq(H7MPo?Wimb^b|t5z(K?8ra~_q~rjsJ@y*L0-feIuoK14Yd+#flv*C|NO#Gm#wZD&G&YSj18q z_pbSKFo zqck206pYW;F>F*&ri8AB^*dx3NROXV<zW&u2{pkVSAq|3&P2R7ekXatSzM=>9r9e) z&D1Vn*Xm~s7S1sH#WWPS=8@!@h zoMZKGYDKn`44lmtZNyM;7FE Date: Fri, 5 May 2017 14:10:46 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E6=94=B9=E5=9B=9Egitignore=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group24/.gitignore | 2 +- group24/75939388/.gitignore | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/group24/.gitignore b/group24/.gitignore index 313344c8f2..2a5296f902 100644 --- a/group24/.gitignore +++ b/group24/.gitignore @@ -1,4 +1,4 @@ -#*.class +*.class # Mobile Tools for Java (J2ME) .mtj.tmp/ diff --git a/group24/75939388/.gitignore b/group24/75939388/.gitignore index 4923f59d8c..596bd7346d 100644 --- a/group24/75939388/.gitignore +++ b/group24/75939388/.gitignore @@ -1,4 +1,4 @@ -#*.class +*.class target/ *.iml .idea/ \ No newline at end of file From 3cc3bb6d709b83c1f606ae7949d872c900709e35 Mon Sep 17 00:00:00 2001 From: macvis_qq75939388 Date: Fri, 5 May 2017 14:25:17 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E9=87=8D=E5=86=99toString?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../stack/TwoStackInOneArray.java | 266 ++++++++++-------- .../java/data_structure/stack/StackTest.java | 2 +- 2 files changed, 147 insertions(+), 121 deletions(-) diff --git a/group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/TwoStackInOneArray.java b/group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/TwoStackInOneArray.java index a07ad44f5a..964da94e7e 100644 --- a/group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/TwoStackInOneArray.java +++ b/group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/TwoStackInOneArray.java @@ -1,130 +1,156 @@ package basic.dataStructure.stack; -import java.util.Arrays; - /** * 用一个数组实现两个栈 * 将数组的起始位置看作是第一个栈的栈底,将数组的尾部看作第二个栈的栈底, * 压栈时,栈顶指针分别向中间移动,直到两栈顶指针相遇,则扩容。 - * @author liuxin * + * @author liuxin */ public class TwoStackInOneArray { - Object[] data = new Object[10]; - - private int index1 = 0; - private int index2 = 9; - - private int size1 = 0; - private int size2 = 0; - - /** - * 向第一个栈中压入元素 - * @param o - */ - public void push1(Object o){ - data[index1] = o; - this.index1 ++; - this.size1 += 1; - - if(size1 + size2 >= data.length) extend(); - } - /** - * 从第一个栈中弹出元素 - * @return - */ - public Object pop1(){ - int index = index1 - 1; - Object obj = data[index]; - //直接置空 - data[index] = null; - this.index1 = index; - this.size1 --; - return obj; - } - - /** - * 获取第一个栈的栈顶元素 - * @return - */ - - public Object peek1(){ - int index = index1 - 1; - Object obj = data[index]; - - return obj; - } - /* - * 向第二个栈压入元素 - */ - public void push2(Object o){ - data[index2] = o; - index2 --; - this.size2 += 1; - - if(size1 + size2 >= data.length) extend(); - } - /** - * 从第二个栈弹出元素 - * @return - */ - public Object pop2(){ - int index = index2 + 1; - Object obj = data[index]; - - data[index] = null; - this.index2 = index; - this.size2--; - return obj; - } - /** - * 获取第二个栈的栈顶元素 - * @return - */ - - public Object peek2(){ - int index = index2 + 1; - Object obj = data[index]; - return obj; - } - - public int size1(){ - return this.size1; - } - - public int size2(){ - return this.size2; - } - - /** - * 扩容 - */ - private void extend(){ - int size = data.length; - int extendSize = 10; - Object[] extended = new Object[size + extendSize]; - - //stack1 - //因为指针在数据添加后已经加上了,所以要减回去 - for(int i = 0; i < index1; i++){ - extended[i] = data[i]; - } - - //stack2 - //因为指针在添加后已经减掉了1,所以要加回去 - for(int i = index2 + 1; i <= size - 1; i++){ - int index = i + extendSize; - extended[index] = data[i]; - } - this.data = extended; - this.index2 += extendSize; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append(Arrays.toString(this.data)); - - return sb.toString(); - } + Object[] data = new Object[10]; + + private int index1 = 0; + private int index2 = 9; + + private int size1 = 0; + private int size2 = 0; + + /** + * 向第一个栈中压入元素 + * + * @param o + */ + public void push1(Object o) { + data[index1] = o; + this.index1++; + this.size1 += 1; + + if (size1 + size2 >= data.length) extend(); + } + + /** + * 从第一个栈中弹出元素 + * + * @return + */ + public Object pop1() { + int index = index1 - 1; + Object obj = data[index]; + //直接置空 + data[index] = null; + this.index1 = index; + this.size1--; + return obj; + } + + /** + * 获取第一个栈的栈顶元素 + * + * @return + */ + + public Object peek1() { + int index = index1 - 1; + Object obj = data[index]; + + return obj; + } + + /* + * 向第二个栈压入元素 + */ + public void push2(Object o) { + data[index2] = o; + index2--; + this.size2 += 1; + + if (size1 + size2 >= data.length) extend(); + } + + /** + * 从第二个栈弹出元素 + * + * @return + */ + public Object pop2() { + int index = index2 + 1; + Object obj = data[index]; + + data[index] = null; + this.index2 = index; + this.size2--; + return obj; + } + + /** + * 获取第二个栈的栈顶元素 + * + * @return + */ + + public Object peek2() { + int index = index2 + 1; + Object obj = data[index]; + return obj; + } + + public int size1() { + return this.size1; + } + + public int size2() { + return this.size2; + } + + /** + * 扩容 + */ + private void extend() { + int size = data.length; + int extendSize = 10; + Object[] extended = new Object[size + extendSize]; + + //stack1 + //因为指针在数据添加后已经加上了,所以要减回去 + for (int i = 0; i < index1; i++) { + extended[i] = data[i]; + } + + //stack2 + //因为指针在添加后已经减掉了1,所以要加回去 + for (int i = index2 + 1; i <= size - 1; i++) { + int index = i + extendSize; + extended[index] = data[i]; + } + this.data = extended; + this.index2 += extendSize; + } + + @Override + public String toString() { + + StringBuilder sb = new StringBuilder(); + sb.append("stack1 -> ["); + for (int i = index1 - 1; i >= 0; i--) { + sb.append(data[i]); + if (i == 0) { + sb.append("]"); + } else { + sb.append(","); + } + } + + sb.append("\nstack2 -> ["); + int length = data.length; + for (int i = index2 + 1; i < length; i++) { + sb.append(data[i]); + if (i == length - 1){ + sb.append("]"); + } else { + sb.append(","); + } + } + return sb.toString(); + } } \ No newline at end of file diff --git a/group24/75939388/learning2017/src/test/java/data_structure/stack/StackTest.java b/group24/75939388/learning2017/src/test/java/data_structure/stack/StackTest.java index cbb1fbbe5e..8656e4df25 100644 --- a/group24/75939388/learning2017/src/test/java/data_structure/stack/StackTest.java +++ b/group24/75939388/learning2017/src/test/java/data_structure/stack/StackTest.java @@ -76,7 +76,7 @@ public void test3(){ } System.out.println("size1 ->" + stack.size1()); System.out.println("size2 ->" + stack.size2()); - System.out.println("stack -> " + stack.toString()); + System.out.println(stack.toString()); Assert.assertEquals(stack.peek1(), 6); Assert.assertEquals(stack.peek2(), 7); From baea2f682f6af22bfaab20a5dd3d7916eee0e84a Mon Sep 17 00:00:00 2001 From: macvis_qq75939388 Date: Fri, 5 May 2017 14:33:53 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/basic/dataStructure/stack/TwoStackInOneArray.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/TwoStackInOneArray.java b/group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/TwoStackInOneArray.java index 964da94e7e..3123b17760 100644 --- a/group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/TwoStackInOneArray.java +++ b/group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/TwoStackInOneArray.java @@ -113,7 +113,7 @@ private void extend() { //stack1 //因为指针在数据添加后已经加上了,所以要减回去 - for (int i = 0; i < index1; i++) { + for (int i = 0; i <= index1 - 1; i++) { extended[i] = data[i]; } From 1e0889de3828a968d970b11b66cea0f838498967 Mon Sep 17 00:00:00 2001 From: macvis_qq75939388 Date: Fri, 5 May 2017 15:15:15 +0800 Subject: [PATCH 7/8] =?UTF-8?q?quickMinStack=E9=A2=98=E7=9B=AE=E7=90=86?= =?UTF-8?q?=E8=A7=A3=E9=94=99=E8=AF=AF=EF=BC=8C=E9=87=8D=E6=96=B0=E7=BC=96?= =?UTF-8?q?=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dataStructure/stack/QuickMinStack.java | 66 ++++++++++++++++--- .../java/data_structure/stack/StackTest.java | 6 ++ 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/QuickMinStack.java b/group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/QuickMinStack.java index 9827a6afa5..837863d873 100644 --- a/group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/QuickMinStack.java +++ b/group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/QuickMinStack.java @@ -8,25 +8,75 @@ */ public class QuickMinStack { - Stack s = null; + private int defaultSize = 10; + private int extendSize = 10; + Object[] elements = new Object[defaultSize]; private int min = -1; - public QuickMinStack(){ - s = new Stack(); + private int size = 0; + public QuickMinStack(){} + + public boolean isEmpty(){ + boolean flag = false; + for(int i = 0; i < elements.length; i++){ + if(elements[i] != null){ + return false; + } + + flag = elements == null; + } + return flag; } public void push(int data){ - if(s.isEmpty()){ + if(isEmpty()){ this.min = data; - }else this.min = data < min ? data : min; - - s.push(data); + }else{ + this.min = data < min ? data : min; + } + size ++; + elements[size - 1] = data; + if(size >= elements.length){ + extend(); + } } public int pop(){ - return (Integer)s.pop(); + Integer d = (Integer)elements[size - 1]; + elements[size - 1] = null; + size--; + return d; } + public int findMin(){ return this.min; } + + public int size(){ + return this.size; + } + + private void extend(){ + int curSize = elements.length; + Object[] arr = new Object[curSize + extendSize]; + + System.arraycopy(elements, 0, arr, 0, curSize); + + this.elements = arr; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("["); + for(int i = size - 1; i >= 0; i--){ + sb.append(elements[i]); + if(i > 0){ + sb.append(","); + }else{ + sb.append("]"); + } + } + + return sb.toString(); + } } diff --git a/group24/75939388/learning2017/src/test/java/data_structure/stack/StackTest.java b/group24/75939388/learning2017/src/test/java/data_structure/stack/StackTest.java index 8656e4df25..78503eff4b 100644 --- a/group24/75939388/learning2017/src/test/java/data_structure/stack/StackTest.java +++ b/group24/75939388/learning2017/src/test/java/data_structure/stack/StackTest.java @@ -53,7 +53,13 @@ public void test3(){ qms.push(0); qms.push(-2); qms.push(10); + for(int i = 0; i < 14; i++){ + qms.push(i); + } Assert.assertEquals(-2, qms.findMin()); + Assert.assertEquals(17, qms.size()); + Assert.assertEquals(13, qms.pop()); + System.out.println(qms.toString()); } //StackWithTwoQueues { From fc61b4c455ccbae1181895f59879ca61234a82cb Mon Sep 17 00:00:00 2001 From: macvis_qq75939388 Date: Fri, 5 May 2017 17:26:02 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E8=AE=BE=E8=AE=A1=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E7=BB=83=E4=B9=A0=EF=BC=8C=E4=BB=A3=E7=A0=81=E6=9A=82=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../designPattern/decorator/Beverage.java | 23 ++++++++++++ .../decorator/CondimentDecorator.java | 9 +++++ .../decorator/beverages/DarkRoast.java | 24 +++++++++++++ .../decorator/beverages/Decaf.java | 24 +++++++++++++ .../decorator/beverages/Espresso.java | 24 +++++++++++++ .../decorator/beverages/HouseBlend.java | 23 ++++++++++++ .../decorator/condiments/Mocha.java | 31 ++++++++++++++++ .../decorator/condiments/Soy.java | 31 ++++++++++++++++ .../decorator/condiments/Whip.java | 31 ++++++++++++++++ .../designPattern/StarBuzzCoffeeTest.java | 35 +++++++++++++++++++ 10 files changed, 255 insertions(+) create mode 100644 group24/75939388/learning2017/src/main/java/designPattern/decorator/Beverage.java create mode 100644 group24/75939388/learning2017/src/main/java/designPattern/decorator/CondimentDecorator.java create mode 100644 group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/DarkRoast.java create mode 100644 group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/Decaf.java create mode 100644 group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/Espresso.java create mode 100644 group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/HouseBlend.java create mode 100644 group24/75939388/learning2017/src/main/java/designPattern/decorator/condiments/Mocha.java create mode 100644 group24/75939388/learning2017/src/main/java/designPattern/decorator/condiments/Soy.java create mode 100644 group24/75939388/learning2017/src/main/java/designPattern/decorator/condiments/Whip.java create mode 100644 group24/75939388/learning2017/src/test/java/designPattern/StarBuzzCoffeeTest.java diff --git a/group24/75939388/learning2017/src/main/java/designPattern/decorator/Beverage.java b/group24/75939388/learning2017/src/main/java/designPattern/decorator/Beverage.java new file mode 100644 index 0000000000..e68e2e7b82 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/designPattern/decorator/Beverage.java @@ -0,0 +1,23 @@ +package designPattern.decorator; + +/** + * @author : 温友朝 + * @date : 2017/5/5 + */ +public abstract class Beverage { + public static final int TALL = 0; + public static final int GRANDE = 1; + public static final int VENTI = 2; + + + public String description = ""; + public static int size = TALL; + + public String getDescription(){ + return description; + } + + public abstract double cost(); + + public abstract int size(); +} diff --git a/group24/75939388/learning2017/src/main/java/designPattern/decorator/CondimentDecorator.java b/group24/75939388/learning2017/src/main/java/designPattern/decorator/CondimentDecorator.java new file mode 100644 index 0000000000..dca6ade059 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/designPattern/decorator/CondimentDecorator.java @@ -0,0 +1,9 @@ +package designPattern.decorator; + +/** + * @author : 温友朝 + * @date : 2017/5/5 + */ +public abstract class CondimentDecorator extends Beverage { + public abstract String getDescription(); +} diff --git a/group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/DarkRoast.java b/group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/DarkRoast.java new file mode 100644 index 0000000000..2c39efecf9 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/DarkRoast.java @@ -0,0 +1,24 @@ +package designPattern.decorator.beverages; + +import designPattern.decorator.Beverage; + +/** + * @author : 温友朝 + * @date : 2017/5/5 + */ +public class DarkRoast extends Beverage { + + public DarkRoast(){ + description = "Dark Roast"; + } + + @Override + public double cost() { + return 0.99; + } + + @Override + public int size() { + return 0; + } +} diff --git a/group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/Decaf.java b/group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/Decaf.java new file mode 100644 index 0000000000..bb4f757ad4 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/Decaf.java @@ -0,0 +1,24 @@ +package designPattern.decorator.beverages; + +import designPattern.decorator.Beverage; + +/** + * @author : 温友朝 + * @date : 2017/5/5 + */ +public class Decaf extends Beverage { + + public Decaf(){ + description = "Decaf"; + } + + @Override + public double cost() { + return 1.99; + } + + @Override + public int size() { + return 0; + } +} diff --git a/group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/Espresso.java b/group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/Espresso.java new file mode 100644 index 0000000000..dca41cc66c --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/Espresso.java @@ -0,0 +1,24 @@ +package designPattern.decorator.beverages; + +import designPattern.decorator.Beverage; + +/** + * @author : 温友朝 + * @date : 2017/5/5 + */ +public class Espresso extends Beverage { + + public Espresso(){ + description = "Espresso"; + } + + public double cost() { + return 1.99d; + } + + + @Override + public int size() { + return 0; + } +} diff --git a/group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/HouseBlend.java b/group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/HouseBlend.java new file mode 100644 index 0000000000..8b296aa3cf --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/HouseBlend.java @@ -0,0 +1,23 @@ +package designPattern.decorator.beverages; + +import designPattern.decorator.Beverage; + +/** + * @author : 温友朝 + * @date : 2017/5/5 + */ +public class HouseBlend extends Beverage { + + public HouseBlend(){ + description = "House Blend"; + } + + public double cost() { + return 0.89; + } + + @Override + public int size() { + return 0; + } +} diff --git a/group24/75939388/learning2017/src/main/java/designPattern/decorator/condiments/Mocha.java b/group24/75939388/learning2017/src/main/java/designPattern/decorator/condiments/Mocha.java new file mode 100644 index 0000000000..799399332f --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/designPattern/decorator/condiments/Mocha.java @@ -0,0 +1,31 @@ +package designPattern.decorator.condiments; + +import designPattern.decorator.Beverage; +import designPattern.decorator.CondimentDecorator; + +/** + * @author : 温友朝 + * @date : 2017/5/5 + */ +public class Mocha extends CondimentDecorator { + Beverage beverage; + + public Mocha(Beverage beverage){ + this.beverage = beverage; + + this.description += this.beverage.description + ", Mocha"; + } + + public String getDescription() { + return this.description; + } + + public double cost() { + return 0.2 + beverage.cost(); + } + + @Override + public int size() { + return 0; + } +} diff --git a/group24/75939388/learning2017/src/main/java/designPattern/decorator/condiments/Soy.java b/group24/75939388/learning2017/src/main/java/designPattern/decorator/condiments/Soy.java new file mode 100644 index 0000000000..ec4873c914 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/designPattern/decorator/condiments/Soy.java @@ -0,0 +1,31 @@ +package designPattern.decorator.condiments; + +import designPattern.decorator.Beverage; +import designPattern.decorator.CondimentDecorator; + +/** + * @author : 温友朝 + * @date : 2017/5/5 + */ +public class Soy extends CondimentDecorator { + Beverage beverage; + + public Soy(Beverage beverage){ + this.beverage = beverage; + + this.description += this.beverage.description + ", Soy"; + } + + public String getDescription() { + return this.description; + } + + public double cost() { + return 0.15 + beverage.cost(); + } + + @Override + public int size() { + return 0; + } +} diff --git a/group24/75939388/learning2017/src/main/java/designPattern/decorator/condiments/Whip.java b/group24/75939388/learning2017/src/main/java/designPattern/decorator/condiments/Whip.java new file mode 100644 index 0000000000..7b8cce87af --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/designPattern/decorator/condiments/Whip.java @@ -0,0 +1,31 @@ +package designPattern.decorator.condiments; + +import designPattern.decorator.Beverage; +import designPattern.decorator.CondimentDecorator; + +/** + * @author : 温友朝 + * @date : 2017/5/5 + */ +public class Whip extends CondimentDecorator { + Beverage beverage; + + public Whip(Beverage beverage){ + this.beverage = beverage; + + this.description += beverage.description + ", "; + } + + public String getDescription() { + return this.description; + } + + public double cost() { + return 0.1 + beverage.cost(); + } + + @Override + public int size() { + return 0; + } +} diff --git a/group24/75939388/learning2017/src/test/java/designPattern/StarBuzzCoffeeTest.java b/group24/75939388/learning2017/src/test/java/designPattern/StarBuzzCoffeeTest.java new file mode 100644 index 0000000000..8f04ce4ff1 --- /dev/null +++ b/group24/75939388/learning2017/src/test/java/designPattern/StarBuzzCoffeeTest.java @@ -0,0 +1,35 @@ +package designPattern; + +import designPattern.decorator.Beverage; +import designPattern.decorator.beverages.DarkRoast; +import designPattern.decorator.beverages.Espresso; +import designPattern.decorator.beverages.HouseBlend; +import designPattern.decorator.condiments.Mocha; +import designPattern.decorator.condiments.Soy; +import designPattern.decorator.condiments.Whip; +import org.junit.Test; + +/** + * @author : 温友朝 + * @date : 2017/5/5 + */ +public class StarBuzzCoffeeTest { + + @Test + public void test1(){ + Beverage espresso = new Espresso(); + System.out.println(espresso.getDescription() + " cost $" + espresso.cost()); + + Beverage darkRoast = new DarkRoast(); + darkRoast = new Mocha(darkRoast); + darkRoast = new Mocha(darkRoast); + darkRoast = new Whip(darkRoast); + System.out.println(darkRoast.getDescription() + " cost $" + darkRoast.cost()); + + Beverage houseBlend = new HouseBlend(); + houseBlend = new Soy(houseBlend); + houseBlend = new Mocha(houseBlend); + houseBlend = new Whip(houseBlend); + System.out.println(houseBlend.getDescription() + " cost $" + houseBlend.cost()); + } +}