forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
337 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,55 @@ | ||
package Week01; | ||
/* | ||
* time:2017-2-20 21:51 created | ||
* | ||
*/ | ||
public class ArrayList implements List{ | ||
|
||
private int size = 0; | ||
//开辟的空间只有100个 | ||
private Object[] elementData = new Object[100]; | ||
|
||
//向末位插入 | ||
public void add(Object o){ | ||
elementData[size++] = o; | ||
} | ||
|
||
//当前位置有元素,就向右移动当前位于该位置的元素及所有后续元素 | ||
public void add(int index, Object o){ | ||
System.arraycopy(elementData, index, elementData, index+1, size-index); | ||
elementData[index] = o; | ||
size++; | ||
} | ||
|
||
public Object get(int index){ | ||
return elementData[index]; | ||
} | ||
//移除此链表上指定的元素,右边元素左移 | ||
public Object remove(int index){ | ||
int numMoved = size - index - 1; | ||
if (numMoved > 0) | ||
System.arraycopy(elementData, index+1, elementData, index, numMoved); | ||
elementData[--size] = null; | ||
return elementData[index]; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return new ArrayListIterator(); | ||
} | ||
|
||
private class ArrayListIterator implements Iterator{ | ||
private int pos = 0; | ||
|
||
public boolean hashNext() { | ||
return pos < size(); | ||
} | ||
|
||
public Object next() { | ||
return elementData[pos++]; | ||
} | ||
} | ||
} |
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,26 @@ | ||
package Week01; | ||
// | ||
/* | ||
*»¹Ã»ÓÐÍê | ||
* */ | ||
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; | ||
} | ||
} |
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 Week01; | ||
//time | ||
public interface Iterator { | ||
public boolean hashNext(); | ||
public Object 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,173 @@ | ||
package Week01; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
/* | ||
* time:2017-2-22 13:00 | ||
* 参考:http://blog.csdn.net/jianyuerensheng/article/details/51204598 | ||
* http://www.jianshu.com/p/681802a00cdf | ||
* jdk1.8源码 | ||
* */ | ||
|
||
//这里采用的是双向链表,jdk1.6中linkedList基于双向循环链表实现 | ||
public class LinkedList implements List { | ||
|
||
private int size = 0; | ||
private Node first; //指向头结点 | ||
private Node last; //指向尾节点 | ||
|
||
//在链表的end添加元素,调用自己的addLast()方法 | ||
public void add(Object o){ | ||
addLast(o); | ||
} | ||
|
||
//加在index后面,这段代码参考同组同学 spike | ||
public void add(int index, Object o){ | ||
if (index < 0 || index > size) | ||
throw new IllegalArgumentException(); | ||
size++; | ||
if (index == size){ | ||
addLast(o); | ||
}else{ | ||
Node target = findIndex(index); | ||
Node newNode = new Node(o, target,target.next); | ||
if (last == target){ | ||
last = newNode; | ||
}else{ | ||
//target.next = newNode;这行要不要加 | ||
target.next.prev = newNode;//有点疑问 | ||
} | ||
} | ||
size++; | ||
} | ||
|
||
public Object get(int index){ | ||
if ( index < 0 || index > size){ | ||
throw new IllegalArgumentException(); | ||
} | ||
return findIndex(index).data; | ||
} | ||
//删除index指定的元素 | ||
public Object remove(int index){ | ||
if (index < 0 || index > size){ | ||
throw new IllegalArgumentException(); | ||
} | ||
|
||
Node target = findIndex(index); | ||
if (target == first){ | ||
first = first.next; | ||
first.prev = null; | ||
}else if(target == last){ | ||
last = last.prev; | ||
last.next = null; | ||
}else{ | ||
target.prev.next = target.next; | ||
target.next.prev = target.prev; | ||
} | ||
return target.data; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public void addFirst(Object o){ | ||
|
||
Node f = first; | ||
Node newNode = new Node(o,null,f); | ||
first = newNode; | ||
if (f == null) | ||
last = newNode; //如果f为null,说明只有last可以指向 | ||
else | ||
f.prev = newNode; | ||
size++; | ||
} | ||
|
||
public void addLast(Object o){ | ||
Node l = last; | ||
Node newNode = new Node(o, l, null); | ||
last = newNode; | ||
if (l == null) | ||
first = newNode; | ||
else | ||
l.next = newNode; | ||
size++; | ||
} | ||
|
||
|
||
public Object removeFirst() { | ||
if ( first == null) | ||
throw new NoSuchElementException(); | ||
Node f = first; | ||
Object data = f.data; | ||
Node next = f.next; | ||
//去除的元素指为null | ||
f.data = null; | ||
f.next = null; | ||
first = next; | ||
if (next == null) | ||
last = null; | ||
else | ||
next.prev = null; | ||
size--; | ||
return data; | ||
} | ||
|
||
public Object removeLast(){ | ||
if (last == null) | ||
throw new NoSuchElementException(); | ||
Node l = last; | ||
Object data = l.data; | ||
Node previous = l.prev; | ||
l.data = null; | ||
l.prev = null; | ||
last = previous; | ||
if (previous == null) | ||
first = null; | ||
else | ||
previous.next = null; | ||
size--; | ||
return data; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return new LinkedListIterator(); | ||
} | ||
|
||
private class LinkedListIterator implements Iterator { | ||
Node curNode = first; | ||
public boolean hashNext() { | ||
return curNode != null; | ||
} | ||
public Object next() { | ||
if (!hashNext()) | ||
throw new NoSuchElementException(); | ||
Object data = curNode.data; | ||
curNode = curNode.next; | ||
return data; | ||
} | ||
} | ||
private Node findIndex(int index) { | ||
Node target = first; | ||
int i = 0; | ||
while(i < index){ | ||
target = target.next; | ||
i++; | ||
} | ||
return target; | ||
} | ||
|
||
//定义结点 | ||
private static class Node{ | ||
private Object data; | ||
//默认也是null | ||
private Node prev = null; //上一个元素节点 | ||
private Node next = null;//下一个元素节点 | ||
|
||
public Node(Object data, Node pre, Node next){ | ||
this.data = data; | ||
this.prev = pre; | ||
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 Week01; | ||
//time: | ||
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(); | ||
} |
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,29 @@ | ||
package Week01; | ||
/* | ||
* time:2017-2-25 13:46 created by Doen | ||
* | ||
* */ | ||
public class Queue { | ||
private LinkedList elementData = new LinkedList(); | ||
//½ø¶ÓÁÐ | ||
|
||
public void enQueue(Object o){ | ||
elementData.add(o); | ||
} | ||
|
||
//³ö¶ÓÁÐ | ||
public Object deQueue(){ | ||
if (isEmpty()) | ||
throw new UnsupportedOperationException(); | ||
return elementData.remove(0); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return elementData.size() == 0; | ||
} | ||
|
||
public int size(){ | ||
return elementData.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,30 @@ | ||
package Week01; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
/* | ||
* time:2017-2-25 13:19 created by Doen | ||
* change | ||
* */ | ||
public class Stack { | ||
|
||
private ArrayList elementData = new ArrayList(); | ||
|
||
public void push(Object o){ | ||
elementData.add(o); | ||
} | ||
|
||
public Object pop(){ | ||
if (isEmpty()) | ||
throw new NoSuchElementException(); | ||
return elementData.remove(elementData.size()-1); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return elementData.size() == 0; | ||
} | ||
|
||
public int size(){ | ||
return elementData.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,9 @@ | ||
package Week01; | ||
//time | ||
public class Test { | ||
public static void main(String[] args){ | ||
ArrayList arraylist = new ArrayList(); | ||
arraylist.add(1); | ||
arraylist.add("A"); | ||
} | ||
} |