-
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 #9 from foreverkai/master
homework:first week
- Loading branch information
Showing
11 changed files
with
436 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>1378560653Learning</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,95 @@ | ||
package com.coding.basic; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
public void add(Object o){ | ||
if( size <= elementData.length){ | ||
elementData[size + 1] = o; | ||
size++; | ||
}else{ | ||
elementData = grow(elementData, 1); | ||
elementData[size+1] = o; | ||
size++; | ||
} | ||
} | ||
public void add(int index, Object o){ | ||
Object[] temp = new Object[elementData.length]; | ||
for(int i = 0; i<elementData.length; i++){ | ||
temp[i] = elementData[i]; | ||
} | ||
if(index <= size){ | ||
elementData = grow(elementData, 1); | ||
elementData[index] = o; | ||
System.arraycopy(temp, index, elementData, index+1, temp.length - index); | ||
size++; | ||
} | ||
} | ||
|
||
public Object get(int index){ | ||
if(index < elementData.length){ | ||
return elementData[index]; | ||
}else{ | ||
return null; | ||
} | ||
} | ||
|
||
public Object remove(int index){ | ||
Object[] temp = new Object[elementData.length]; | ||
for(int i = 0; i<elementData.length; i++){ | ||
temp[i] = elementData[i]; | ||
} | ||
if(index < elementData.length){ | ||
System.arraycopy(temp, index+1, elementData, index, temp.length - index - 1); | ||
size--; | ||
return elementData; | ||
}else{ | ||
return null; | ||
} | ||
} | ||
|
||
public int size(){ | ||
for(int i = 0; i < elementData.length;i++){ | ||
if(elementData[i] != null){ | ||
size++; | ||
} | ||
} | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return new ArrayListIterator(this); | ||
} | ||
|
||
private class ArrayListIterator implements Iterator { | ||
ArrayList arraylist = null; | ||
int pos = 0; | ||
|
||
private ArrayListIterator(ArrayList arraylist) { | ||
this.arraylist = arraylist; | ||
} | ||
@Override | ||
public boolean hasNext() { | ||
pos ++; | ||
if(pos >= size){ | ||
return false; | ||
}else{ | ||
return true; | ||
} | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
return elementData[pos]; | ||
} | ||
|
||
} | ||
public static Object[] grow(Object[]src, int size){ | ||
Object[] target = new Object[src.length + size]; | ||
System.arraycopy(src, 0, target, 0, src.length); | ||
return target; | ||
} | ||
} |
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 com.coding.basic; | ||
|
||
public class BinaryTree { | ||
private BinaryTreeNode root; | ||
|
||
public BinaryTreeNode getRoot(){ | ||
return root; | ||
} | ||
|
||
public BinaryTreeNode insert(Object o){ | ||
BinaryTreeNode node = new BinaryTreeNode(o); | ||
if(root == null){ | ||
root = node; | ||
root.setLeft(null); | ||
root.setRight(null); | ||
return root; | ||
}else{ | ||
BinaryTreeNode currentNode = root; | ||
BinaryTreeNode parentNode; | ||
while(true){ | ||
parentNode = currentNode; | ||
|
||
if(((Integer)node.getData()) > ((Integer)currentNode.getData())){ | ||
currentNode = currentNode.getRight(); | ||
if(currentNode == null){ | ||
parentNode.setRight(node); | ||
return node; | ||
} | ||
}else{ | ||
currentNode = currentNode.getLeft(); | ||
if(currentNode == null){ | ||
parentNode.setLeft(node); | ||
return node; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
group06/1378560653/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,34 @@ | ||
package com.coding.basic; | ||
|
||
public class BinaryTreeNode { | ||
|
||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public BinaryTreeNode(Object data){ | ||
this.left = null; | ||
this.right = null; | ||
this.data = data; | ||
} | ||
|
||
public Object getData() { | ||
return data; | ||
} | ||
public void setData(int 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; | ||
} | ||
} | ||
|
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 @@ | ||
package com.coding.basic; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
} |
164 changes: 164 additions & 0 deletions
164
group06/1378560653/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,164 @@ | ||
package com.coding.basic; | ||
|
||
public class LinkedList implements List { | ||
|
||
private Node head; | ||
private int size = 0; | ||
|
||
public void add(Object o){ | ||
if(null == head){ | ||
head = new Node(o); | ||
head.next = null; | ||
}else{ | ||
Node pt = head; | ||
while(head.next != null){ | ||
pt = pt.next; | ||
} | ||
pt.next = new Node(o); | ||
pt.next.next =null; | ||
} | ||
size++; | ||
} | ||
|
||
public void add(int index , Object o){ | ||
if(index < size){ | ||
Node pt = head; | ||
for(int i = 0; i < index-1; i++){ | ||
pt = pt.next; | ||
} | ||
Node pt1 = pt.next.next; | ||
pt.next = new Node(o); | ||
pt.next.next = pt1; | ||
size ++; | ||
} | ||
} | ||
public Object get(int index){ | ||
if(index < size){ | ||
Node pt = head; | ||
for(int i = 0; i < index; i++){ | ||
pt = pt.next; | ||
} | ||
return pt.data; | ||
}else{ | ||
return null; | ||
} | ||
} | ||
public Object remove(int index){ | ||
if(index < size){ | ||
Node pt = head; | ||
for(int i = 0; i< index -1;i++){ | ||
pt = pt.next; | ||
} | ||
Node pt1 = pt.next; | ||
pt.next = pt1.next; | ||
return pt1.data; | ||
}else{ | ||
return null; | ||
} | ||
} | ||
|
||
public int size(){ | ||
if(null == head){ | ||
size = 0; | ||
}else{ | ||
Node pt = head; | ||
while(pt.next != null){ | ||
size++; | ||
} | ||
} | ||
return size; | ||
} | ||
|
||
public void addFirst(Object o){ | ||
if(null == head){ | ||
head = new Node(o); | ||
head.next = null; | ||
}else{ | ||
Node pt = new Node(o); | ||
pt.next = head; | ||
head = pt; | ||
} | ||
size++; | ||
} | ||
public void addLast(Object o){ | ||
if(null == head){ | ||
head = new Node(o); | ||
head.next = null; | ||
}else{ | ||
Node pt = head; | ||
while(pt.next != null){ | ||
pt = pt.next; | ||
} | ||
pt.next = new Node(o); | ||
Node pt1 = pt.next; | ||
pt1.next = null; | ||
} | ||
size++; | ||
} | ||
public Object removeFirst(){ | ||
if(null != head){ | ||
Node pt = head; | ||
head = pt.next; | ||
size--; | ||
return head.data; | ||
}else{ | ||
return null; | ||
} | ||
} | ||
public Object removeLast(){ | ||
if(null != head){ | ||
Node pt = head; | ||
while(pt.next.next != null){ | ||
pt = pt.next; | ||
} | ||
Node pt1 = pt.next; | ||
pt.next = null; | ||
size--; | ||
return pt1.data; | ||
}else{ | ||
return null; | ||
} | ||
} | ||
|
||
public Iterator iterator(){ | ||
return new LinkedListIterator(this); | ||
} | ||
|
||
private class LinkedListIterator implements Iterator { | ||
LinkedList linkedlist = null; | ||
int pos = 0; | ||
|
||
private LinkedListIterator(LinkedList linkedlist) { | ||
this.linkedlist = linkedlist; | ||
} | ||
@Override | ||
public boolean hasNext() { | ||
pos++; | ||
if(pos >= size){ | ||
return false; | ||
}else{ | ||
return true; | ||
} | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
Node pt = head; | ||
for(int i = 0; i < pos; i++ ){ | ||
while(pt.next != null){ | ||
pt = pt.next; | ||
} | ||
} | ||
return pt.data; | ||
} | ||
|
||
} | ||
|
||
private static class Node{ | ||
public Node(Object o) { | ||
this.data = o; | ||
} | ||
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,9 @@ | ||
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.