forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Pxshuo163/master
01-提交作业-2017/2/25
- Loading branch information
Showing
20 changed files
with
935 additions
and
0 deletions.
There are no files selected for viewing
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,7 @@ | ||
/.metadata/ | ||
|
||
/RemoteSystemsTempFiles/ | ||
/.recommenders/ | ||
.settings/ | ||
.project | ||
.classpath |
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 @@ | ||
/bin/ |
15 changes: 15 additions & 0 deletions
15
group06/2415980327/CodeSE01/src/com/pxshuo/basic/Iterator.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,15 @@ | ||
package com.pxshuo.basic; | ||
|
||
public interface Iterator { | ||
/** | ||
* 查看是否有下一个元素 | ||
* @return 有的话返回true,否则返回false | ||
*/ | ||
public boolean hasNext(); | ||
/** | ||
* 获得下一个元素,也就是当前元素 | ||
* @return 获取当前位置的元素 | ||
*/ | ||
public Object next(); | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
group06/2415980327/CodeSE01/src/com/pxshuo/basic/List.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,37 @@ | ||
package com.pxshuo.basic; | ||
/** | ||
* 数组的接口 | ||
* @author Pxshuo | ||
* | ||
*/ | ||
|
||
public interface List { | ||
/** | ||
* 向数组中添加一个元素 | ||
* @param o 添加的元素 | ||
*/ | ||
public void add(Object o); | ||
/** | ||
* 向数组中某一个位置添加一个元素 | ||
* @param index 添加元素的位置 | ||
* @param o 添加的元素 | ||
*/ | ||
public void add(int index,Object o); | ||
/** | ||
* 从数组中根据位置获取一个元素 | ||
* @param index 想要获取的元素的位置 | ||
* @return 想获取的元素 | ||
*/ | ||
public Object get(int index); | ||
/** | ||
* 根据位置移除数组中的一个元素 | ||
* @param index 被移除元素的位置 | ||
* @return 被移除的元素 | ||
*/ | ||
public Object remove(int index); | ||
/** | ||
* 获取数组的长度 | ||
* @return 返回数组的长度 | ||
*/ | ||
public int size(); | ||
} |
24 changes: 24 additions & 0 deletions
24
group06/2415980327/CodeSE01/src/com/pxshuo/basic/TreeData.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,24 @@ | ||
package com.pxshuo.basic; | ||
|
||
public class TreeData implements Comparable<Object> { | ||
private int data; | ||
|
||
public TreeData(int data) { | ||
this.data = data; | ||
} | ||
|
||
public int getData() { | ||
return data; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return data + ""; | ||
} | ||
|
||
@Override | ||
public int compareTo(Object o) { | ||
return data - ((TreeData)o).data; | ||
} | ||
|
||
} |
138 changes: 138 additions & 0 deletions
138
group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/ArrayList.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,138 @@ | ||
package com.pxshuo.basic.impl; | ||
|
||
import com.pxshuo.basic.Iterator; | ||
import com.pxshuo.basic.List; | ||
|
||
/** | ||
* 实现一个ArrayList | ||
* @author Pxshuo | ||
* | ||
*/ | ||
|
||
public class ArrayList implements List{ | ||
|
||
private int size = -1;//数组的长度的下标 | ||
private Object[] elements = new Object[10];//数组内容 | ||
private int addSize = 10;//每次增加的长度 | ||
|
||
@Override | ||
public void add(Object o) { | ||
elements = grow(); | ||
size++; | ||
elements[size] = o;//size与index同一个概念 | ||
} | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
if (index > size + 1) { | ||
return; | ||
} | ||
elements = grow(); | ||
int moveNum = size - index + 1;//本次操作需要移动的元素的个数; | ||
size++; | ||
if (index >= elements.length - 1) {//按照位置来看 | ||
elements = grow(elements, index - (elements.length - 1)); | ||
size = index;//size与index同一个概念 | ||
} | ||
|
||
/** | ||
* 整体向后移一位 | ||
*/ | ||
if(moveNum > 0){ | ||
System.arraycopy(elements, index, elements, index + 1, moveNum); | ||
} | ||
// for(int i = size - 1; i >= index; i--) | ||
// { | ||
// elements[i] = elements[i-1]; | ||
// } | ||
|
||
elements[index] = o; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
return elements[index]; | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
if (index > size) { | ||
return null; | ||
} | ||
Object removeEle = elements[index]; | ||
int moveNum = size - index;//本次操作需要移动的元素的个数; | ||
if (moveNum > 0) { | ||
System.arraycopy(elements, index + 1, elements, index, size - index + 1); | ||
} | ||
elements[size] = null; | ||
size--; | ||
return removeEle; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size + 1; | ||
} | ||
|
||
/** | ||
* 设置迭代器 | ||
* @return | ||
*/ | ||
public Iterator iterator() { | ||
return new ArrayListIterator(this); | ||
} | ||
|
||
private class ArrayListIterator implements Iterator{ | ||
|
||
ArrayList arrayList = null; | ||
int position = -1; | ||
|
||
public ArrayListIterator(ArrayList arrayList) { | ||
this.arrayList = arrayList; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
position ++; | ||
if (position >= arrayList.size()) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
return arrayList.elements[position]; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* 自动控制是否增加数组长度 | ||
* @return 如果增加一条数据会造成数组溢出,则增加数组的长度,否则不进行改变。 | ||
*/ | ||
private Object[] grow(){ | ||
if (size() >= elements.length) { | ||
return grow(elements, addSize); | ||
} | ||
else { | ||
return elements; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* 动态增加数组长度 | ||
* @param src | ||
* @param addSize | ||
* @return | ||
*/ | ||
private Object[] grow(Object[] src,int addSize){ | ||
Object[] target = new Object[src.length + addSize]; | ||
System.arraycopy(src, 0, target, 0, src.length); | ||
return target; | ||
|
||
//return Arrays.copyOf(src, src.length + addSize);同理 | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTree.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,65 @@ | ||
package com.pxshuo.basic.impl; | ||
|
||
import com.pxshuo.basic.Iterator; | ||
|
||
/** | ||
* 排序二叉树 | ||
* @author Pxshuo | ||
* | ||
*/ | ||
|
||
public class BinaryTree { | ||
BinaryTreeNode root = null; | ||
|
||
/** | ||
* 添加一个二叉树的节点 | ||
* @param o | ||
*/ | ||
public void add(Comparable<Object> o){ | ||
if (root == null) { | ||
root = new BinaryTreeNode(); | ||
root.setData(o); | ||
} | ||
else { | ||
root.insert(o); | ||
} | ||
} | ||
|
||
public Object get(int index){ | ||
Stack findChild = childPath(index); | ||
BinaryTreeNode child = null; | ||
int childNum = 0; | ||
for(;!findChild.isEmpty();){ | ||
childNum = (int)findChild.pop(); | ||
if (childNum != -1) { | ||
child = child.getChild(childNum); | ||
} | ||
else { | ||
child = root; | ||
} | ||
} | ||
return child == null ? null : child.getData(); | ||
} | ||
|
||
public void display(){ | ||
root.display(1); | ||
} | ||
|
||
private Stack childPath(int index) { | ||
Stack findChild = new Stack(); | ||
|
||
while(true){ | ||
if (index == 1 || index <= 0) { | ||
findChild.push(-1); | ||
return findChild; | ||
} | ||
if (index%2 == 1) { | ||
findChild.push(1); | ||
} | ||
else { | ||
findChild.push(0); | ||
} | ||
index = index/2; | ||
} | ||
} | ||
} |
Oops, something went wrong.