-
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 #11 from RalfNick/master
作业的提交内容
- Loading branch information
Showing
8 changed files
with
594 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,136 @@ | ||
package BasicData; | ||
|
||
import java.util.Arrays; | ||
import java.util.NoSuchElementException; | ||
|
||
/** | ||
* 实现基本的数据结构——ArrayList | ||
* | ||
* @author Ralf | ||
* | ||
* @param <T> | ||
*/ | ||
public class MyArrayList<T> implements MyList<T> { | ||
|
||
private static final int DEFAULT_CAPACITY = 10; | ||
private static int msize; | ||
private T[] elements; | ||
|
||
public MyArrayList() { | ||
msize = 0; | ||
ensureCapacity(DEFAULT_CAPACITY); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private void ensureCapacity(int newCapacity) { | ||
// TODO Auto-generated method stub | ||
if (msize > newCapacity) { | ||
return; | ||
} | ||
T[] oldElements = elements; | ||
elements = (T[]) new Object[newCapacity]; | ||
for (int i = 0; i < size(); i++) { | ||
elements[i] = oldElements[i]; | ||
} | ||
|
||
} | ||
public void trimSize(){ | ||
if (msize < elements.length) { | ||
ensureCapacity(msize); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean add(T t) { | ||
// TODO Auto-generated method stub | ||
|
||
if (elements.length == size()) { | ||
ensureCapacity(2 * size() + 1); | ||
} | ||
elements[msize++] = t; | ||
return true; | ||
} | ||
|
||
@Override | ||
public void add(int index, T t) { | ||
|
||
if (msize == elements.length) { | ||
ensureCapacity(2 * msize + 1); | ||
} | ||
for (int i = size(); i >= index; i--) { | ||
elements[i + 1] = elements[i]; | ||
} | ||
elements[index] = t; | ||
msize++; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
// TODO Auto-generated method stub | ||
return msize; | ||
} | ||
|
||
@Override | ||
public T remove(int index) { | ||
// TODO Auto-generated method stub | ||
if (index < 0 || index > size()) { | ||
throw new ArrayIndexOutOfBoundsException(); | ||
} | ||
T old = elements[index]; | ||
for (int i = index; i < msize; i++) { | ||
elements[i] = elements[i + 1]; | ||
} | ||
elements[msize--] = null; | ||
return old; | ||
|
||
} | ||
|
||
@Override | ||
public boolean set(int index, T t) { | ||
// TODO Auto-generated method stub | ||
if (index < 0 || index > size()) { | ||
throw new ArrayIndexOutOfBoundsException(); | ||
} | ||
elements[index] = t; | ||
return true; | ||
} | ||
|
||
@Override | ||
public T get(int index) { | ||
// TODO Auto-generated method stub | ||
if (index < 0 || index > msize) { | ||
throw new ArrayIndexOutOfBoundsException(); | ||
} | ||
return elements[index]; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return Arrays.toString(elements); | ||
} | ||
|
||
public MyIterator<T> iterator() { | ||
return new MyArrayListIterator(); | ||
} | ||
|
||
private class MyArrayListIterator implements MyIterator<T> { | ||
|
||
private int current = 0;// 迭代器的指针 | ||
|
||
public boolean hasNext() { | ||
// TODO Auto-generated method stub | ||
|
||
return current < size(); | ||
} | ||
|
||
public T Next() { | ||
// TODO Auto-generated method stub | ||
if (!hasNext()) { | ||
throw new NoSuchElementException(); | ||
} | ||
return elements[current++]; | ||
} | ||
|
||
} | ||
|
||
} |
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 BasicData; | ||
|
||
public interface MyIterator<T> { | ||
|
||
public abstract boolean hasNext(); | ||
public abstract T 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,16 @@ | ||
package BasicData; | ||
|
||
/** | ||
* ´´½¨×Ô¼ºµÄList½Ó¿Ú | ||
* @author Ralf | ||
* | ||
*/ | ||
public interface MyList<T> { | ||
|
||
public abstract boolean add(T t); | ||
public abstract void add(int index, T t); | ||
public abstract int size(); | ||
public abstract T remove(int index); | ||
public abstract boolean set(int index, T t); | ||
public abstract T get(int 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,79 @@ | ||
package BasicData; | ||
|
||
/** | ||
* ʵÏÖ»ù±¾Êý¾Ý½á¹¹Queue(»·ÐÎ) | ||
* | ||
* @author Administrator | ||
* | ||
*/ | ||
public class MyQueue<T> { | ||
|
||
private int head; | ||
private int tail; | ||
private T[] elements; | ||
private static final int DEFAUL_SIZE = 10; | ||
private int numOfelements; | ||
|
||
public MyQueue() { | ||
head = 0; | ||
tail = 0; | ||
numOfelements = 0; | ||
setCapacity(DEFAUL_SIZE); | ||
} | ||
|
||
public MyQueue(int capacity) { | ||
head = 0; | ||
tail = 0; | ||
numOfelements = 0; | ||
setCapacity(capacity); | ||
|
||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private void setCapacity(int capacity) { | ||
elements = (T[]) new Object[capacity]; | ||
} | ||
|
||
public boolean enQueue(T t) { | ||
|
||
if (numOfelements == elements.length) { | ||
return false; | ||
} else { | ||
elements[tail] = t; | ||
numOfelements++; | ||
if (tail == elements.length) | ||
tail = 0; | ||
else | ||
tail++; | ||
return true; | ||
} | ||
|
||
} | ||
|
||
public T deQueue() { | ||
if (head == tail) { | ||
return null; | ||
} else { | ||
T t = elements[head]; | ||
numOfelements--; | ||
elements[head] = null; | ||
if (head == elements.length) | ||
head = 0; | ||
else { | ||
head++; | ||
} | ||
return t; | ||
} | ||
|
||
} | ||
|
||
public boolean isEmpty() { | ||
return numOfelements == 0; | ||
} | ||
|
||
public int size() { | ||
int msize = head - tail; | ||
return msize > 0 ? msize : -msize; | ||
} | ||
|
||
} |
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,44 @@ | ||
package BasicData; | ||
|
||
import java.util.LinkedList; | ||
import java.util.NoSuchElementException; | ||
|
||
/** | ||
* ʵÏÖ»ù±¾Êý¾Ý½á¹¹Õ» | ||
* | ||
* @author Ralf | ||
* | ||
*/ | ||
public class MyStack<T> { | ||
|
||
private LinkedList<T> linkedList; | ||
|
||
public MyStack() { | ||
if (null == linkedList) { | ||
linkedList = new LinkedList<T>(); | ||
} | ||
} | ||
|
||
public void push(T t) { | ||
linkedList.addFirst(t); | ||
} | ||
|
||
public T pop() { | ||
if (size() == 0) { | ||
throw new NoSuchElementException(); | ||
} | ||
return linkedList.removeFirst(); | ||
} | ||
|
||
public T peek() { | ||
return (size() == 0) ? null : linkedList.getFirst(); | ||
} | ||
|
||
public int size() { | ||
return linkedList.size(); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return linkedList.isEmpty(); | ||
} | ||
} |
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,99 @@ | ||
package BasicData; | ||
|
||
//insert ·½·¨ÓÐÎÊÌâ | ||
public class MyTreeNode<T extends Comparable<T>> { | ||
|
||
private T data; | ||
private MyTreeNode<T> left = null; | ||
private MyTreeNode<T> right = null; | ||
private MyTreeNode<T> root = null; | ||
private MyTreeNode<T> cureeTreeNode = null; | ||
|
||
public T getData() { | ||
return data; | ||
} | ||
public void setData(T data) { | ||
this.data = data; | ||
} | ||
public MyTreeNode<T> getLeft() { | ||
return left; | ||
} | ||
public void setLeft(MyTreeNode<T> left) { | ||
this.left = left; | ||
} | ||
public MyTreeNode<T> getRight() { | ||
return right; | ||
} | ||
public void setRight(MyTreeNode<T> right) { | ||
this.right = right; | ||
} | ||
@Override | ||
public String toString() { | ||
StringBuilder string = new StringBuilder(); | ||
string.append("["); | ||
if (cureeTreeNode == null) { | ||
string.append("]"); | ||
return string.toString(); | ||
} else { | ||
string.append(cureeTreeNode.toString()).append("]"); | ||
return string.toString(); | ||
} | ||
} | ||
|
||
public MyTreeNode<T> insert(T o){ | ||
MyTreeNode<T> newNode = new MyTreeNode<T>(); | ||
MyTreeNode<T> current = null; | ||
newNode.setData(o); | ||
if (root == null) { | ||
root = newNode; | ||
cureeTreeNode = newNode;//²âÊÔ | ||
return newNode; | ||
} | ||
else { | ||
Digui(o, root); | ||
current = cureeTreeNode; | ||
if (current.getData().compareTo(o) == -1) { | ||
current.right = newNode; | ||
} else { | ||
current.left = newNode; | ||
} | ||
cureeTreeNode = newNode; //²âÊÔ | ||
return newNode; | ||
} | ||
|
||
} | ||
|
||
public void Digui(T o,MyTreeNode<T> parentnode){ | ||
cureeTreeNode = parentnode; | ||
if (parentnode.left!= null) { | ||
if (parentnode.getData().compareTo(o) == -1) { | ||
parentnode = parentnode.right; | ||
Digui(o, parentnode); | ||
} | ||
else | ||
return; | ||
} | ||
if (parentnode.right != null) { | ||
if (parentnode.getData().compareTo(o) == 1) { | ||
parentnode = parentnode.left; | ||
Digui(o, parentnode); | ||
} | ||
else | ||
return; | ||
} | ||
} | ||
|
||
public void preOrder(MyTreeNode<T> root) { | ||
visit(root); | ||
if(root.getLeft() != null) { | ||
preOrder(root.getLeft()); | ||
} | ||
if(root.getRight() != null) { | ||
preOrder(root.getRight()); | ||
} | ||
} | ||
|
||
public void visit(MyTreeNode<T> btree) { | ||
System.out.print(btree.getData() + "\t"); | ||
} | ||
} |
Oops, something went wrong.