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.
Merge branch 'master' of https://github.com/jiangshaojie/coding2017
- Loading branch information
Showing
215 changed files
with
11,743 additions
and
6 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
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 @@ | ||
this is my projectWorkcpace! |
94 changes: 94 additions & 0 deletions
94
group12/2258659044/zj-2017/src/com/coding/basic/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,94 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
/*扩容因子*/ | ||
private static final int GENE = 10; | ||
|
||
private Object[] elementData = new Object[10]; | ||
/*扩容引用*/ | ||
private Object[] newElementData; | ||
|
||
public void add(Object o){ | ||
grow(); | ||
elementData[size] = o; | ||
size ++; | ||
} | ||
public void add(int index, Object o){ | ||
|
||
if(index>size){ | ||
throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); | ||
} | ||
grow(); | ||
if(index<size){//长度足够需要移动 | ||
newElementData = new Object[elementData.length]; | ||
System.arraycopy(elementData, 0, newElementData, 0, index); | ||
System.arraycopy(elementData, index, newElementData, index+1, size-index); | ||
elementData = newElementData; | ||
} | ||
elementData[index] = o; | ||
size ++; | ||
} | ||
|
||
public Object get(int index){ | ||
|
||
if(index>size){ | ||
throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); | ||
} | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index){ | ||
|
||
Object o = elementData[index]; | ||
System.arraycopy(elementData, index+1, elementData, index, size-(index+1)); | ||
size --; | ||
return o; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
/** | ||
* 扩容,扩容因子为10 | ||
*/ | ||
private void grow(){ | ||
|
||
if(size>=elementData.length){//长度不够需要扩容 | ||
newElementData = new Object[size+GENE]; | ||
System.arraycopy(elementData, 0, newElementData, 0, elementData.length); | ||
elementData = newElementData; | ||
} | ||
} | ||
|
||
|
||
public Iterator iterator(){ | ||
|
||
return new Itr(); | ||
} | ||
|
||
private class Itr implements Iterator{ | ||
|
||
int cursor; | ||
@Override | ||
public boolean hasNext() { | ||
return cursor != ArrayList.this.size; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
|
||
int i = this.cursor; | ||
if (i >= ArrayList.this.size){ | ||
throw new NoSuchElementException(); | ||
} | ||
this.cursor = (i + 1); | ||
return ArrayList.this.elementData[i]; | ||
} | ||
|
||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
group12/2258659044/zj-2017/src/com/coding/basic/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,64 @@ | ||
package com.coding.basic; | ||
|
||
public class BinaryTree { | ||
|
||
//根节点 | ||
private BinaryTreeNode root; | ||
|
||
@SuppressWarnings({ "rawtypes", "unchecked" }) | ||
public <T extends Comparable<? super T>> BinaryTreeNode insert(T o){ | ||
|
||
BinaryTreeNode treeNode = new BinaryTreeNode(); | ||
treeNode.setData(o); | ||
if(root == null){ | ||
root = treeNode; | ||
}else{ | ||
BinaryTreeNode currentNode = root; | ||
BinaryTreeNode parent; | ||
while(true){ | ||
parent = currentNode; | ||
if(((Comparable)currentNode.getData()).compareTo(o)>0){//向左放 | ||
currentNode = currentNode.getLeft(); | ||
if(currentNode == null){ | ||
parent.setLeft(treeNode); | ||
treeNode.setParent(parent); | ||
break; | ||
} | ||
}else{//向右放 | ||
currentNode = currentNode.getRight(); | ||
if(currentNode == null){ | ||
parent.setRight(treeNode); | ||
treeNode.setParent(parent); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
return treeNode; | ||
} | ||
|
||
/** | ||
* 先序遍历 | ||
* @param node | ||
* @return | ||
*/ | ||
public List traversalBefore(BinaryTreeNode node){ | ||
//所有数据集合 | ||
List datas = new ArrayList(); | ||
return traversal(node,datas); | ||
} | ||
private List traversal(BinaryTreeNode node,List datas){ | ||
|
||
if(node !=null){ | ||
datas.add(node.getData()); | ||
traversal(node.getLeft(),datas); | ||
traversal(node.getRight(),datas); | ||
} | ||
return datas; | ||
} | ||
|
||
public BinaryTreeNode getRoot() { | ||
return root; | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
group12/2258659044/zj-2017/src/com/coding/basic/BinaryTreeNode.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.coding.basic; | ||
public class BinaryTreeNode { | ||
|
||
private Object data; | ||
//父节点 | ||
private BinaryTreeNode parent; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public Object getData() { | ||
return data; | ||
} | ||
public void setData(Object data) { | ||
this.data = data; | ||
} | ||
|
||
public BinaryTreeNode getLeft() { | ||
return left; | ||
} | ||
public void setLeft(BinaryTreeNode left) { | ||
this.left = left; | ||
} | ||
|
||
public BinaryTreeNode getRight() { | ||
return right; | ||
} | ||
public void setRight(BinaryTreeNode right) { | ||
this.right = right; | ||
} | ||
|
||
public BinaryTreeNode getParent() { | ||
return parent; | ||
} | ||
public void setParent(BinaryTreeNode parent) { | ||
this.parent = parent; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
group12/2258659044/zj-2017/src/com/coding/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,8 @@ | ||
package com.coding.basic; | ||
|
||
public interface Iterator { | ||
|
||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
137 changes: 137 additions & 0 deletions
137
group12/2258659044/zj-2017/src/com/coding/basic/LinkedList.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,137 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
public class LinkedList implements List { | ||
|
||
private Node head; | ||
|
||
private int size = 0; | ||
|
||
public void add(Object o){ | ||
|
||
Node addNode = new Node(); | ||
addNode.data = o; | ||
if(size==0){ | ||
head = addNode; | ||
}else{ | ||
//获取最后一个节点 | ||
Node lastNode = getPointNode(size-1); | ||
lastNode.next = addNode; | ||
} | ||
size++; | ||
} | ||
public void add(int index , Object o){ | ||
|
||
Node addNode = new Node(); | ||
addNode.data = o; | ||
if(index == 0){ | ||
addFirst(o); | ||
return; | ||
} | ||
if(index == size){ | ||
Node lastNode = getPointNode(size-1); | ||
lastNode.next = addNode; | ||
}else{ | ||
Node pointNode = getPointNode(index); | ||
Node prePointNode = getPointNode(index-1); | ||
prePointNode.next = addNode; | ||
addNode.next = pointNode; | ||
} | ||
size ++; | ||
} | ||
public Object get(int index){ | ||
|
||
Node node = getPointNode(index); | ||
return node.data; | ||
} | ||
|
||
public Object remove(int index){ | ||
|
||
Node pointNode = getPointNode(index); | ||
Node nextPointNode = getPointNode(index+1); | ||
if(index ==0){ | ||
head = nextPointNode; | ||
}else{ | ||
Node prePointNode = getPointNode(index-1); | ||
prePointNode.next = nextPointNode; | ||
} | ||
size --; | ||
return pointNode.data; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public void addFirst(Object o){ | ||
|
||
Node secondNode = head; | ||
head = new Node(); | ||
head.data = o; | ||
if(size>0){ | ||
head.next = secondNode; | ||
} | ||
size ++; | ||
} | ||
|
||
public void addLast(Object o){ | ||
add(o); | ||
} | ||
|
||
public Object removeFirst(){ | ||
|
||
return remove(0); | ||
} | ||
|
||
public Object removeLast(){ | ||
|
||
return remove(size-1); | ||
} | ||
public Iterator iterator(){ | ||
return new Itr(); | ||
} | ||
|
||
private class Itr implements Iterator{ | ||
|
||
int cursor; | ||
@Override | ||
public boolean hasNext() { | ||
return cursor != LinkedList.this.size; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
|
||
int i = this.cursor; | ||
if (i >= LinkedList.this.size){ | ||
throw new NoSuchElementException(); | ||
} | ||
this.cursor = (i + 1); | ||
return LinkedList.this.get(i); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* 获取指定的节点 | ||
* @return | ||
*/ | ||
private Node getPointNode(int index){ | ||
|
||
if(index>size){ | ||
throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size+""); | ||
} | ||
Node node = head; | ||
for (int i = 0; i < index; i++) { | ||
node = node.next; | ||
} | ||
return node; | ||
} | ||
|
||
private static class Node{ | ||
Object data; | ||
Node next; | ||
|
||
} | ||
} |
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,10 @@ | ||
package com.coding.basic; | ||
|
||
public interface List { | ||
|
||
public void add(Object o); | ||
public void add(int index, Object o); | ||
public Object get(int index); | ||
public Object remove(int index); | ||
public int size(); | ||
} |
Oops, something went wrong.