forked from diliuzuzhanghao/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 0
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 diliuzuzhanghao#22 from camilesing/dev
push first homework at here
- Loading branch information
Showing
13 changed files
with
702 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -13,4 +13,4 @@ hs_err_pid* | |
|
||
#ide config | ||
.metadata | ||
.recommenders | ||
.recommenders |
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,141 @@ | ||
package Impl; | ||
|
||
import Interface.ArrayList; | ||
import Interface.Iterator; | ||
import ex.MyArrest; | ||
|
||
/** | ||
* Created by Administrator on 2017/2/25. | ||
*/ | ||
public class MyArraryList extends ArrayList { | ||
private Object[] objArr; | ||
private int size; | ||
private int postion; | ||
|
||
public MyArraryList() { | ||
this.objArr = new Object[10]; | ||
this.size = 10; | ||
this.postion = 0; | ||
} | ||
|
||
|
||
public MyArraryList(int size) { | ||
this.objArr = new Object[size]; | ||
this.size = size; | ||
this.postion = 0; | ||
} | ||
|
||
public MyArraryList(Object[] objArr) { | ||
this.objArr = objArr; | ||
this.size = objArr.length; | ||
this.postion = objArr.length - 1; | ||
} | ||
|
||
@Override | ||
public void add(Object o) { | ||
int limit = this.size + (this.size / 2); | ||
Object[] newObjArr = new Object[limit]; | ||
//public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)从指定源数组中复制一个数组, | ||
// 复制从指定的位置开始,到目标数组的指定位置结束。从src引用的源数组到dest引用的目标数组, | ||
// 数组组件的一个子序列被复制下来。被复制的组件的编号等于length参数。 | ||
// 源数组中位置在srcPos到srcPos+length-1之间的组件被分别复制到目标数组中的destPos到destPos+length-1位置。 | ||
System.arraycopy(this.objArr, 0, newObjArr, 0, objArr.length); | ||
this.postion = this.size - 1; | ||
newObjArr[this.postion] = o; | ||
this.size = limit; | ||
objArr = null; | ||
this.objArr = newObjArr; | ||
} | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
arrIndexVildate(index); | ||
objArr[index - 1] = o; | ||
size++; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
arrIndexVildate(index); | ||
return objArr[index - 1]; | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
arrIndexVildate(index); | ||
Object remoteObj = objArr[index - 1]; | ||
objArr[index - 1] = null; | ||
size--; | ||
//TODO need GC ccontrol | ||
return remoteObj; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return this.size; | ||
} | ||
|
||
@Override | ||
public Iterator iterator() { | ||
return new ArrayListIterator(this); | ||
} | ||
|
||
private class ArrayListIterator implements Iterator { | ||
private MyArraryList arraryList; | ||
private int index; | ||
|
||
public ArrayListIterator(MyArraryList arraryList) { | ||
this.arraryList = arraryList; | ||
this.index = arraryList.size - 1; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if (index > arraryList.size) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
Object obj = arraryList.get(index); | ||
index++; | ||
return obj; | ||
} | ||
} | ||
|
||
private void arrIndexVildate(int index) { | ||
if (index > size - 1 || index < 0) { | ||
new Exception(String.format("cant than that array index %s,but got %", size - 1, index)); | ||
} | ||
} | ||
|
||
//test method | ||
public static void main(String[] args) { | ||
MyArraryList myArrary = new MyArraryList(); | ||
MyArrest.arrestEq(10, myArrary.size()); | ||
myArrary.add(1, 10); | ||
MyArrest.arrestEq(10, myArrary.get(1)); | ||
myArrary.add(100); | ||
System.out.println(myArrary.get(11)); | ||
myArrary.remove(1); | ||
MyArrest.arrestIsNull(myArrary.get(1)); | ||
if (myArrary.iterator().hasNext()) { | ||
myArrary.iterator().next(); | ||
} | ||
System.out.println("test myArrary2"); | ||
MyArraryList myArrary2 = new MyArraryList(20); | ||
MyArrest.arrestEq(20, myArrary2.size()); | ||
myArrary2.add(1, 10); | ||
MyArrest.arrestEq(10, myArrary2.get(1)); | ||
myArrary2.add(100); | ||
MyArrest.arrestIsNull(myArrary2.get(20)); | ||
myArrary2.remove(1); | ||
MyArrest.arrestIsNull(myArrary2.get(1)); | ||
if (myArrary.iterator().hasNext()) { | ||
myArrary2.iterator().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,177 @@ | ||
package Impl; | ||
|
||
import Interface.Iterator; | ||
import Interface.LinkedList; | ||
import ex.MyArrest; | ||
|
||
/** | ||
* Created by Administrator on 2017/2/26. | ||
*/ | ||
public class MyLinkedList extends LinkedList { | ||
private MyLinkedList.Node head; | ||
private int size = 1; | ||
|
||
public MyLinkedList() { | ||
|
||
} | ||
|
||
private static class Node { | ||
Object data; | ||
MyLinkedList.Node next; | ||
|
||
public Node(Object data, Node next) { | ||
this.data = data; | ||
this.next = next; | ||
} | ||
} | ||
|
||
public void add(Object o) { | ||
if (this.size == 1) { | ||
head.data = o; | ||
return; | ||
} | ||
MyLinkedList.Node newHead = new MyLinkedList.Node(o, this.head); | ||
this.size += 1; | ||
this.head = newHead; | ||
} | ||
|
||
|
||
public void add(int index, Object o) { | ||
IndexVildate(index); | ||
int pos = 0; | ||
if (index == 1) { | ||
this.head = new Node(o, null); | ||
return; | ||
} | ||
for (MyLinkedList.Node node = this.head; node != null; node = node.next) { | ||
pos += 1; | ||
if (pos == index - 1) { | ||
node.data = o; | ||
this.size += 1; | ||
} | ||
} | ||
} | ||
|
||
|
||
public Object get(int index) { | ||
int pos = 0; | ||
for (MyLinkedList.Node node = this.head; node != null; node = node.next) { | ||
if (pos == index - 1) { | ||
return node.data; | ||
} | ||
pos += 1; | ||
} | ||
return null; | ||
} | ||
|
||
|
||
public Object remove(int index) { | ||
IndexVildate(index); | ||
int pos = 0; | ||
MyLinkedList.Node preNode; | ||
for (MyLinkedList.Node node = this.head; node != null; node = node.next) { | ||
pos += 1; | ||
if (pos == index - 2) { | ||
//record previous node | ||
preNode = node; | ||
if (pos == index - 1) { | ||
MyLinkedList.Node willDelNode = node; | ||
preNode.next = node.next; | ||
node = null; | ||
this.size -= 1; | ||
return willDelNode; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
|
||
public int size() { | ||
return this.size; | ||
} | ||
|
||
|
||
public void addFirst(Object o) { | ||
MyLinkedList.Node newHead = this.head; | ||
newHead.data = o; | ||
newHead.next = this.head; | ||
this.size += 1; | ||
this.head = newHead; | ||
} | ||
|
||
|
||
public void addLast(Object o) { | ||
for (MyLinkedList.Node node = this.head; node != null; node = node.next) { | ||
if (node.next == null) { | ||
MyLinkedList.Node lastNode = new MyLinkedList.Node(o, null); | ||
node.next = lastNode; | ||
this.size += 1; | ||
} | ||
} | ||
} | ||
|
||
|
||
public Object removeFirst() { | ||
MyLinkedList.Node oldHead = this.head; | ||
this.head = oldHead.next; | ||
this.size -= 1; | ||
return oldHead; | ||
} | ||
|
||
|
||
public Object removeLast() { | ||
for (MyLinkedList.Node node = this.head; node != null; node = node.next) { | ||
if (node.next == null) { | ||
MyLinkedList.Node willDelNode = node.next; | ||
node.next = null; | ||
this.size -= 1; | ||
return willDelNode; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public Iterator iterator() { | ||
return new LinkedListIterator(this); | ||
} | ||
|
||
private class LinkedListIterator implements Iterator { | ||
private MyLinkedList linkedList; | ||
private int index; | ||
|
||
public LinkedListIterator(MyLinkedList linkedList) { | ||
this.linkedList = linkedList; | ||
this.index = linkedList.size; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if (index > linkedList.size) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
Object obj = linkedList.get(index); | ||
index++; | ||
return obj; | ||
} | ||
} | ||
|
||
private void IndexVildate(int index) { | ||
if (index > this.size || index < 0) { | ||
System.out.println("happend error"); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
MyLinkedList linkedList = new MyLinkedList(); | ||
linkedList.add(1, 23); | ||
MyArrest.arrestEqByBasicType(1, linkedList.size()); | ||
|
||
} | ||
} |
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,68 @@ | ||
package Impl; | ||
|
||
import Interface.Queue; | ||
|
||
/** | ||
* Created by Administrator on 2017/2/26. | ||
*/ | ||
public class MyQueue extends Queue { | ||
|
||
private Node first; // beginning of queue | ||
private Node last; // end of queue | ||
private int size; // number of elements on queue | ||
|
||
private static class Node { | ||
private Object value; | ||
private Node next; | ||
|
||
public Node(Object value, Node next) { | ||
this.value = value; | ||
this.next = next; | ||
} | ||
} | ||
|
||
public MyQueue() { | ||
first = null; | ||
last = null; | ||
int n = 0; | ||
} | ||
|
||
@Override | ||
public void enQueue(Object o) { | ||
Node oldlast = this.last; | ||
this.last = new Node(o, null); | ||
size += 1; | ||
//第一个进队列 | ||
if (isEmpty()) { | ||
first = last; | ||
} else { | ||
oldlast.next = this.last; | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public Object deQueue() { | ||
if (isEmpty()) { | ||
return null; | ||
} else { | ||
Node oldFirst = this.first; | ||
Node newFirst = this.first.next; | ||
this.first = null; | ||
this.first = newFirst; | ||
this.size -= 1; | ||
return oldFirst; | ||
|
||
} | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return first == null; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
} |
Oops, something went wrong.