forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 13
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 #3 from AllenLink/master
FirstHomework
- Loading branch information
Showing
10 changed files
with
536 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,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</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/ |
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,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>FirstWeek</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
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,103 @@ | ||
package myList; | ||
|
||
/* | ||
* ArrayList的底层是一个数组,通过重新创建新数组的方法,动态扩大数组的容量。 | ||
* 该ArrayList不允许隔空插入数据。 | ||
*/ | ||
|
||
public class MyArrayList { | ||
private int theSize; //当前大小 | ||
private static final int DEFAULT_CAPACITY=10; //默认容量 | ||
private Object[] theArr=new Object[10]; //底层数组 | ||
|
||
//初始化 | ||
public MyArrayList(){ | ||
clear(); | ||
} | ||
|
||
//清空 | ||
public void clear(){ | ||
theSize=0; | ||
capacityBigger(DEFAULT_CAPACITY); | ||
} | ||
|
||
//获取大小 | ||
public int size(){ | ||
return theSize; | ||
} | ||
|
||
//获取底层数组 | ||
public Object[] getArr(){ | ||
return theArr; | ||
|
||
} | ||
//插入,直接插入到数组尾部。 | ||
public void add(Object a){ | ||
add(theSize, a); | ||
} | ||
|
||
//根据下标获取数据 | ||
public Object get(int i){ | ||
if(i<0||i>=theSize){ | ||
throw new ArrayIndexOutOfBoundsException(); | ||
} | ||
return theArr[i]; | ||
} | ||
|
||
//插入,根据指定下标插入。 | ||
public void add(int i,Object a){ | ||
|
||
if(theSize==theArr.length){ //开始容量为10,每当成功添加了一个数据时,则size+1,当size和数组的大小相同时,则调用数组扩大方法,动态扩大数组的大小。 | ||
capacityBigger(size()); | ||
} | ||
for(int j=theSize-1;j>=i;j--){ | ||
theArr[j+1]=theArr[j]; | ||
} | ||
theArr[i]=a; | ||
|
||
theSize++; | ||
} | ||
|
||
//删除,根据下标删除数据。 | ||
public void remove(int i){ | ||
|
||
for(int j=i;j<theSize;j++){ | ||
theArr[j]=theArr[j+1]; | ||
} | ||
|
||
theSize--; | ||
} | ||
|
||
//数组扩大容量 | ||
public void capacityBigger(int Size){ | ||
Object[] newTheArr=new Object[Size*2]; | ||
for(int i=0;i<theArr.length;i++){ | ||
newTheArr[i]=theArr[i]; | ||
} | ||
theArr=newTheArr; | ||
} | ||
|
||
//获取MyIterator接口对象 | ||
public MyIterator myIterator(){ | ||
|
||
return myIterator(this); | ||
} | ||
|
||
public MyIterator myIterator(Object arr){ | ||
MyIterator i=new MyIterator(arr); | ||
return i; | ||
} | ||
|
||
|
||
//打印数组 | ||
public void print(){ | ||
for(int i=0;i<theSize;i++){ | ||
System.out.println(theArr[i]); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
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,113 @@ | ||
|
||
package myList; | ||
|
||
public class MyBinarySearchTree { | ||
|
||
private BinaryNode root; //根节点 | ||
|
||
//节点BinaryNode | ||
private static class BinaryNode{ | ||
|
||
Object element; //节点数据 | ||
BinaryNode left; //该节点的左节点 | ||
BinaryNode right; //该节点的右节点 | ||
|
||
BinaryNode(Object theElement){ | ||
this(theElement, null, null); | ||
} | ||
|
||
BinaryNode(Object element,BinaryNode left,BinaryNode right){ | ||
this.element=element; | ||
this.left=left; | ||
this.right=right; | ||
} | ||
} | ||
|
||
public MyBinarySearchTree(){ | ||
this.root=null; | ||
} | ||
|
||
//清空二叉树。 | ||
public void makeEmpty(){ | ||
root=null; | ||
} | ||
|
||
//判断是否为空。 | ||
public boolean isEmpty(){ | ||
return this.root==null; | ||
} | ||
|
||
//判断是否存在一个数。 | ||
public boolean contains(Object x,BinaryNode aNode){ | ||
|
||
//首先判断该二叉树是否为空,就是没有找到符合的子节点。 | ||
if(aNode==null){ | ||
return false; | ||
} | ||
|
||
//和当前的节点进行比较。 | ||
Integer comparaResult=(Integer)aNode.element-(Integer)x; | ||
|
||
//当数据小于当前节点的数据时,则该数据应该在当前节点的左孩子节点中。 | ||
if(comparaResult>0){ | ||
return contains(x,aNode.left); | ||
} | ||
else if(comparaResult<0){//当数据大于当前节点的数据时,则该数据应该在当前节点的右孩子节点中。 | ||
return contains(x,aNode.right); | ||
} | ||
else{ //当数据等于当前节点的数据时,则该数据应该在当前节点中。 | ||
return true; | ||
} | ||
} | ||
|
||
//插入数据。 | ||
public void insert(Object x){ | ||
root=insert(x,root); | ||
} | ||
|
||
public BinaryNode insert(Object x,BinaryNode aNode){ | ||
|
||
if(aNode==null){//当前为新的数据节点,因为是叶子节点,所以左右节点为null. | ||
return new BinaryNode(x,null,null); | ||
} | ||
|
||
//和当前的节点进行比较。 | ||
Integer comparaResult=(Integer)aNode.element-(Integer)x; | ||
|
||
//当数据小于当前节点的数据时,则该数据应该在当前节点的左孩子节点中。 | ||
if(comparaResult>0){ | ||
aNode.left= insert(x,aNode.left); | ||
} | ||
else if(comparaResult<0){//当数据大于当前节点的数据时,则该数据应该在当前节点的右孩子节点中。 | ||
aNode.right=insert(x,aNode.right); | ||
} | ||
else{ //当数据等于当前节点的数据时,则该数据应该在当前节点中,则不做任何操作。 | ||
; | ||
} | ||
return aNode; | ||
} | ||
|
||
//打印出二叉树。 | ||
public void getData(){ | ||
getData(root); | ||
} | ||
public void getData(BinaryNode root){ | ||
if (root != null) { | ||
//左孩子 | ||
this.getData(root.left); | ||
|
||
//右孩子 | ||
this.getData(root.right); | ||
//父节点 | ||
this.print(root); | ||
} | ||
|
||
} | ||
|
||
//打印节点。 | ||
public void print(BinaryNode root){ | ||
System.out.println( | ||
(Integer)(root.element) | ||
); | ||
} | ||
} |
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,40 @@ | ||
|
||
package myList; | ||
|
||
public class MyIterator { | ||
|
||
private Object aData; | ||
private int i=0; | ||
private int l=0; | ||
MyLinkedList.Node node; | ||
public MyIterator(Object aDate){ | ||
this.aData=aDate; | ||
} | ||
|
||
public boolean hasNext(){ | ||
if(aData instanceof MyArrayList){//MyArrayListµÄIterator | ||
|
||
Object[] arr=((MyArrayList) aData).getArr(); | ||
int a=((MyArrayList)aData).size(); | ||
return a>i; | ||
} | ||
else{//MyLinkedListµÄIterator | ||
node=((MyLinkedList)aData).getHeadNode();//»ñµÃÍ·½Úµã | ||
int a=((MyLinkedList)aData).size(); | ||
return a>l; | ||
} | ||
|
||
|
||
} | ||
public Object next(){ | ||
if(aData instanceof MyArrayList){//MyArrayListµÄIterator | ||
|
||
Object[] arr=((MyArrayList) aData).getArr(); | ||
return arr[++i]; | ||
} | ||
else{//MyLinkedListµÄIterator | ||
l++; | ||
return node.getDate(); | ||
} | ||
} | ||
} |
Oops, something went wrong.