forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 8
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 #7 from GordenChow/master
提交第一周作业 周国栋
- Loading branch information
Showing
11 changed files
with
645 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,7 @@ | ||
<?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="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> | ||
<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>coding2017</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> |
120 changes: 120 additions & 0 deletions
120
group27/513274874/homework/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,120 @@ | ||
|
||
package com.coding.basic; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* @autor zhougd 20170306 | ||
* 数组实现ArrayList | ||
*/ | ||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData; | ||
|
||
//扩容默认值 | ||
private static final int INCREAMENT_CAP = 10; | ||
|
||
//含参数的构造函数 | ||
public ArrayList(int size, Object[] elementData) { | ||
this.size = size; | ||
this.elementData = elementData; | ||
} | ||
|
||
//默认100容量的构造函数 | ||
public ArrayList() { | ||
this.size = 0; | ||
this.elementData = new Object[100]; | ||
} | ||
|
||
@Override | ||
public void add(Object o) { | ||
//判断超过容量自动扩容 | ||
if (this.size + 1 > this.elementData.length) { | ||
increase(); | ||
} | ||
this.elementData[size++] = o; | ||
} | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
if (index < 0 || index > this.size) { | ||
throw new IndexOutOfBoundsException("Index out of bound!"); | ||
} | ||
//判断超过容量自动扩容 | ||
if (this.size + 1 > this.elementData.length) { | ||
increase(); | ||
} | ||
this.size++; | ||
//index后面数组后移一位 | ||
for (int cur = this.size; cur > index; cur--) { | ||
this.elementData[cur] = this.elementData[cur - 1]; | ||
} | ||
|
||
this.elementData[index] = o; | ||
|
||
} | ||
|
||
public Object get(int index) { | ||
if (index < 0 || index > this.size) { | ||
throw new IndexOutOfBoundsException("Index out of bound!"); | ||
} | ||
return this.elementData[index]; | ||
} | ||
|
||
public Object remove(int index) { | ||
Object o = this.get(index); | ||
this.size--; | ||
//index后面的数向前移动一位 | ||
for (int cur = index + 1; cur < this.size; cur++) { | ||
this.elementData[cur] = this.elementData[cur + 1]; | ||
} | ||
return o; | ||
} | ||
|
||
public int size() { | ||
return this.size; | ||
} | ||
|
||
public Iterator iterator() { | ||
return new ArrayListIterator(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
String arrayStr = "ArrayList{ size = " + this.size() + " , "; | ||
|
||
arrayStr += "elementData=["; | ||
for(int i = 0 ;i<this.size();i++){ | ||
arrayStr += | ||
i == this.size()-1 ? | ||
elementData[i]+"]":elementData[i]+"," ; | ||
} | ||
arrayStr+= " }"; | ||
return arrayStr; | ||
|
||
} | ||
|
||
private void increase() { | ||
this.elementData = Arrays.copyOf(this.elementData, this.elementData.length + INCREAMENT_CAP); | ||
} | ||
|
||
private class ArrayListIterator implements Iterator { | ||
|
||
private int currentIndex = 0; | ||
private int count = size(); | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return currentIndex < count-1; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
currentIndex++; | ||
return get(currentIndex); | ||
} | ||
} | ||
} | ||
|
32 changes: 32 additions & 0 deletions
32
group27/513274874/homework/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,32 @@ | ||
package com.coding.basic; | ||
|
||
public class BinaryTreeNode { | ||
|
||
private Object data; | ||
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 insert(Object o){ | ||
return null; | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
group27/513274874/homework/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,7 @@ | ||
package com.coding.basic; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
188 changes: 188 additions & 0 deletions
188
group27/513274874/homework/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,188 @@ | ||
|
||
package com.coding.basic; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
/** | ||
* @author zhougd 20170306 | ||
* 单向链表实现LinkedList | ||
*/ | ||
|
||
public class LinkedList implements List { | ||
java.util.LinkedList a; | ||
/** | ||
* 第一个元素 | ||
*/ | ||
private Node head; | ||
|
||
/** | ||
* 最后一个元素 | ||
*/ | ||
private Node tail; | ||
|
||
/** | ||
* 元素容量 | ||
*/ | ||
private int size = 0; | ||
|
||
public void add(Object o){ | ||
Node node = new Node(o); | ||
//判断是否链表为空 | ||
if(this.size() == 0){ | ||
this.addFirst(node); | ||
}else{ | ||
this.addLast(node); | ||
} | ||
|
||
} | ||
public void add(int index , Object o){ | ||
checkIndex(index); | ||
|
||
Node oldNode= this.getNode(index); | ||
Object oldObject = oldNode.getData(); | ||
Node next = oldNode.getNext(); | ||
|
||
//将原位置修改为新元素 | ||
oldNode.setData(o); | ||
//设置下一个元素 | ||
oldNode.setNext(new Node(oldObject)); | ||
//设置下一个元素的下一个元素 | ||
oldNode.getNext().setNext(next); | ||
|
||
size ++; | ||
} | ||
|
||
public Object get(int index){ | ||
checkIndex(index); | ||
return this.getNode(index).getData(); | ||
} | ||
|
||
public Object remove(int index){ | ||
checkIndex(index); | ||
//获取到当前元素和下一个元素 | ||
//把当前元素的值设置成下一个元素的值,删除掉下一个元素,这样的话,不必管上一个元素是什么,是不是第一个元素 | ||
Node node = this.getNode(index); | ||
Object data = node.getData(); | ||
Node nextNode = this.getNode(index + 1); | ||
node.setData(nextNode.getData()); | ||
node.setNext(nextNode.getNext()); | ||
|
||
return data; | ||
} | ||
|
||
public int size(){ | ||
return this.size(); | ||
} | ||
|
||
public void addFirst(Object o){ | ||
Node node = new Node(o); | ||
//原头变为第二 | ||
Node temp = this.head; | ||
this.head = node; | ||
node.next = temp; | ||
size++; | ||
} | ||
public void addLast(Object o){ | ||
Node node = new Node(o); | ||
Node t = this.tail; | ||
if(t == null){ | ||
this.head = node; | ||
}else{ | ||
this.tail.next = node; | ||
this.tail = node; | ||
} | ||
size++; | ||
} | ||
public Object removeFirst(){ | ||
Node head = this.head; | ||
if(head == null){ | ||
throw new NoSuchElementException("No such element !"); | ||
} | ||
this.head = this.head.getNext(); | ||
size--; | ||
return head ; | ||
} | ||
|
||
public Object removeLast(){ | ||
Node node ; | ||
if(this.tail == null){ | ||
throw new NoSuchElementException("No such element !"); | ||
} | ||
node = this.tail; | ||
if(this.head ==this.tail){ | ||
node = this.head; | ||
this.head = null; | ||
this.size = 0; | ||
}else{ | ||
//获取尾元素的上一个元素 | ||
this.tail = this.getNode(this.size-2); | ||
this.tail.setNext(null); | ||
this.size--; | ||
} | ||
|
||
return node; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return new LinkedListIterator(); | ||
} | ||
|
||
private void checkIndex(int index){ | ||
if(index < 0 || index >size()){ | ||
throw new IndexOutOfBoundsException("Index out of bound !"); | ||
} | ||
} | ||
|
||
private Node getNode(int index ){ | ||
|
||
Node node = this.head; | ||
for(int i = 0 ;i<size();i++){ | ||
node = node.next; | ||
} | ||
return node; | ||
} | ||
|
||
private class LinkedListIterator implements Iterator{ | ||
|
||
private int currentIndex = 0; | ||
private int count = size(); | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return currentIndex < count-1; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
currentIndex++; | ||
return get(currentIndex); | ||
} | ||
} | ||
|
||
private static class Node{ | ||
Object data; | ||
Node next; | ||
|
||
public Node(Object data) { | ||
this.data = data; | ||
} | ||
|
||
public Object getData() { | ||
return data; | ||
} | ||
|
||
public void setData(Object data) { | ||
this.data = data; | ||
} | ||
|
||
public Node getNext() { | ||
return next; | ||
} | ||
|
||
public void setNext(Node next) { | ||
this.next = 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.