-
Notifications
You must be signed in to change notification settings - Fork 641
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 #27 from hztaiyang/master
update from ogrgin
- Loading branch information
Showing
7 changed files
with
419 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,103 @@ | ||
package com.rd.p2p.common.util.liuxin; | ||
|
||
import java.util.Arrays; | ||
|
||
public class ArrayList<E> implements List<E> { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[10]; | ||
|
||
public void add(E o){ | ||
if(size >= elementData.length){ | ||
elementData = Arrays.copyOf(elementData, elementData.length * 2); | ||
} | ||
elementData[size++] = o; | ||
size++; | ||
} | ||
|
||
public void add(int index, E o){ | ||
if(index > size){ | ||
throw new ArrayIndexOutOfBoundsException("插入索引数不能大于数列总长度 " + index + ">" + size); | ||
} | ||
if(size >= elementData.length){ | ||
elementData = Arrays.copyOf(elementData, elementData.length * 2); | ||
} | ||
Object[] tempData = Arrays.copyOfRange(elementData, index, size); | ||
elementData[index] = o; | ||
for(int i = 1; i <= tempData.length; i++){ | ||
elementData[i+index] = tempData[i-1]; | ||
} | ||
size++; | ||
|
||
} | ||
|
||
public Object get(int index){ | ||
if(index >= size){ | ||
throw new ArrayIndexOutOfBoundsException("取出数组索引不能大于等于数组总长度 " + index + ">=" + size); | ||
} | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index){ | ||
if(index >= size){ | ||
throw new ArrayIndexOutOfBoundsException("移除索引数不能大于等于数列总长度 " + index + ">=" + size); | ||
} | ||
Object data = get(index); | ||
Object[] tempData = Arrays.copyOfRange(elementData, index+1, size); | ||
for(int i = 0; i < tempData.length; i++){ | ||
elementData[index+i] = tempData[i]; | ||
} | ||
elementData[size - 1] = null; | ||
size--; | ||
return data; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
for (Object object : elementData) { | ||
System.out.println(object); | ||
} | ||
return null; | ||
} | ||
|
||
//迭代器 | ||
public Iterator iterator(){ | ||
return new Iterator() { | ||
|
||
private int index = 0; | ||
|
||
@Override | ||
public Object next() { | ||
if(index >= size){ | ||
throw new ArrayIndexOutOfBoundsException("取出数组索引不能大于等于数组总长度 " + index + ">=" + size); | ||
} | ||
return get(index++); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if(size > index){ | ||
return true; | ||
}else{ | ||
return false; | ||
} | ||
} | ||
}; | ||
} | ||
|
||
public static void main(String[] args) { | ||
ArrayList<Integer> list = new ArrayList<Integer>(); | ||
list.add(1); | ||
list.add(2); | ||
list.add(3,3); | ||
Iterator in = list.iterator(); | ||
while(in.hasNext()){ | ||
System.out.println(in.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,68 @@ | ||
package com.rd.p2p.common.util.liuxin; | ||
|
||
/** | ||
* 用Integer易于比较和插入 | ||
* @author jhn | ||
* time:2017年2月24日 | ||
*/ | ||
public class BinaryTreeNode { | ||
|
||
private Integer data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public BinaryTreeNode(){ | ||
|
||
} | ||
|
||
public BinaryTreeNode(Integer integer){ | ||
this.data = integer; | ||
} | ||
|
||
public Integer getData() { | ||
return data; | ||
} | ||
public void setData(Integer 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(Integer o){ | ||
if(data == null){ | ||
data = o; | ||
return this; | ||
} | ||
BinaryTreeNode node = new BinaryTreeNode(o); | ||
BinaryTreeNode tempBinaryTreeNode = this; | ||
boolean begin = true; | ||
while(begin){ | ||
if(o < data){ | ||
tempBinaryTreeNode = tempBinaryTreeNode.getLeft(); | ||
if(tempBinaryTreeNode.getLeft() == null){ | ||
tempBinaryTreeNode.setLeft(node); | ||
begin = false;; | ||
} | ||
}else{ | ||
tempBinaryTreeNode = tempBinaryTreeNode.getRight(); | ||
if(tempBinaryTreeNode.getRight() == null){ | ||
tempBinaryTreeNode.setRight(node); | ||
begin = false;; | ||
} | ||
} | ||
|
||
} | ||
return node; | ||
} | ||
|
||
} |
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.rd.p2p.common.util.liuxin; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
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,150 @@ | ||
package com.rd.p2p.common.util.liuxin; | ||
|
||
public class LinkedList<E> implements List<E> { | ||
|
||
private Node head; | ||
|
||
private int size; | ||
|
||
public void add(E o){ | ||
if(size == 0){ | ||
head = new Node(); | ||
head.data = o; | ||
}else{ | ||
Node node = new Node(); | ||
node.data = o; | ||
getNode(size-1).next = node; | ||
} | ||
size ++; | ||
} | ||
|
||
public void add(int index , E o){ | ||
Node node = new Node(); | ||
node.data = o; | ||
if(index == 0){ | ||
node.next = head; | ||
head = node; | ||
}else{ | ||
Node indexNode = getNode(index - 1); | ||
indexNode.next = node; | ||
if(index < size){ | ||
node.next = getNode(index); | ||
} | ||
} | ||
size ++; | ||
} | ||
|
||
public Object get(int index){ | ||
return getNode(index).data; | ||
} | ||
|
||
public Object remove(int index){ | ||
if(index > size - 1){ | ||
throw new ArrayIndexOutOfBoundsException("移除索引超出数组索引边界 " + index + ">" + (size - 1)); | ||
} | ||
if(index < 0){ | ||
throw new ArrayIndexOutOfBoundsException("索引不能为负数"); | ||
} | ||
Node returnNode = null; | ||
if(index == 0){ | ||
returnNode = head; | ||
if(head.next != null){ | ||
head = head.next; | ||
}else{ | ||
head = null; | ||
} | ||
}else{ | ||
returnNode = getNode(index); | ||
if(returnNode.next != null){ | ||
Node preNode = getNode(index-1); | ||
Node nextNode = getNode(index+1); | ||
preNode.next = nextNode; | ||
} | ||
} | ||
size--; | ||
return returnNode.data; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public void addFirst(E o){ | ||
add(0,o); | ||
} | ||
|
||
public void addLast(E o){ | ||
add(size-1,o); | ||
} | ||
|
||
public Object removeFirst(){ | ||
return remove(0); | ||
} | ||
|
||
public Object removeLast(){ | ||
return remove(size - 1); | ||
} | ||
|
||
private Node getNode(int index){ | ||
if(index > size - 1){ | ||
throw new ArrayIndexOutOfBoundsException("查询索引超出数组索引边界 " + index + ">" + (size - 1)); | ||
} | ||
if(index < 0){ | ||
throw new ArrayIndexOutOfBoundsException("索引不能为负数"); | ||
} | ||
Node tempNode = head; | ||
if(index == 0){ | ||
tempNode = head; | ||
}else{ | ||
for(int i = 0; i < index; i++){ | ||
tempNode = tempNode.next; | ||
} | ||
} | ||
return tempNode; | ||
} | ||
|
||
private static class Node{ | ||
Object data; | ||
Node next; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
for (int i = 0; i < size; i++) { | ||
System.out.println(get(i)); | ||
} | ||
return null; | ||
} | ||
|
||
//迭代器 | ||
public Iterator iterator(){ | ||
return new Iterator() { | ||
private int index = 0; | ||
|
||
@Override | ||
public Object next() { | ||
if(index >= size){ | ||
throw new ArrayIndexOutOfBoundsException("取出数组索引不能大于等于数组总长度 " + index + ">=" + size); | ||
} | ||
return get(index++); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if(size > index){ | ||
return true; | ||
}else{ | ||
return false; | ||
} | ||
} | ||
}; | ||
} | ||
|
||
public static void main(String[] args) { | ||
LinkedList<Integer> list = new LinkedList<Integer>(); | ||
list.add(1); | ||
list.add(2); | ||
list.add(3); | ||
list.toString(); | ||
} | ||
} |
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 com.rd.p2p.common.util.liuxin; | ||
|
||
public interface List<E> { | ||
public void add(E o); | ||
public void add(int index, E 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,30 @@ | ||
package com.rd.p2p.common.util.liuxin; | ||
|
||
public class Queue<T> { | ||
|
||
ArrayList<T> arrayList = new ArrayList<T>(); | ||
|
||
public void enQueue(T o){ | ||
arrayList.add(o); | ||
} | ||
|
||
public T deQueue(){ | ||
if(arrayList.size() > 0){ | ||
@SuppressWarnings("unchecked") | ||
T t = (T) arrayList.get(0); | ||
arrayList.remove(0); | ||
return t; | ||
}else{ | ||
return null; | ||
} | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return arrayList.size() == 0 ? true : false; | ||
} | ||
|
||
public int size(){ | ||
return arrayList.size(); | ||
} | ||
} | ||
|
Oops, something went wrong.