Skip to content

Commit

Permalink
Merge pull request #1 from eloiseSJTU/master
Browse files Browse the repository at this point in the history
update
  • Loading branch information
FelixCJF authored Feb 28, 2017
2 parents c707d7d + 37551bd commit f59cd60
Show file tree
Hide file tree
Showing 771 changed files with 39,587 additions and 7 deletions.
17 changes: 16 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
*.class

# Mobile Tools for Java (J2ME)
.mtj.tmp/

Expand All @@ -8,9 +7,25 @@
*.war
*.ear

*.iml
*.idea


# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

#ide config
.metadata
.recommenders


#macOS
.DS_Store

.idea/
*.iml
rebel.*
.rebel.*

target

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;
}
}

}
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;
}

}
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();

}
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;
}

}


}
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();
}
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;
}

}
Loading

0 comments on commit f59cd60

Please sign in to comment.