forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 13
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 #3 from FelixCJF/master
第一周作业提交
- Loading branch information
Showing
12 changed files
with
770 additions
and
0 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
group02/1554421063/src/com/github/FelixCJF/coding2017/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,105 @@ | ||
package com.github.FelixCJF.coding2017.basic; | ||
|
||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
public void add(Object o){ | ||
//容量增加 | ||
ensureCapacity(size + 1); | ||
//添加 | ||
elementData[size ++] = o; | ||
} | ||
|
||
public void add(int index, Object o){ | ||
|
||
//检查是否越界 | ||
rangeCheck(index); | ||
// 进行扩容检查 | ||
ensureCapacity(size + 1); | ||
// 对数组进行复制处理,目的就是空出index的位置插入element,并将index后的元素位移一个位置 | ||
System. arraycopy(elementData, index, elementData, index + 1, | ||
size - index); | ||
// 将指定的index位置赋值为Object o | ||
elementData[index] = o; | ||
// 自增一位长度 | ||
size++; | ||
} | ||
|
||
public Object get(int index){ | ||
rangeCheck(index); | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index){ | ||
// 数组越界检查 | ||
rangeCheck(index); | ||
// 取出要删除位置的元素,供返回使用 | ||
Object oldValue = elementData[index]; | ||
// 计算数组要复制的数量 | ||
int numMoved = size - index - 1; | ||
// 数组复制,就是将index之后的元素往前移动一个位置 | ||
if (numMoved > 0) | ||
System. arraycopy(elementData, index+1, elementData, index, | ||
numMoved); | ||
// 将数组最后一个元素置空(因为删除了一个元素,然后index后面的元素都向前移动了,所以最后一个就没用了),好让gc尽快回收 | ||
// 不要忘了size减一 | ||
elementData[--size] = null; | ||
return oldValue; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return new ArrayListIterator(); | ||
} | ||
|
||
//内部类,实现Iterator | ||
private class ArrayListIterator implements Iterator{ | ||
|
||
private int currentIndex = 0; //当前索引 | ||
|
||
public boolean hasNext() { | ||
if (currentIndex >= size) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public Object next() { | ||
Object object = elementData[currentIndex]; | ||
currentIndex ++ ; | ||
return object; | ||
} | ||
} | ||
//扩容 | ||
public void ensureCapacity( int minCapacity) { | ||
// 当前数组的长度 | ||
int oldCapacity = elementData .length; | ||
// 最小需要的容量大于当前数组的长度则进行扩容 | ||
if (minCapacity > oldCapacity) { | ||
// 扩容 | ||
int newCapacity = oldCapacity + (oldCapacity >> 1); | ||
// 如果新扩容的数组长度还是比最小需要的容量小,则以最小需要的容量为长度进行扩容 | ||
if (newCapacity < minCapacity) | ||
newCapacity = minCapacity; | ||
//数组复制 | ||
Object[] elementData2 = new Object[newCapacity]; | ||
for (int i = 0; i < oldCapacity; i++) { | ||
elementData2[i] = elementData[i]; | ||
} | ||
elementData = elementData2; | ||
} | ||
} | ||
//检查是否越界 | ||
private void rangeCheck(int index){ | ||
if (index < 0 || index >= this.size) { | ||
throw new IndexOutOfBoundsException("index :" + index + "size :" + size); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
group02/1554421063/src/com/github/FelixCJF/coding2017/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.github.FelixCJF.coding2017.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; | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
group02/1554421063/src/com/github/FelixCJF/coding2017/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,8 @@ | ||
package com.github.FelixCJF.coding2017.basic; | ||
|
||
public interface Iterator { | ||
|
||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
214 changes: 214 additions & 0 deletions
214
group02/1554421063/src/com/github/FelixCJF/coding2017/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,214 @@ | ||
package com.github.FelixCJF.coding2017.basic; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
public class LinkedList implements List { | ||
|
||
private Node head;//头指针 | ||
private Node last;//尾指针 | ||
private int size = 0; | ||
|
||
public void add(Object o){ | ||
addLast(o); | ||
} | ||
|
||
public void add(int index , Object o){ | ||
//检查是否越界 | ||
checkIndex(index); | ||
|
||
Node indexNode = node(index); | ||
|
||
if (index == size) { | ||
addLast(o); | ||
} else { | ||
final Node pred = indexNode.prv; | ||
|
||
final Node newNode = new Node(); | ||
newNode.data = o; | ||
newNode.next = indexNode; | ||
newNode.prv = pred; | ||
|
||
indexNode.prv = newNode; | ||
|
||
if (pred == null) { | ||
head = newNode; | ||
} else { | ||
pred.next = newNode; | ||
} | ||
} | ||
size ++; | ||
} | ||
public Object get(int index){ | ||
//检查是否越界 | ||
checkIndex(index); | ||
|
||
Node indexNode = node(index); | ||
|
||
return indexNode.data; | ||
} | ||
public Object remove(int index){ | ||
//检查是否越界 | ||
checkIndex(index); | ||
|
||
Node indexNode = node(index); | ||
Object element = indexNode.data; | ||
Node pre = indexNode.prv; | ||
Node next = indexNode.next; | ||
|
||
if (pre == null) { | ||
head = next; | ||
} else { | ||
pre.next = next; | ||
indexNode.prv = null; | ||
} | ||
|
||
if (next == null) { | ||
last = pre; | ||
} else { | ||
next.prv = pre; | ||
indexNode.next = null; | ||
} | ||
|
||
indexNode.data = null; | ||
|
||
size --; | ||
|
||
return element; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public void addFirst(Object o){ | ||
//节点变量存放原来的头指针 | ||
final Node oldHead = head; | ||
//创建新的节点对象 | ||
final Node newNode = new Node(); | ||
newNode.data = o; | ||
newNode.next = head; | ||
newNode.prv = null; | ||
//判断oldhead是否为null | ||
if (oldHead == null) { | ||
last = newNode; | ||
}else { | ||
//头指针指向新创建的节点对象 | ||
oldHead.prv = newNode; | ||
} | ||
//将newNode变为头指针 | ||
head = newNode; | ||
size ++; | ||
} | ||
public void addLast(Object o){ | ||
//节点新变量放原先的尾指针 | ||
final Node oldLast = last; | ||
//创建新节点,加入要添加的对象 | ||
final Node newNode = new Node(); | ||
newNode.data = o; | ||
newNode.next = null; | ||
newNode.prv = oldLast; | ||
if (oldLast == null) { | ||
head = newNode; | ||
} else { | ||
//尾指针指向新创建的节点 | ||
oldLast.next = newNode; | ||
} | ||
//newNode变为尾指针 | ||
last = newNode; | ||
size++; | ||
} | ||
public Object removeFirst(){ | ||
//通过头指针创建头节点 | ||
final Node hNode = head; | ||
if (hNode == null) { | ||
throw new NoSuchElementException(); | ||
} | ||
final Node next = hNode.next; | ||
final Object element = hNode.data; | ||
|
||
//移除 | ||
hNode.data = null; | ||
hNode.next = null; | ||
head = next; | ||
//判断是否为尾节点 | ||
if (next == null) { | ||
last = null; | ||
}else { | ||
next.prv = null; | ||
} | ||
size --; | ||
return element; | ||
} | ||
public Object removeLast(){ | ||
//通过尾指针创建节点 | ||
final Node lastNode = last; | ||
if (lastNode == null) { | ||
throw new NoSuchElementException(); | ||
} | ||
final Object element = lastNode.data; | ||
final Node prve = lastNode.prv; | ||
|
||
//移除 | ||
lastNode.data = null; | ||
lastNode.prv = null; | ||
last = prve; | ||
|
||
if (prve == null) { | ||
head = null; | ||
} else { | ||
prve.next = null; | ||
} | ||
size --; | ||
return element; | ||
} | ||
public Iterator iterator(){ | ||
return new LinkedListIterator(); | ||
} | ||
|
||
private class LinkedListIterator implements Iterator{ | ||
|
||
private Node currentNode = head;//当前节点 | ||
|
||
public boolean hasNext() { | ||
if (currentNode == null) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public Object next() { | ||
Object element = currentNode.data; | ||
currentNode = currentNode.next; | ||
return element; | ||
} | ||
|
||
} | ||
|
||
//查找index节点,并返回该节点 | ||
Node node(int index) { | ||
// assert isElementIndex(index); | ||
|
||
if (index < (size >> 1)) { | ||
Node x = head; | ||
for (int i = 0; i < index; i++) | ||
x = x.next; | ||
return x; | ||
} else { | ||
Node x = last; | ||
for (int i = size - 1; i > index; i--) | ||
x = x.prv; | ||
return x; | ||
} | ||
} | ||
//检查索引 | ||
private void checkIndex(int index){ | ||
if (index < 0 || index > size) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
} | ||
private static class Node{ | ||
Object data; | ||
Node next; | ||
Node prv; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
group02/1554421063/src/com/github/FelixCJF/coding2017/basic/List.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,11 @@ | ||
package com.github.FelixCJF.coding2017.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(); | ||
public Iterator iterator(); | ||
} |
Oops, something went wrong.