forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 15
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 #8 from begin16119/master
Master
- Loading branch information
Showing
8 changed files
with
429 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
101 changes: 101 additions & 0 deletions
101
group04/349184132/src/Collection/Onehomework/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,101 @@ | ||
package Collection.Onehomework; | ||
|
||
import java.util.Date; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData ; | ||
|
||
public ArrayList(){ this(10); } | ||
public ArrayList(int capacity){ | ||
if(capacity<0) | ||
throw new IllegalArgumentException(); | ||
elementData = new Object[capacity]; | ||
} | ||
public void add(Object o){ | ||
if(size==elementData.length) | ||
ResizeCapacity(); | ||
elementData[size++] = o; | ||
} | ||
private void ResizeCapacity(){ | ||
Object[] newDatas = new Object[size*2+1]; | ||
System.arraycopy(elementData,0,newDatas,0,size); | ||
elementData = newDatas; | ||
} | ||
private void rangeCheck(int index){ | ||
if(index<0 || index > size-1) | ||
throw new IllegalArgumentException(); | ||
} | ||
public void add(int index, Object o){ | ||
rangeCheck(index); | ||
System.arraycopy(elementData,index,elementData,index+1,size-index); | ||
elementData[index] = o; | ||
size++; | ||
} | ||
|
||
|
||
public Object get(int index){ | ||
rangeCheck(index); | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index){ | ||
rangeCheck(index); | ||
Object oldElement = elementData[index]; | ||
System.arraycopy(elementData,index+1,elementData,index,size-index-1); | ||
size--; | ||
return oldElement; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
public boolean isEmpty(){ return size==0; } | ||
|
||
public Iterator iterator(){ | ||
return new ArrayListIterator(); | ||
} | ||
|
||
private class ArrayListIterator implements Iterator{ | ||
private int pos = 0; | ||
@Override | ||
public boolean hasNext() { | ||
return pos<size; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
if(pos>size) | ||
throw new IllegalArgumentException(); | ||
return elementData[pos++]; | ||
} | ||
|
||
} | ||
|
||
public static void main(String[] args) { | ||
ArrayList list = new ArrayList(); | ||
list.add(1); | ||
list.add("str"); | ||
list.add(new Date()); | ||
|
||
|
||
|
||
Iterator iter = list.iterator(); | ||
while(iter.hasNext()){ | ||
System.out.println(iter.next()); | ||
} | ||
list.add(2,123); | ||
list.add(0,15); | ||
Iterator iter2 = list.iterator(); | ||
while(iter2.hasNext()){ | ||
System.out.println(iter2.next()); | ||
} | ||
|
||
|
||
|
||
} | ||
|
||
|
||
} |
79 changes: 79 additions & 0 deletions
79
group04/349184132/src/Collection/Onehomework/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,79 @@ | ||
package Collection.Onehomework; | ||
|
||
|
||
|
||
|
||
public class BinaryTreeNode { | ||
|
||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
private BinaryTreeNode root; | ||
|
||
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){ | ||
BinaryTreeNode newNode =null; | ||
if(o==null) | ||
throw new NullPointerException("数据不能为空"); | ||
|
||
if(root==null){ | ||
root = new BinaryTreeNode(); | ||
root.setData(o); | ||
}else { | ||
newNode = new BinaryTreeNode(); | ||
BinaryTreeNode nowNode = root; | ||
int val = (int)root.getData(); | ||
nowNode.setData(o); | ||
while(true) { | ||
|
||
if ((int)newNode.getData()< val) { | ||
if(nowNode.left==null){ | ||
nowNode.setLeft(newNode); | ||
break; | ||
} else { | ||
nowNode = nowNode.left; | ||
} | ||
} else if((int)newNode.getData()> val){ | ||
if (nowNode.right==null ) { | ||
nowNode.setRight(newNode); | ||
break; | ||
} else{ | ||
nowNode = newNode.right; | ||
|
||
} | ||
}else { | ||
|
||
throw new IllegalArgumentException("已存在元素结点"); | ||
} | ||
} | ||
} | ||
return newNode; | ||
} | ||
|
||
public static void main(String[] args) { | ||
|
||
} | ||
|
||
|
||
|
||
} |
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 Collection.Onehomework; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
170 changes: 170 additions & 0 deletions
170
group04/349184132/src/Collection/Onehomework/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,170 @@ | ||
package Collection.Onehomework; | ||
|
||
public class LinkedList implements List { | ||
|
||
private Node head; | ||
private Node now ; | ||
private int size = 0; | ||
public void add(Object o){ | ||
if(head == null) { | ||
head = new Node(o, null); | ||
now = head; | ||
} | ||
else{ | ||
|
||
Node node = new Node(o,null); | ||
now.next = node; | ||
now = node; | ||
} | ||
size++; | ||
|
||
} | ||
private void rangeCheck(int index) { | ||
if (index < 0 || index > size - 1) | ||
throw new IllegalArgumentException(); | ||
} | ||
public void add(int index , Object o){ | ||
rangeCheck(index); | ||
|
||
if(index==0){ | ||
addFirst(o); | ||
}else { | ||
Node node = new Node(o,null); | ||
Node now = head; | ||
Node next = head; | ||
for (int i = 1; i <= index; i++) { | ||
next = next.next; | ||
if (i == index) { | ||
node.next = next; | ||
now.next = node; | ||
} | ||
now = now.next; | ||
} | ||
size++; | ||
} | ||
} | ||
|
||
public Object get(int index){ | ||
Node indexNode = head; | ||
if(index==0) | ||
return indexNode.data; | ||
else { | ||
|
||
for (int i = 1; i <= index; i++) { | ||
indexNode = indexNode.next; | ||
if (i == index) | ||
return indexNode.data; | ||
} | ||
} | ||
return null; | ||
} | ||
public Object remove(int index){ | ||
rangeCheck(index); | ||
|
||
if(index == 0){ | ||
return removeFirst(); | ||
}else { | ||
Node pre = head; | ||
Node now = head; | ||
for (int i = 1; i <= index; i++) { | ||
now = now.next; | ||
if (i == index) { | ||
pre.next = now.next; | ||
} | ||
pre = pre.next; | ||
} | ||
size--; | ||
return now.data; | ||
} | ||
} | ||
|
||
public int size(){ | ||
return size ; | ||
} | ||
|
||
public boolean isEmpty(){ return size==0; } | ||
|
||
public void addFirst(Object o){ | ||
Node oldhead = head; | ||
Node newhead = new Node(o,oldhead); | ||
head = newhead; | ||
size++; | ||
} | ||
public void addLast(Object o){ | ||
Node node = head; | ||
while(node !=null){ | ||
node = node.next; | ||
if(node==null){ | ||
Node lastnode = new Node(o,null); | ||
node.next = lastnode; | ||
} | ||
} | ||
size++; | ||
} | ||
public Object removeFirst(){ | ||
Node oldhead = head; | ||
Node newhead = head.next; | ||
oldhead.next = null; | ||
head = newhead; | ||
size--; | ||
return oldhead.data; | ||
} | ||
public Object removeLast(){ | ||
Node node = head; | ||
Node prev = head; | ||
while(node !=null){ | ||
node = node.next; | ||
if(node==null){ | ||
prev.next = null; | ||
} | ||
prev = prev.next; | ||
} | ||
size--; | ||
return node.data; | ||
} | ||
public Iterator iterator(){ | ||
return new LinkedListIterator(); | ||
} | ||
|
||
private class LinkedListIterator implements Iterator{ | ||
int pos = 0; | ||
@Override | ||
public boolean hasNext() { | ||
return pos<size; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
if(pos>size) | ||
throw new IllegalArgumentException(); | ||
return get(pos++); | ||
} | ||
} | ||
private static class Node{ | ||
Object data; | ||
Node next; | ||
private Node(Object data,Node next){ | ||
this.data = data; | ||
this.next = next; | ||
|
||
} | ||
|
||
|
||
} | ||
|
||
public static void main(String[] args) { | ||
LinkedList list = new LinkedList(); | ||
list.add(1); | ||
list.add(2); | ||
list.add(3); | ||
list.add(0,15); | ||
list.add(1,14); | ||
list.removeLast(); | ||
list.addFirst(1); | ||
list.removeFirst(); | ||
Iterator iter = list.iterator(); | ||
while(iter.hasNext()){ | ||
System.out.println(iter.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,9 @@ | ||
package Collection.Onehomework; | ||
|
||
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(); | ||
} |
Oops, something went wrong.