forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 2
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 #1 from eloiseSJTU/master
update
- Loading branch information
Showing
771 changed files
with
39,587 additions
and
7 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
97 changes: 97 additions & 0 deletions
97
group02/106614649/106614649Learnin/src/com/github/Ven13/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,97 @@ | ||
package com.github.Ven13.coding2017.basic; | ||
|
||
public class ArrayList implements List { | ||
|
||
//返回集合大小 | ||
private int size = 0; | ||
|
||
//先给定一个长度为10的数组 | ||
Object[] elementData = new Object[100]; | ||
|
||
@Override | ||
//动态添加元素 | ||
public void add(Object o) { | ||
//先判断数组是否已满 | ||
if(size == elementData.length) { | ||
Object[] newObjects = new Object[elementData.length * 2]; | ||
System.arraycopy(elementData, 0, newObjects, 0, elementData.length); | ||
elementData = newObjects; | ||
} | ||
|
||
//为新添加的元素指定下标 | ||
elementData[size] = o; | ||
size++; | ||
} | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
//先判断数组是否已满 | ||
if(size == elementData.length) { | ||
Object[] newObjects = elementData; | ||
this.elementData = new Object[elementData.length * 2]; | ||
for(int j = 0; j < newObjects.length; j++) { | ||
this.elementData[j] = newObjects[j]; | ||
} | ||
} | ||
|
||
for(int i = size - 1; i >= index; i--) { | ||
elementData[i+1] = elementData[i]; | ||
} | ||
|
||
elementData[index] = o; | ||
size++; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
return elementData[index]; | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
if (index > size) { | ||
return null; | ||
}; | ||
|
||
int moveSize = size - index - 1; | ||
|
||
if (moveSize > 0) { | ||
System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); | ||
} | ||
elementData[--size] = null; | ||
|
||
//for(int i = index; i < elementData.length; i++) { | ||
// elementData[i] = elementData[i+1]; | ||
//} | ||
|
||
return elementData; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return new ArrayListIterator(); | ||
} | ||
|
||
private class ArrayListIterator implements Iterator { | ||
|
||
private int currentIndex = 0; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if(currentIndex >= size) return false; | ||
else return true; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
Object o = elementData[currentIndex]; | ||
currentIndex ++; | ||
return o; | ||
} | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
group02/106614649/106614649Learnin/src/com/github/Ven13/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.Ven13.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; | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
group02/106614649/106614649Learnin/src/com/github/Ven13/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,7 @@ | ||
package com.github.Ven13.coding2017.basic; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
173 changes: 173 additions & 0 deletions
173
group02/106614649/106614649Learnin/src/com/github/Ven13/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,173 @@ | ||
package com.github.Ven13.coding2017.basic; | ||
|
||
public class LinkedList implements List { | ||
|
||
//表示该链表的长度 | ||
private int size; | ||
|
||
//链表的头元素 | ||
private Node head; | ||
//链表的尾元素 | ||
private Node tail; | ||
|
||
//使用内部类来实现链表的每一个节点,每个节点有一个指向下一个元素的next,以及自身的data | ||
private static class Node { | ||
public Object data; | ||
public Node next; | ||
|
||
public Node(Object data) { | ||
this.data = data; | ||
} | ||
} | ||
|
||
//链表的构造方法 | ||
public LinkedList() { | ||
} | ||
|
||
@Override | ||
public void add(Object o) { | ||
add(size, o); | ||
} | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
if(index == 0) { | ||
addFirst(o); | ||
} else { | ||
if(index >= size) { | ||
addLast(o); | ||
} else { | ||
Node node = head; | ||
for (int i = 1; i < index; i++) { | ||
head = head.next; | ||
} | ||
Node nextNode = node.next; | ||
Node temp = new Node(o); | ||
node.next = temp; | ||
temp.next = nextNode; | ||
size++; | ||
} | ||
} | ||
} | ||
|
||
//添加前面 | ||
public void addFirst(Object o) { | ||
Node newNode = new Node(o); | ||
newNode.next = head; | ||
head = newNode; | ||
size++; | ||
if(tail == null) { | ||
tail = head; | ||
} | ||
} | ||
|
||
//添加后面 | ||
public void addLast(Object o) { | ||
if(tail == null) { | ||
tail = head = new Node(o); | ||
} else { | ||
Node newNode = new Node(o); | ||
tail.next = newNode; | ||
tail = tail.next; | ||
} | ||
size++; | ||
} | ||
|
||
|
||
@Override | ||
public Object get(int index) { | ||
Node node = head; | ||
for(int i = 0; i < index; i++) { | ||
node = node.next; | ||
} | ||
return node.data; | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
if(size == 0) { | ||
throw new java.util.NoSuchElementException(); | ||
} | ||
if(index == 0) { | ||
Node node = head; | ||
Node temp = node.next; | ||
head = temp; | ||
size--; | ||
return node.data; | ||
} else { | ||
if(index >= size) { | ||
throw new java.util.NoSuchElementException(); | ||
} else { | ||
Node node = head; | ||
for(int i = 1; i < index; i++) { | ||
node = node.next; | ||
} | ||
Node temp = node.next; | ||
node.next = temp.next; | ||
size--; | ||
return node.data; | ||
} | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
public Object removeFirst() { | ||
//通过头指针创建头节点 | ||
Node hNode = head; | ||
if (hNode == null) { | ||
throw new java.util.NoSuchElementException(); | ||
} | ||
Node nNode = hNode.next; | ||
Object element = hNode.data; | ||
|
||
//移除 | ||
hNode.data = null; | ||
hNode.next = null; | ||
head = nNode; | ||
//判断是否为尾节点 | ||
if (nNode == null) { | ||
tail = null; | ||
}else { | ||
nNode = null; | ||
} | ||
size --; | ||
return element; | ||
} | ||
|
||
public Object removeLast() { | ||
return remove(size - 1); | ||
} | ||
|
||
public Iterator iterator() { | ||
return new LinkedListIterator(); | ||
} | ||
|
||
private class LinkedListIterator implements Iterator { | ||
|
||
private Node node = head.next; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return node != tail; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
|
||
if(!hasNext()) { | ||
throw new java.util.NoSuchElementException(); | ||
} | ||
Object nextData = node.data; | ||
node = node.next; | ||
return nextData; | ||
} | ||
|
||
} | ||
|
||
|
||
} |
11 changes: 11 additions & 0 deletions
11
group02/106614649/106614649Learnin/src/com/github/Ven13/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.Ven13.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(); | ||
} |
30 changes: 30 additions & 0 deletions
30
group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Queue.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,30 @@ | ||
package com.github.Ven13.coding2017.basic; | ||
|
||
public class Queue { | ||
|
||
private LinkedList list = new LinkedList(); | ||
private int size = 0; | ||
|
||
public void enQueue(Object o){ | ||
size++; | ||
list.addLast(o); | ||
} | ||
|
||
public Object deQueue(){ | ||
size--; | ||
return list.removeFirst(); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
if(size == 0) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
} |
Oops, something went wrong.