forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 15
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 #12 from hiei17/master
....
- Loading branch information
Showing
8 changed files
with
481 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,99 @@ | ||
import java.util.Arrays; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
private int length=3; | ||
private Object[] elementData = new Object[length]; | ||
|
||
public void add(Object o){ | ||
if(size>=length){ | ||
grow(100); | ||
} | ||
elementData[size]=o; | ||
size++; | ||
} | ||
public void add(int index, Object o){ | ||
size++; | ||
if(size>=length){ | ||
grow(100); | ||
} | ||
System.arraycopy(elementData,index,elementData,index+1,size-index-1); | ||
elementData[index]=o; | ||
} | ||
|
||
public Object get(int index){ | ||
if(index<size) | ||
return elementData[index]; | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
|
||
public Object remove(int index){ | ||
//越界 | ||
if(index>=size) | ||
throw new IndexOutOfBoundsException(); | ||
size--; | ||
Object a=elementData[index]; | ||
//刚好最后一个 | ||
if (index+1==size){ | ||
return a; | ||
} | ||
System.arraycopy(elementData,index+1,elementData,index,size); | ||
return a; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return new ArrayListIterator(); | ||
} | ||
private class ArrayListIterator implements Iterator{ | ||
private int index=0; | ||
@Override | ||
public boolean hasNext() { | ||
if(index+1<size){ | ||
index++; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
return elementData[index]; | ||
} | ||
} | ||
private void grow(int increase){ | ||
//return Arrays.copyOf(src,src.length+size); | ||
Object[] target=new Object[length+increase]; | ||
System.arraycopy(elementData,0,target,0,length); | ||
elementData= target; | ||
length=length+increase; | ||
} | ||
public String toString(){ | ||
return Arrays.toString(Arrays.copyOf(elementData,size)); | ||
} | ||
public static void main(String[] arg){ | ||
ArrayList a=new ArrayList(); | ||
a.add(0); | ||
a.add(1); | ||
a.add("2"); | ||
a.add("3"); | ||
a.add("4"); | ||
a.add("5"); | ||
a.add("six"); | ||
a.add("七"); | ||
a.add(8); | ||
Iterator iterator=a.iterator(); | ||
while (iterator.hasNext()) | ||
System.out.println(iterator.next()); | ||
/*System.out.println(a.remove(3)); | ||
System.out.println(a.size()); | ||
System.out.println(a);*/ | ||
|
||
//System.out.println(a.get(3)); | ||
//System.out.println(a.get(99)); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
group15/1503_1311822904/myCollection/src/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,70 @@ | ||
import java.util.Objects; | ||
|
||
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; | ||
} | ||
//TODO | ||
public BinaryTreeNode insert(Integer o){ | ||
if(data==null){ | ||
data=o; | ||
return this; | ||
} | ||
|
||
BinaryTreeNode b=new BinaryTreeNode(); | ||
b.setData(o); | ||
|
||
if(Objects.equals(data, o)){ | ||
return this; | ||
} | ||
if(data<o){ | ||
if(left==null){ | ||
left=b; | ||
return b; | ||
} | ||
left.insert(o); | ||
} | ||
if(data>o){ | ||
if(right==null){ | ||
right=b; | ||
return b; | ||
} | ||
right.insert(o); | ||
} | ||
return b; | ||
} | ||
|
||
public void showAll(){ | ||
if(right!=null){ | ||
right.showAll(); | ||
} | ||
System.out.print(data+" "); | ||
if(left!=null){ | ||
left.showAll(); | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
} |
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,5 @@ | ||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
182 changes: 182 additions & 0 deletions
182
group15/1503_1311822904/myCollection/src/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,182 @@ | ||
public class LinkedList implements List { | ||
private int size = 0; | ||
private Node head=new Node(null); | ||
|
||
public void add(Object o){ | ||
if(head.data==null){ | ||
head.data=o; | ||
return; | ||
} | ||
Node n=head; | ||
while (n.next!=null){ | ||
n=n.next; | ||
} | ||
n.next=new Node(o); | ||
size++; | ||
} | ||
public void add(int index , Object o){ | ||
Node n=getNode(index); | ||
Node newN=new Node(o); | ||
newN.next=n.next; | ||
n.next=newN; | ||
size++; | ||
} | ||
public Object get(int index){ | ||
return getNode(index).data; | ||
} | ||
private Node getNode(int index){ | ||
Node n=head; | ||
for (int i=0;i<index;i++){ | ||
if(n==null) | ||
throw new IndexOutOfBoundsException(); | ||
n= n.next; | ||
} | ||
return n; | ||
} | ||
public Object remove(int index){ | ||
Object o; | ||
if (index==0){ | ||
o=head.data; | ||
head.data=null; | ||
size--; | ||
return o; | ||
} | ||
Node n=getNode(index-1); | ||
o=n.next.data; | ||
n.next=n.next.next; | ||
size--; | ||
return o; | ||
} | ||
|
||
public int size(){ | ||
|
||
return size; | ||
} | ||
|
||
public void addFirst(Object o){ | ||
size++; | ||
if(head.data==null){ | ||
head.data=o; | ||
return; | ||
} | ||
Node n=new Node(o); | ||
n.next=head; | ||
head=n; | ||
|
||
} | ||
public void addLast(Object o){ | ||
size++; | ||
//要是还没有值 就是头 | ||
if (head.data==null){ | ||
head.data=o; | ||
} | ||
Node n=head; | ||
//找到最后一个 | ||
while (n.next!=null){ | ||
n=n.next; | ||
} | ||
//接上去 | ||
n.next=new Node(o); | ||
|
||
} | ||
public Object removeFirst(){ | ||
//没有头 | ||
if(head.data==null) | ||
throw new IndexOutOfBoundsException(); | ||
Object o=head.data; | ||
head=head.next; | ||
size--; | ||
return o; | ||
} | ||
public Object removeLast(){ | ||
//还没有值 异常 | ||
if (head.data==null){ | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
size--; | ||
Object o; | ||
//只有头有值 | ||
if(head.next==null){ | ||
o=head.data; | ||
head.data=o; | ||
return o; | ||
} | ||
Node n=head; | ||
//找到最后第二个 | ||
while (n.next.next!=null) | ||
n=n.next; | ||
//拿到最后一个值 | ||
o=n.next.data; | ||
//去掉最后一个节点 | ||
n.next=null; | ||
return o; | ||
} | ||
public Iterator iterator(){ | ||
return new LinkedIterator(); | ||
} | ||
private class LinkedIterator implements Iterator{ | ||
private Node n=head; | ||
@Override | ||
public boolean hasNext() { | ||
if(n!=null&&n.next!=null){ | ||
n=n.next; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
return n.data; | ||
} | ||
} | ||
|
||
private static class Node{ | ||
Object data; | ||
Node next; | ||
Node(Object o){ | ||
data=o; | ||
} | ||
|
||
|
||
} | ||
|
||
public String toString(){ | ||
String s="["; | ||
for (int i=0;i<size;i++) | ||
s+=get(i)+", "; | ||
s+="]"; | ||
return s; | ||
} | ||
|
||
public static void main(String[] arg){ | ||
LinkedList a=new LinkedList(); | ||
// a.removeFirst(); | ||
a.addFirst("first"); | ||
a.addLast("ll"); | ||
a.add(0); | ||
a.add(1); | ||
a.add("2"); | ||
a.add("3"); | ||
a.add("4"); | ||
a.add("5"); | ||
a.add("six"); | ||
a.add("七"); | ||
a.add(8); | ||
System.out.println(a); | ||
Iterator iterator=a.iterator(); | ||
while (iterator.hasNext()) | ||
System.out.println(iterator.next()); | ||
/*System.out.println(a.size); | ||
System.out.println(a); | ||
System.out.println(a.remove(3)); | ||
System.out.println(a.remove(3)); | ||
a.removeFirst(); | ||
a.removeLast(); | ||
System.out.println(a);*/ | ||
|
||
|
||
//System.out.println(a.get(3)); | ||
//System.out.println(a.get(99)); | ||
} | ||
} |
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 @@ | ||
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.