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.
Merge pull request #6 from ipk2015/master
121111914第一次提交代码
- Loading branch information
Showing
13 changed files
with
741 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
group24/121111914/src/com/github/ipk2015/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,77 @@ | ||
package com.github.ipk2015.coding2017.basic; | ||
|
||
import java.util.Arrays; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
public void add(Object o){ | ||
add(size,o); | ||
} | ||
/* | ||
* 分两种情况,index的范围为0到size,超出则抛出异常 | ||
* */ | ||
public void add(int index, Object o){ | ||
ListUtils.checkIndexInRange(index,size); | ||
if(size==elementData.length){ | ||
elementData=Arrays.copyOf(elementData, size+1); | ||
} | ||
if(index<size){ | ||
for(int i=size;i>index;i--){ | ||
elementData[i]=elementData[i-1]; | ||
} | ||
} | ||
elementData[index]=o; | ||
size++; | ||
} | ||
|
||
public Object get(int index){ | ||
ListUtils.checkIndexInRange(index,size-1); | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index){ | ||
ListUtils.checkIndexInRange(index,size-1); | ||
Object object=elementData[index]; | ||
for(int i=index;i<size-1;i++){ | ||
elementData[i]=elementData[i+1]; | ||
} | ||
elementData[size-1]=null; | ||
size--; | ||
return object; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
|
||
return new Iterator(){ | ||
private int currentPos=0; | ||
@Override | ||
public boolean hasNext() { | ||
if(size==0){ | ||
return false; | ||
}else{ | ||
if(currentPos<size){ | ||
return true; | ||
}else{ | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
Object object= get(currentPos); | ||
currentPos++; | ||
return object; | ||
} | ||
|
||
}; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
group24/121111914/src/com/github/ipk2015/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,61 @@ | ||
package com.github.ipk2015.coding2017.basic; | ||
|
||
public class BinaryTreeNode { | ||
|
||
private Comparable data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public Object getData() { | ||
return data; | ||
} | ||
public void setData(Comparable 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(Comparable o){ | ||
BinaryTreeNode insertNode=new BinaryTreeNode(); | ||
BinaryTreeNode compareNode=this; | ||
insertNode.setData(o); | ||
|
||
while(null!=compareNode){ | ||
if(null==compareNode.getData()){ | ||
compareNode.setData(o); | ||
break; | ||
}else{ | ||
Comparable com=(Comparable) compareNode.getData(); | ||
int result=com.compareTo(o); | ||
if(result==0){ | ||
break; | ||
}else if(result>0){ | ||
if(null==compareNode.getLeft()){ | ||
compareNode.setLeft(insertNode); | ||
break; | ||
} | ||
compareNode=compareNode.getLeft(); | ||
}else if(result<0){ | ||
if(null==compareNode.getRight()){ | ||
compareNode.setRight(insertNode); | ||
break; | ||
} | ||
compareNode=compareNode.getRight(); | ||
} | ||
} | ||
} | ||
|
||
return insertNode; | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
group24/121111914/src/com/github/ipk2015/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,6 @@ | ||
package com.github.ipk2015.coding2017.basic; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
} |
244 changes: 244 additions & 0 deletions
244
group24/121111914/src/com/github/ipk2015/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,244 @@ | ||
package com.github.ipk2015.coding2017.basic; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
public class LinkedList implements List { | ||
|
||
private Node head; | ||
|
||
public void add(Object o){ | ||
if(null==head){ | ||
head=new Node(); | ||
head.data=o; | ||
}else{ | ||
Node node=head; | ||
while(null!=node.next){ | ||
node=node.next; | ||
} | ||
Node addNode=new Node(); | ||
addNode.data=o; | ||
node.next=addNode; | ||
} | ||
} | ||
public void add(int index , Object o){ | ||
int size=size(); | ||
ListUtils.checkIndexInRange(index, size); | ||
if(index==size){ | ||
add(o); | ||
}else{ | ||
if(size==0){ | ||
head=new Node(); | ||
head.data=o; | ||
}else{ | ||
Node node=head; | ||
Node addNode=new Node(); | ||
addNode.data=o; | ||
for(int i=0;i<index-1;i++){ | ||
node=node.next; | ||
} | ||
addNode.next=node.next; | ||
node.next=addNode; | ||
} | ||
} | ||
} | ||
public Object get(int index){ | ||
ListUtils.checkIndexInRange(index, size()-1); | ||
Node node=head; | ||
for(int i=0;i<index;i++){ | ||
node=node.next; | ||
} | ||
return node.data; | ||
} | ||
public Object remove(int index){ | ||
int size=size(); | ||
ListUtils.checkIndexInRange(index, size-1); | ||
Node removeNode=head; | ||
if(index==0){ | ||
head=head.next; | ||
}else{ | ||
Node node=head; | ||
for(int i=0;i<index-1;i++){ | ||
node=node.next; | ||
} | ||
removeNode=node.next; | ||
node.next=removeNode.next; | ||
} | ||
return removeNode.data; | ||
} | ||
|
||
public int size(){ | ||
int size=0; | ||
if(null==head){ | ||
return size; | ||
}else{ | ||
Node node=head.next; | ||
size=1; | ||
while(null!=node){ | ||
node=node.next; | ||
size++; | ||
} | ||
} | ||
return size; | ||
} | ||
|
||
public void addFirst(Object o){ | ||
if(null==head){ | ||
head=new Node(); | ||
head.data=o; | ||
}else{ | ||
Node node=new Node(); | ||
node.data=head.data; | ||
node.next=head.next; | ||
head.next=node; | ||
head.data=o; | ||
} | ||
} | ||
public void addLast(Object o){ | ||
if(null==head){ | ||
head=new Node(); | ||
head.data=o; | ||
}else{ | ||
Node node=head; | ||
while(null!=node.next){ | ||
node=node.next; | ||
} | ||
Node addNode=new Node(); | ||
addNode.data=o; | ||
node.next=addNode; | ||
} | ||
} | ||
public Object removeFirst(){ | ||
if(null==head){ | ||
throw new NoSuchElementException(); | ||
} | ||
Node node=head; | ||
head=node.next; | ||
return node; | ||
} | ||
public Object removeLast(){ | ||
if(null==head){ | ||
throw new NoSuchElementException(); | ||
} | ||
Node node=head; | ||
if(null==node.next){ | ||
head=null; | ||
return node; | ||
}else{ | ||
while(null!=node.next.next){ | ||
node=node.next; | ||
} | ||
Node lastNode=node.next; | ||
node.next=null; | ||
return lastNode; | ||
} | ||
} | ||
public Iterator iterator(){ | ||
return new Iterator(){ | ||
private int currentPos=0; | ||
@Override | ||
public boolean hasNext() { | ||
int size=size(); | ||
if(size==0){ | ||
return false; | ||
}else{ | ||
if(currentPos<size){ | ||
return true; | ||
}else{ | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
Object object= get(currentPos); | ||
currentPos++; | ||
return object; | ||
} | ||
|
||
}; | ||
} | ||
|
||
|
||
private static class Node{ | ||
Object data; | ||
Node next; | ||
|
||
} | ||
|
||
/** | ||
* 把该链表逆置 | ||
* 例如链表为 3->7->10 , 逆置后变为 10->7->3 | ||
*/ | ||
public void reverse(){ | ||
|
||
} | ||
|
||
/** | ||
* 删除一个单链表的前半部分 | ||
* 例如:list = 2->5->7->8 , 删除以后的值为 7->8 | ||
* 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 | ||
*/ | ||
public void removeFirstHalf(){ | ||
|
||
} | ||
|
||
/** | ||
* 从第i个元素开始, 删除length 个元素 , 注意i从0开始 | ||
* @param i | ||
* @param length | ||
*/ | ||
public void remove(int i, int length){ | ||
|
||
} | ||
/** | ||
* 假定当前链表和list均包含已升序排列的整数 | ||
* 从当前链表中取出那些list所指定的元素 | ||
* 例如当前链表 = 11->101->201->301->401->501->601->701 | ||
* listB = 1->3->4->6 | ||
* 返回的结果应该是[101,301,401,601] | ||
* @param list | ||
*/ | ||
public static int[] getElements(LinkedList list){ | ||
return null; | ||
} | ||
|
||
/** | ||
* 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 | ||
* 从当前链表中中删除在list中出现的元素 | ||
* @param list | ||
*/ | ||
|
||
public void subtract(LinkedList list){ | ||
|
||
} | ||
|
||
/** | ||
* 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 | ||
* 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) | ||
*/ | ||
public void removeDuplicateValues(){ | ||
|
||
} | ||
|
||
/** | ||
* 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 | ||
* 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) | ||
* @param min | ||
* @param max | ||
*/ | ||
public void removeRange(int min, int max){ | ||
|
||
} | ||
|
||
/** | ||
* 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) | ||
* 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 | ||
* @param list | ||
*/ | ||
public LinkedList rsection( LinkedList list){ | ||
return null; | ||
} | ||
} |
Oops, something went wrong.