-
Notifications
You must be signed in to change notification settings - Fork 641
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 #10 from chaoszcy/master
homework
- Loading branch information
Showing
7 changed files
with
503 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,123 @@ | ||
import java.util.Arrays; | ||
//import java.util.Objects; | ||
|
||
public class ArrayList implements List { | ||
|
||
//private int size = 0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
public void add(Object o) { | ||
if (o == null) { | ||
System.out.println("传入对象不能为空!"); | ||
return; | ||
} else { | ||
if (size() == elementData.length) { | ||
Object[] elementData2 = Arrays.copyOf(elementData, ((int) (elementData.length * 1.2))); | ||
elementData2[size()] = o; | ||
elementData = elementData2; | ||
} else { | ||
elementData[size()] = o; | ||
} | ||
} | ||
} | ||
|
||
public void add(int index, Object o) { | ||
if (o == null) { | ||
System.out.println("传入对象不能为空!"); | ||
return; | ||
} | ||
if ((index > size())) { | ||
System.out.println("超出数组长度!"); | ||
return; | ||
} else { | ||
if (size() == elementData.length) { | ||
Object[] elementData2 = Arrays.copyOf(elementData, ((int) (elementData.length * 1.2))); | ||
rightShift(elementData2, index); | ||
elementData2[index] = o; | ||
elementData = elementData2; | ||
return; | ||
} else { | ||
rightShift(elementData, index); | ||
elementData[size()] = o; | ||
return; | ||
} | ||
} | ||
} | ||
|
||
public Object get(int index) { | ||
if ((index > size() - 1)) { | ||
System.out.println("超出数组长度!"); | ||
return null; | ||
} else { | ||
return elementData[index]; | ||
} | ||
} | ||
|
||
public Object remove(int index) { | ||
if (index > size() - 1) { | ||
System.out.println("超出数组长度!"); | ||
return null; | ||
}else{ | ||
Object o = elementData[index]; | ||
elementData[index]=null; | ||
leftShift(elementData,index); | ||
return o; | ||
} | ||
} | ||
|
||
public int size() { | ||
int n; | ||
for (n = 0; n < elementData.length; n++) { | ||
if (elementData[n] == null) { | ||
break; | ||
} | ||
} | ||
return n; | ||
} | ||
|
||
|
||
public Iterator iterator() { | ||
return new ArrayListIterator(this); | ||
} | ||
|
||
public class ArrayListIterator implements Iterator{ | ||
ArrayList list = new ArrayList(); | ||
int pos = 0; | ||
ArrayListIterator(ArrayList list){ | ||
this.list = list; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if (pos<list.size()){ | ||
return true; | ||
}else{ | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
pos++; | ||
return list.get(pos); | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
public void rightShift(Object[] o, int index) { | ||
for (int k = size(); k > index; k--) { | ||
o[k] = o[k - 1]; | ||
} | ||
} | ||
|
||
public void leftShift(Object[] o, int index) { | ||
for (int k = index; k < size(); k++) { | ||
o[k] = o[k + 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,59 @@ | ||
public class BinaryTreeNode { | ||
|
||
private Integer data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public Integer getData() { | ||
return data; | ||
} | ||
public void setData(Integer 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 insert(Integer o){ | ||
if(o>getData()){ | ||
if(this.right==null){ | ||
BinaryTreeNode node = new BinaryTreeNode(); | ||
node.data = o; | ||
node.left = null; | ||
node.right = null; | ||
this.right = node; | ||
return node; | ||
}else{ | ||
getRight(); | ||
insert(o); | ||
return null; | ||
} | ||
}else if(o<getData()){ | ||
if (this.left == null){ | ||
BinaryTreeNode node = new BinaryTreeNode(); | ||
node.data = o; | ||
node.left = null; | ||
node.right = null; | ||
this.left = node; | ||
return node; | ||
}else{ | ||
getLeft(); | ||
insert(o); | ||
return null; | ||
} | ||
}else{ | ||
System.out.println("出现重复元素,请检查输入!"); | ||
return null; | ||
} | ||
} | ||
|
||
} |
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 @@ | ||
package com.coding.basic; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
Oops, something went wrong.