forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 5
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 #2 from eloiseSJTU/master
add Test junit
- Loading branch information
Showing
25 changed files
with
1,297 additions
and
161 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
83 changes: 83 additions & 0 deletions
83
...02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.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,83 @@ | ||
package com.github.lhpmatlab.coding2017.basic; | ||
|
||
/** | ||
* Created by andy on 2017/2/18. | ||
*/ | ||
public class MyArrayList<T> { | ||
|
||
private Object[] initialArray = {}; | ||
private Object[] dataArray; | ||
private int initSize = 10; | ||
private int arraySize; | ||
public MyArrayList() { | ||
dataArray = initialArray; | ||
} | ||
|
||
public MyArrayList(int init) { | ||
dataArray = new Object[init]; | ||
} | ||
|
||
public void ensureCapacity(int newCapacity) { | ||
if (newCapacity < arraySize) | ||
return; | ||
|
||
Object[] old = dataArray; | ||
dataArray = new Object[newCapacity]; | ||
for (int i = 0; i < size(); i++) { | ||
dataArray[i] = old[i]; | ||
} | ||
} | ||
|
||
public void add(T element) { | ||
add(size(), element); | ||
} | ||
|
||
public void add(int index, T element) { | ||
if (size() == dataArray.length) { | ||
ensureCapacity(size()*2 + 1); | ||
} | ||
for(int i=arraySize;i>index;i--) { | ||
dataArray[i] = dataArray[i - 1]; | ||
} | ||
dataArray[index] = element; | ||
arraySize++; | ||
} | ||
|
||
public T delete(int index) { | ||
if (index < 0 || index > arraySize) { | ||
throw new ArrayIndexOutOfBoundsException(); | ||
} | ||
T removeElement = (T)dataArray[index]; | ||
for (int i = index; i < size() -1; i++) { | ||
dataArray[i] = dataArray[i + 1]; | ||
} | ||
arraySize--; | ||
return removeElement; | ||
} | ||
|
||
public T get(int index) { | ||
if (index < 0 || index > arraySize) { | ||
throw new ArrayIndexOutOfBoundsException(); | ||
} | ||
return (T)dataArray[index]; | ||
} | ||
|
||
public T set(int index, T newElement) { | ||
if (index < 0 || index > arraySize) { | ||
throw new ArrayIndexOutOfBoundsException(); | ||
} | ||
T oldElement = (T) dataArray[index]; | ||
dataArray[index] = newElement; | ||
|
||
return oldElement; | ||
} | ||
|
||
public int size() { | ||
return arraySize; | ||
} | ||
|
||
public boolean isEmpty() { | ||
return size() == 0; | ||
} | ||
|
||
} |
119 changes: 119 additions & 0 deletions
119
...2/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.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,119 @@ | ||
package com.github.lhpmatlab.coding2017.basic; | ||
|
||
/** | ||
* Created by andy on 2017/2/18. | ||
*/ | ||
public class MyLinkedList<T> { | ||
private class Node<T> { | ||
public Node<T> pre; | ||
public Node<T> next; | ||
public T data; | ||
|
||
public Node(Node<T> pre,Node<T> next,T data) { | ||
this.pre = pre; | ||
this.next = next; | ||
this.data = data; | ||
} | ||
} | ||
|
||
private int dataSize; | ||
|
||
private Node head; | ||
private Node tail; | ||
|
||
public MyLinkedList() { | ||
head = new Node<T>(null,null,null); | ||
tail = new Node(head, null, null); | ||
head.next = tail; | ||
dataSize = 0; | ||
} | ||
|
||
public void add(T t) { | ||
// add(size(), t); | ||
Node<T> newNode = new Node<>(null, tail, t); | ||
newNode.pre = tail.pre; | ||
tail.pre.next = newNode; | ||
tail.pre = newNode; | ||
dataSize++; | ||
|
||
} | ||
|
||
/** | ||
* 根据索引添加没有实现 | ||
* @param index | ||
* @param element | ||
*/ | ||
public void add(int index,T element) { | ||
//TODO 根据索引添加元素 | ||
// addBefore(getNode(index,0,size()-1),element); | ||
// if (index == dataSize) { | ||
// add(element); | ||
// } else { | ||
// | ||
// } | ||
} | ||
|
||
public T get(int index) { | ||
return getNode(index).data; | ||
} | ||
|
||
public T set(int index, T newValue) { | ||
Node<T> node = getNode(index); | ||
T oldData = node.data; | ||
node.data = newValue; | ||
return oldData; | ||
} | ||
|
||
public T remove(int index) { | ||
Node<T> node = getNode(index); | ||
node.next.pre = node.pre; | ||
node.pre.next = node.next; | ||
dataSize--; | ||
|
||
return node.data; | ||
|
||
} | ||
|
||
private void addBefore(Node<T> node, T element) { | ||
// newNode.pre.next = newNode; | ||
// node.pre = newNode; | ||
Node<T> pre = node.pre; | ||
Node<T> newNode = new Node<>(node.pre, node, element); | ||
node.pre = newNode; | ||
pre.next = newNode; | ||
|
||
dataSize++; | ||
} | ||
|
||
private Node<T> getNode(int index) { | ||
return getNode(index, 0, size()); | ||
} | ||
|
||
private Node<T> getNode(int index, int lower, int upper) { | ||
Node<T> p; | ||
if (index < lower || index > upper) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
|
||
if (index < size() / 2) { | ||
p = head.next; | ||
for (int i = 0; i < index; i++) { | ||
p = p.next; | ||
} | ||
} else { | ||
p = tail.pre; | ||
for (int i = size()-1; i > index; i--) { | ||
p = p.pre; | ||
} | ||
} | ||
return p; | ||
} | ||
|
||
public int size() { | ||
return dataSize; | ||
} | ||
|
||
public boolean isEmpty() { | ||
return size() == 0; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.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,29 @@ | ||
package com.github.lhpmatlab.coding2017.basic; | ||
|
||
/** | ||
* Created by andy on 2017/2/22. | ||
*/ | ||
public class MyQueue<T> { | ||
private MyLinkedList<T> link = new MyLinkedList<>(); | ||
|
||
public void enQueue(T t) { | ||
link.add(t); | ||
} | ||
|
||
public T deQueue() { | ||
if (size() <= 0) { | ||
return null; | ||
} | ||
T t = link.get(0); | ||
link.remove(0); | ||
return t; | ||
} | ||
|
||
public boolean isEmpty() { | ||
return size() == 0; | ||
} | ||
|
||
public int size() { | ||
return link.size(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.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.lhpmatlab.coding2017.basic; | ||
|
||
/** | ||
* Created by andy on 2017/2/22. | ||
*/ | ||
public class MyStack<T> { | ||
private MyArrayList<T> list = new MyArrayList<>(); | ||
|
||
public void push(T t) { | ||
list.add(t); | ||
} | ||
|
||
public T pop() { | ||
if (size() <= 0) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
return list.delete(size() - 1); | ||
} | ||
|
||
public T peek() { | ||
return list.get(size() - 1); | ||
} | ||
|
||
public boolean isEmpty() { | ||
return list.size() == 0; | ||
} | ||
public int size() { | ||
return list.size(); | ||
} | ||
} |
Oops, something went wrong.