forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
由于fork错了地址,重新提交第一次作业 2017-3-1 CoderXLoong
- Loading branch information
1 parent
b72a63e
commit 6bb35fd
Showing
93 changed files
with
5,353 additions
and
0 deletions.
There are no files selected for viewing
128 changes: 128 additions & 0 deletions
128
group13/1274639949/lesson01/src/com/hans/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,128 @@ | ||
package com.hans; | ||
|
||
import java.util.Arrays; | ||
|
||
import javax.management.RuntimeErrorException; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0;//ArrayList中已经存储的元素个数,也是下一个存入的元素的下标。 | ||
|
||
private Object[] elementData = null; | ||
|
||
public ArrayList() { | ||
elementData = new Object[3]; | ||
} | ||
|
||
public ArrayList(int capacity){ | ||
elementData = new Object[capacity]; | ||
} | ||
|
||
// private static final final default | ||
|
||
/** | ||
* 将参数o添加到ArrayList中最后一个元素的后面 | ||
* @param o 添加的元素 | ||
*/ | ||
public void add(Object o){ | ||
checkSize(); | ||
// add(size++, o); | ||
elementData[size++] = o; | ||
} | ||
|
||
/** | ||
* 将参数o添加到指定的index位置 | ||
* index应满速 index >= 0 && index <= size() 为 true | ||
* @param index 添加的位置 | ||
* @param o 添加的元素 | ||
*/ | ||
public void add(int index, Object o){ | ||
if(index > size || index < 0){ | ||
//size代表即将存入的元素的下标,所以index等于size也可以。 | ||
throw new ArrayIndexOutOfBoundsException("Index:" + index); | ||
} | ||
checkSize(); | ||
System.arraycopy(elementData, index, elementData, index + 1, size - index); | ||
elementData[index] = o; | ||
size++; | ||
} | ||
|
||
/** | ||
* 获取输入的index位置处元素 | ||
* index应满足 index >= 0 && index < size() 为 true | ||
*/ | ||
public Object get(int index){ | ||
checkIndex(index); | ||
return elementData[index]; | ||
} | ||
|
||
/** | ||
* 删除输入的index位置处的元素 | ||
* index应满足 index >= 0 && index < size() 为 true | ||
* @return 被删除的元素 | ||
*/ | ||
public Object remove(int index){ | ||
checkIndex(index); | ||
Object obj = elementData[index]; | ||
System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); | ||
size--; | ||
return obj; | ||
} | ||
|
||
/** | ||
* 获取当前ArrayList中已经存储的元素的个数 | ||
*/ | ||
public int size(){ | ||
return this.size; | ||
} | ||
|
||
/** | ||
* 获取当前ArrayList的迭代器 | ||
* @return 当前ArrayList的迭代器 | ||
*/ | ||
public Iterator iterator(){ | ||
return new Iterator(){ | ||
private int count = 0; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return size > count; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
if(count == size){ | ||
throw new RuntimeErrorException(null, "没有更多的元素!"); | ||
} | ||
return get(count++); | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
ArrayList.this.remove(count - 1); | ||
} | ||
|
||
}; | ||
} | ||
|
||
/** | ||
* 用来检查get和remove操作中输入的下标是否越界 | ||
* @param index 输入的下标 | ||
*/ | ||
private void checkIndex(int index){ | ||
if(index >= size || index < 0){ | ||
//size代表即将存入的元素的下标,所以index下标上还没有存储内容,无法进行get和remove操作 | ||
throw new ArrayIndexOutOfBoundsException("Index:" + index); | ||
} | ||
} | ||
|
||
/** | ||
* 检查ArrayList是否将要存满。 | ||
* 在ArrayList将要存满时对其进行扩容,每次扩容将使其容量增加20。 | ||
*/ | ||
private void checkSize(){ | ||
if(size < elementData.length) return; | ||
elementData = Arrays.copyOf(elementData, elementData.length + 20/*(int)(elementData.length * 1.2)*/); | ||
} | ||
|
||
} |
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,10 @@ | ||
package com.hans; | ||
|
||
public class BinaryTree { | ||
BinaryTreeNode root = new BinaryTreeNode(); | ||
|
||
public boolean add(Object obj){ | ||
|
||
return true; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
group13/1274639949/lesson01/src/com/hans/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,32 @@ | ||
package com.hans; | ||
|
||
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; | ||
} | ||
|
||
} |
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,8 @@ | ||
package com.hans; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
public void remove(); | ||
} | ||
|
148 changes: 148 additions & 0 deletions
148
group13/1274639949/lesson01/src/com/hans/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,148 @@ | ||
package com.hans; | ||
|
||
public class LinkedList implements List { | ||
|
||
private int size; | ||
|
||
private Node head; | ||
|
||
public LinkedList() { | ||
head = new Node(); | ||
// head.data = head; | ||
} | ||
|
||
/** | ||
* 添加一个元素 | ||
*/ | ||
public void add(Object o){ | ||
Node tail = getTail(); | ||
|
||
Node node = new Node(); | ||
node.data = o; | ||
|
||
tail.next = node; | ||
size++; | ||
return; | ||
} | ||
|
||
/** | ||
* 在指定位置加入一个元素 | ||
* @param index 指定的位置,应该满足 index > 0 && index <= size() 为 true。 | ||
*/ | ||
public void add(int index , Object o){ | ||
if(index < 0 || index > size) | ||
throw new IndexOutOfBoundsException("Index:" + index); | ||
|
||
Node pos = head; | ||
for(int i = 0; i < index; i++){ | ||
//要在index位置添加一个元素,只要获取到 index - 1 位置的元素即可 | ||
pos = pos.next; | ||
} | ||
Node node = new Node(); | ||
node.data = o; | ||
node.next = pos.next; | ||
pos.next = node; | ||
size++; | ||
return; | ||
} | ||
|
||
/** | ||
* 获取指定位置处的元素 | ||
* @param index 要获取的元素的位置,应该满足 index > 0 && index < size() 为 true。 | ||
*/ | ||
public Object get(int index){ | ||
checkIndex(index); | ||
Node pos = head; | ||
for(int i = 0; i <= index; i++){ | ||
//结束条件为 <= ,这是因为需要获取到index位置 | ||
pos = pos.next; | ||
} | ||
return pos.data; | ||
} | ||
|
||
/** | ||
* 移除并返回指定位置处的元素 | ||
* @param index 要移除的元素的位置,应该满足 index > 0 && index < size() 为 true。 | ||
*/ | ||
public Object remove(int index){ | ||
checkIndex(index); | ||
Node pos = head; | ||
for(int i = 0; i < index; i++){ | ||
pos = pos.next; | ||
} | ||
Node temp = pos.next; | ||
pos.next = temp.next; | ||
size--; | ||
return temp.data; | ||
} | ||
|
||
/** | ||
* 获取存储的元素的个数 | ||
*/ | ||
public int size(){ | ||
return this.size; | ||
} | ||
|
||
/** | ||
* 在第一个元素的前面出添加一个元素 | ||
* @param o | ||
*/ | ||
public void addFirst(Object o){ | ||
add(0, o); | ||
} | ||
|
||
/** | ||
* 在最后一个元素的后面添加一个元素 | ||
* @param o | ||
*/ | ||
public void addLast(Object o){ | ||
add(o); | ||
} | ||
|
||
/** | ||
* 移除第一个元素 | ||
* @return 被移除的元素 | ||
*/ | ||
public Object removeFirst(){ | ||
return remove(0); | ||
} | ||
|
||
/** | ||
* 移除最后一个元素 | ||
* @return 被移除的元素 | ||
*/ | ||
public Object removeLast(){ | ||
return remove(size - 1); | ||
} | ||
|
||
public Iterator iterator(){ | ||
return null; | ||
} | ||
|
||
|
||
private static final class Node{ | ||
Object data; | ||
Node next; | ||
} | ||
|
||
/** | ||
* 获取最后一个节点 | ||
* @return | ||
*/ | ||
private Node getTail(){ | ||
Node tail = head; | ||
while(tail.next != null){ | ||
tail = tail.next; | ||
} | ||
return tail; | ||
} | ||
/** | ||
* 检查get和remove操作中所输入的元素是否有效 | ||
* @param index | ||
*/ | ||
private void checkIndex(int index) { | ||
if(index < 0 || index >= size) | ||
throw new IndexOutOfBoundsException("Index:" + index); | ||
} | ||
} | ||
|
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,10 @@ | ||
package com.hans; | ||
|
||
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(); | ||
} |
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,22 @@ | ||
package com.hans; | ||
|
||
public class Queue { | ||
|
||
private LinkedList elementData = new LinkedList(); | ||
|
||
public void enQueue(Object o){ | ||
elementData.add(o); | ||
} | ||
|
||
public Object deQueue(){ | ||
return elementData.removeFirst(); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return elementData.size() == 0; | ||
} | ||
|
||
public int size(){ | ||
return elementData.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.hans; | ||
|
||
public class Stack { | ||
private ArrayList elementData = new ArrayList(); | ||
|
||
public void push(Object o){ | ||
elementData.add(o); | ||
} | ||
|
||
public Object pop(){ | ||
if(elementData.size() <= 0){ | ||
return null; | ||
} | ||
return elementData.remove(elementData.size() - 1); | ||
} | ||
|
||
public Object peek(){ | ||
if(elementData.size() <= 0){ | ||
return null; | ||
} | ||
return elementData.get(elementData.size() - 1); | ||
} | ||
public boolean isEmpty(){ | ||
return elementData.size() == 0; | ||
} | ||
public int size(){ | ||
return elementData.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 @@ | ||
如今计算机已经深入走进我们生活的方方面面,购物、娱乐、餐饮······,可以说我们无时无刻不在利用计算机给我们的 |
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 @@ | ||
/bin/ |
Oops, something went wrong.