forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 20
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 #5 from Ren650119726/master
数据结构作业
- Loading branch information
Showing
9 changed files
with
487 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,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>102228177</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,148 @@ | ||
package data2_19; | ||
|
||
import java.util.ConcurrentModificationException; | ||
import java.util.NoSuchElementException; | ||
|
||
|
||
public class ArrayList implements List{ | ||
public static final int defLen = 10; | ||
private Object[] elements; | ||
private int size; | ||
private int maxLen; | ||
|
||
public ArrayList(){ | ||
size = 0; | ||
maxLen = defLen; | ||
elements = new Object[defLen]; | ||
} | ||
|
||
/** | ||
* 在ArrayList末尾处追加元素 | ||
* @param o 添加的元素 | ||
*/ | ||
public void add(Object o){ | ||
if(size >= maxLen){ | ||
grow(); | ||
} | ||
elements[size] = o; | ||
size++; | ||
} | ||
|
||
/** | ||
* 数组扩容 | ||
*/ | ||
private void grow(){ | ||
maxLen = maxLen + (maxLen >> 1); | ||
Object[] newArr = new Object[maxLen]; | ||
System.arraycopy(elements, 0, newArr, 0, size); | ||
elements = newArr; | ||
} | ||
|
||
/** | ||
* 在指定索引处添加元素 | ||
* @param i 指定索引 | ||
* @param o 添加元素 | ||
*/ | ||
public void add(int i,Object o){ | ||
//判断插入位置大于数组实际长度 | ||
if(i > size){ | ||
size = i; | ||
if(size >= maxLen){//数组大小大于数组最大容量则需要扩容 | ||
grow(); | ||
} | ||
} | ||
//插入位置不大于数组实际长度时,将插入位置的元素向后移。 | ||
for (int j = size; j > i ; j++) { | ||
elements[j] = elements[j-1]; | ||
} | ||
elements[i] = o; | ||
size++; | ||
} | ||
|
||
/** | ||
* 获取传入索引的元素 | ||
* @param index 索引 | ||
* @return 返回传入索引的元素 | ||
*/ | ||
public Object get(int index){ | ||
//索引不在实际范围内 | ||
if(index < 0||index >= size){ | ||
throw new ArrayIndexOutOfBoundsException(); | ||
} | ||
for (int i = 0; i < size; i++) { | ||
return elements[index]; | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* 删除指定索引元素并返回 | ||
* @param index | ||
* @return 该索引处元素 | ||
*/ | ||
public Object remove(int index){ | ||
//索引不在实际范围内 | ||
if(index < 0||index >= size){ | ||
throw new ArrayIndexOutOfBoundsException(); | ||
}else{ | ||
for (int j = index; j < size-1; j++) { | ||
elements[j]=elements[j+1]; | ||
} | ||
size--; | ||
return elements[index]; | ||
} | ||
} | ||
|
||
/** | ||
* 获取大小 | ||
* @return | ||
*/ | ||
public int size(){ | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return new ArrayListIterator(); | ||
} | ||
|
||
private class ArrayListIterator implements Iterator{ | ||
int cursor; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return cursor != size; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
int i = cursor; | ||
if(i >= size){ | ||
throw new NoSuchElementException(); | ||
} | ||
if (i >= elements.length){ | ||
throw new ConcurrentModificationException(); | ||
} | ||
cursor = i+1; | ||
return elements[i]; | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
ArrayList list = new ArrayList(); | ||
list.add(0); | ||
list.add(1); | ||
list.add(2); | ||
list.add(3); | ||
list.add(4); | ||
list.add(6, 6); | ||
list.remove(3); | ||
for (int i = 0; i < list.size(); i++) { | ||
System.out.println(i+":"+list.get(i)); | ||
} | ||
|
||
Iterator it = list.iterator(); | ||
while (it.hasNext()) { | ||
System.out.println(it.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,74 @@ | ||
package data2_19; | ||
|
||
public class BinaryTreeNode implements Comparable<BinaryTreeNode>{ | ||
|
||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public BinaryTreeNode(Object o) { | ||
this.data = o; | ||
} | ||
|
||
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; | ||
} | ||
|
||
@Override | ||
public int compareTo(BinaryTreeNode o) { | ||
return (this.data.hashCode() < o.data.hashCode()) ? -1 : | ||
((this.data.hashCode() == o.data.hashCode()) ? 0 : 1); | ||
} | ||
|
||
public BinaryTreeNode insert(Object o){ | ||
BinaryTreeNode node = new BinaryTreeNode(o); | ||
insertNode(this,node); | ||
return node; | ||
} | ||
|
||
private void insertNode(BinaryTreeNode parentNode, BinaryTreeNode node) { | ||
//父节点大于添加元素 | ||
if(parentNode.compareTo(node) == 1){ | ||
if(parentNode.left == null){ | ||
parentNode.left = node; | ||
return; | ||
} | ||
insertNode(parentNode.left, node); | ||
} | ||
//父节点小于添加元素 | ||
else | ||
if(parentNode.compareTo(node) == -1){ | ||
if(parentNode.right == null){ | ||
parentNode.right = node; | ||
return; | ||
} | ||
insertNode(parentNode.right, node); | ||
}else{ | ||
throw new RuntimeException("No duplicate vertex allowed!"); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
BinaryTreeNode tree = new BinaryTreeNode(5); | ||
tree.insert(2); | ||
tree.insert(23); | ||
tree.insert(7); | ||
tree.insert(1); | ||
} | ||
|
||
} |
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 data2_19; | ||
|
||
public interface Iterator { | ||
|
||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
Oops, something went wrong.