forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: CoderXLoong <[email protected]>
- Loading branch information
1 parent
f462045
commit 9d07aea
Showing
22 changed files
with
1,452 additions
and
0 deletions.
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
group13/413007522/lesson05/src/main/java/cn/xl/basic/stack/MyStack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package cn.xl.basic.stack; | ||
|
||
import java.util.Arrays; | ||
import java.util.EmptyStackException; | ||
|
||
/** | ||
* Stack是一个后进先出(last in first out,LIFO)的堆栈, | ||
* 在Vector类的基础上扩展5个方法而来 | ||
* @author XIAOLONG | ||
* | ||
*/ | ||
public class MyStack { | ||
|
||
private int elementCount; | ||
|
||
private Object[] elementData = new Object[5]; | ||
|
||
/** | ||
* 无参构造方法,创建一个空栈 | ||
* | ||
*/ | ||
public MyStack(){ | ||
|
||
} | ||
|
||
|
||
/** | ||
* 元素入栈 | ||
* @param item | ||
* @return 入栈元素 | ||
*/ | ||
public synchronized Object push(Object item){ | ||
|
||
ensureCapacity(elementCount+1); | ||
elementData[elementCount] = item; | ||
elementCount ++; | ||
return item; | ||
} | ||
|
||
/** | ||
* 将栈顶元素移除,并返回该元素 | ||
* @return 栈顶元素 | ||
*/ | ||
public synchronized Object pop(){ | ||
Object obj; | ||
|
||
obj = peek(); | ||
elementCount --; | ||
elementData[elementCount] = null; | ||
|
||
return obj; | ||
} | ||
|
||
/** | ||
* 查看栈顶元素 | ||
* | ||
* @return 栈顶元素 | ||
* @throws 如果栈为空 , 抛出 EmptyStackException异常 . | ||
*/ | ||
public synchronized Object peek(){ | ||
int len = elementCount; | ||
|
||
if(len == 0) | ||
throw new EmptyStackException(); | ||
|
||
return elementData[len - 1]; | ||
|
||
} | ||
|
||
/** | ||
* 检查栈是否为空 | ||
* | ||
* @return True or false | ||
*/ | ||
public boolean isEmpty(){ | ||
|
||
return elementCount == 0; | ||
} | ||
|
||
/** | ||
* 查询占栈是否存在某元素 | ||
* @param 查询元素 | ||
* @return 如果元素存在返回元素所在位置(栈顶元素位置为1); | ||
* 如果该元素在栈中有重复,则返回距离栈顶最近的元素位置; | ||
* 如果该元素在栈中不存在,则返回 -1 。 | ||
*/ | ||
public synchronized int search(Object o){ | ||
|
||
if(o == null){ | ||
for(int i = elementCount -1;i >= 0; i--){ | ||
if(elementData[i] == null){ | ||
return elementCount - i; | ||
} | ||
} | ||
}else{ | ||
for(int i = elementCount -1;i >= 0; i-- ){ | ||
if(o.equals(elementData[i])){ | ||
return elementCount - i; | ||
} | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
/** | ||
* 扩展容量,增加自身容量的一倍 | ||
* @param 当前栈所需最小容量size | ||
*/ | ||
private void ensureCapacity(int minCapacity){ | ||
int oldCapacity = elementData.length; | ||
if(minCapacity > oldCapacity){ | ||
int newCapacity = oldCapacity << 1; | ||
elementData = Arrays.copyOf(elementData, newCapacity); | ||
} | ||
} | ||
|
||
public static void main(String[] args){ | ||
|
||
|
||
|
||
} | ||
|
||
} |
190 changes: 190 additions & 0 deletions
190
group13/413007522/lesson05/src/main/java/cn/xl/basic/stack/StackUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
package cn.xl.basic.stack; | ||
|
||
|
||
public class StackUtil { | ||
|
||
|
||
/** | ||
* 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 | ||
* 注意:只能使用MyStack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 | ||
*/ | ||
public static void reverse(MyStack s) { | ||
|
||
if(s == null || s.isEmpty()){ | ||
return ; | ||
} | ||
|
||
MyStack ns = new MyStack(); | ||
Object o = s.pop(); | ||
int count = 0; | ||
while(!s.isEmpty()){ | ||
ns.push(s.pop()); | ||
count ++; | ||
} | ||
s.push(o); | ||
|
||
while(!ns.isEmpty()){ | ||
s.push(ns.pop()); | ||
} | ||
|
||
for(int i = 0; i < count - 1 ;i++){ | ||
o = s.pop(); | ||
for(int j = 0; j < count - 1 - i ;j++){ | ||
ns.push(s.pop()); | ||
} | ||
s.push(o); | ||
while(!ns.isEmpty()){ | ||
s.push(ns.pop()); | ||
} | ||
} | ||
|
||
//组员写的 ,不错 | ||
/*MyStack a = new MyStack(); | ||
while (!s.isEmpty()){ | ||
Object o = s.pop(); | ||
int count =0; | ||
while (!a.isEmpty()){ | ||
s.push(a.pop()); | ||
count++; | ||
} | ||
a.push(o); | ||
for (int i = 0; i < count; i++) { | ||
a.push(s.pop()); | ||
} | ||
} | ||
while (!a.isEmpty()){ | ||
s.push(a.pop()); | ||
} | ||
return s;*/ | ||
} | ||
|
||
/** | ||
* 删除栈中的某个元素 注意:只能使用MyStack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 | ||
* | ||
* @param o | ||
*/ | ||
public static void remove(MyStack s,Object o) { | ||
|
||
if(s == null || s.isEmpty()){ | ||
return ; | ||
} | ||
|
||
MyStack ms = new MyStack(); | ||
Object obj = null; | ||
while(!s.isEmpty()){ | ||
obj = s.pop(); | ||
if(obj == null){ | ||
if(o == null){ | ||
continue; | ||
}else{ | ||
ms.push(obj); | ||
} | ||
}else{ | ||
if(obj.equals(o)|| obj.toString().equals(o)){ | ||
continue; | ||
}else{ | ||
ms.push(obj); | ||
} | ||
} | ||
} | ||
|
||
while(!ms.isEmpty()){ | ||
s.push(ms.pop()); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* 从栈顶取得len个元素, 原来的栈中元素保持不变 | ||
* 注意:只能使用MyStack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 | ||
* @param len | ||
* @return | ||
*/ | ||
public static Object[] getTop(MyStack s,int len) { | ||
MyStack ns = new MyStack(); | ||
int count = 0; | ||
Object[] o = new Object[len]; | ||
while(!s.isEmpty()){ | ||
ns.push(s.pop()); | ||
count++; | ||
} | ||
int n = 0; | ||
for(int i = 0; i < count - len;i++){ | ||
if(i < count - len){ | ||
s.push(ns.pop()); | ||
}else{ | ||
o[n] = ns.pop(); | ||
n++; | ||
} | ||
} | ||
|
||
return o; | ||
} | ||
/** | ||
* 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz | ||
* 使用堆栈检查字符串s中的括号是不是成对出现的。 | ||
* 例如s = "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true | ||
* 如果 s = "([b{x]y})", 则该字符串中的括号不是成对出现的, 该方法返回false; | ||
* @param s | ||
* @return | ||
*/ | ||
public static boolean isValidPairs(String s){ | ||
|
||
if(s == null || "".equals(s)){ | ||
return true; | ||
} | ||
char[] b = s.toCharArray(); | ||
MyStack ms = new MyStack(); | ||
String str = null; | ||
for(int i = 0; i < b.length ; i++){ | ||
str = String.valueOf(b[i]); | ||
if("(".equals(str)){ | ||
ms.push(b[i]); | ||
}else if(")".equals(str)){ | ||
if(!popObj(ms,"("))return false; | ||
}else if("[".equals(str)){ | ||
ms.push(b[i]); | ||
}else if("]".equals(str)){ | ||
if(!popObj(ms,"["))return false; | ||
}else if("{".equals(str)){ | ||
ms.push(b[i]); | ||
}else if("}".equals(str)){ | ||
if(!popObj(ms,"{"))return false; | ||
} | ||
} | ||
if(ms.isEmpty()){ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
|
||
private static boolean popObj(MyStack s,Object o) { | ||
|
||
MyStack ms = new MyStack(); | ||
Object obj = null; | ||
while(!s.isEmpty()){ | ||
obj = s.pop(); | ||
if(obj.equals(o)||obj.toString().equals(o)){ | ||
while(!ms.isEmpty()){ | ||
s.push(ms.pop()); | ||
} | ||
return true; | ||
}else{ | ||
ms.push(obj); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
|
||
|
||
public static void main(String[] args){ | ||
|
||
|
||
|
||
} | ||
|
||
} |
Oops, something went wrong.