forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 5
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 #2 from cmj1993/master
homework
- Loading branch information
Showing
10 changed files
with
961 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,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
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/ |
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,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>SimpleDataStructure</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
154 changes: 154 additions & 0 deletions
154
group03/763878069/src/cmj/datastructure/list/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,154 @@ | ||
package cmj.datastructure.list; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
public class ArrayList implements List { | ||
private transient Object[] elementData; | ||
private int size; | ||
|
||
/** | ||
* ArrayList初始化无参数构造函数 | ||
*/ | ||
public ArrayList() { | ||
this(10); | ||
} | ||
|
||
/** | ||
* ArrayList带容量的构造函数 | ||
* | ||
* @param initialCapacity初始化容量 | ||
*/ | ||
public ArrayList(int initialCapacity) { | ||
super(); | ||
if (initialCapacity < 0) | ||
throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity); | ||
// 新建一个数组 | ||
this.elementData = new Object[initialCapacity]; | ||
} | ||
|
||
/** | ||
* 检查数组的容量 | ||
* | ||
* @param neededMinCapacity所需最小的容量 | ||
*/ | ||
public void ensureCapacity(int neededMinCapacity) { | ||
int currCapacity = elementData.length;// 获取当前数据的全部容量 | ||
// 需要扩容的情况 | ||
if (neededMinCapacity > currCapacity) { | ||
int newCapacity = (currCapacity * 3) / 2 + 1;// 计算新的容量 | ||
elementData = Arrays.copyOf(elementData, newCapacity); | ||
} | ||
} | ||
|
||
/** | ||
* 添加数据 | ||
* | ||
* @param o要添加的元素 | ||
* @return 是否添加成功 | ||
*/ | ||
public void add(Object o) { | ||
// 确定ArrayList的容量大小 | ||
ensureCapacity(size + 1); // Increments modCount!! | ||
// 添加o到ArrayList中 | ||
elementData[size++] = o; | ||
} | ||
|
||
/** | ||
* 就是检查一下是不是超出数组界限了,超出了就抛出IndexOutBoundsException异常。 | ||
* | ||
* @param index要用于检查的索引 | ||
*/ | ||
private void RangeCheck(int index) { | ||
if (index > size || index < 0) | ||
throw new IndexOutOfBoundsException("Index: " + index + " 超出访问范围"); | ||
} | ||
|
||
/** | ||
* 向指定的位置添加元素 | ||
* | ||
* @param index | ||
* @param o | ||
*/ | ||
public void add(int index, Object o) { | ||
RangeCheck(index); | ||
ensureCapacity(size + 1);// 检查容量 | ||
|
||
/* 将原数组从第index个位置复制到原数组第index+1个位置上,一共移动size-index(也就是后面剩下的)个元素 */ | ||
System.arraycopy(elementData, index, elementData, index + 1, size - index); | ||
elementData[index] = o; | ||
size++; | ||
} | ||
|
||
public boolean addAll(Collection<? extends Object> c) { | ||
Object[] a = c.toArray(); | ||
int growthNum = a.length; | ||
ensureCapacity(size + growthNum); // Increments modCount | ||
System.arraycopy(a, 0, elementData, size, growthNum); | ||
size += growthNum; | ||
return growthNum != 0; | ||
} | ||
|
||
public Object get(int index) { | ||
RangeCheck(index); | ||
return elementData[index]; | ||
|
||
} | ||
|
||
public Object remove(int index) { | ||
RangeCheck(index); | ||
int numMoved = size - index - 1;// 删除后需要移动的对象 | ||
Object RemovedValue = elementData[index]; | ||
if (numMoved > 0) | ||
System.arraycopy(elementData, index + 1, elementData, index, numMoved); | ||
elementData[--size] = null; | ||
return RemovedValue; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
String arraylist = "["; | ||
for (int i = 0; i < size; i++) { | ||
if (i == size - 1) { | ||
arraylist += elementData[i].toString() + "]"; | ||
} else { | ||
arraylist += elementData[i].toString() + " ,"; | ||
} | ||
} | ||
return arraylist; | ||
} | ||
|
||
public static void main(String[] args) { | ||
ArrayList arrayList = new ArrayList(5); | ||
arrayList.add(1); | ||
arrayList.add(2); | ||
arrayList.add(3); | ||
arrayList.add(4); | ||
arrayList.add(5); | ||
arrayList.add(6); | ||
System.out.println(arrayList); | ||
arrayList.add(1, 1234); | ||
System.out.println(arrayList); | ||
arrayList.remove(1); | ||
System.out.println(arrayList); | ||
System.out.println(arrayList.get(5)); | ||
|
||
ArrayList stringArraylist = new ArrayList(3); | ||
stringArraylist.add("Hello "); | ||
stringArraylist.add("string "); | ||
stringArraylist.add("arraylist"); | ||
System.out.println(stringArraylist); | ||
|
||
ArrayList mixArraylist = new ArrayList(5); | ||
mixArraylist.add("String"); | ||
mixArraylist.add(1); | ||
mixArraylist.add('f'); | ||
mixArraylist.add(3.1f); | ||
mixArraylist.add(4L); | ||
System.out.println(mixArraylist); | ||
} | ||
} |
208 changes: 208 additions & 0 deletions
208
group03/763878069/src/cmj/datastructure/list/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,208 @@ | ||
package cmj.datastructure.list; | ||
|
||
public class LinkedList implements List { | ||
|
||
private Node head;// 头结点 | ||
private Node current;// 尾结点 | ||
private int size; | ||
|
||
public LinkedList() { | ||
// 头指针和尾指针都指向头结点 | ||
head = new Node(null, null); | ||
current = head; | ||
} | ||
|
||
/** | ||
* 添加元素 | ||
* | ||
* @param o——用于添加的元素 | ||
*/ | ||
public void add(Object o) { | ||
Node node = new Node(o, null);// 新建一个结点 | ||
current.next = node;// 尾指针指向它 | ||
current = current.next;// 尾指针指向最后一个元素 | ||
size++; | ||
} | ||
|
||
/** | ||
* 在第index个位置插入元素 | ||
* | ||
* @param index——要插入的位置 | ||
* @param o——用于插入的对象 | ||
*/ | ||
public void add(int index, Object o) { | ||
Node node = new Node(o, null);// 新建一个结点 | ||
if (index == 0) { | ||
addFirst(o); | ||
} else { | ||
Node curr = (Node) this.get(index - 1);// 获得前一个结点 | ||
Node behind = (Node) this.get(index);// 获得后一个结点 | ||
// 在这两个结点之间插入新的元素,修改引用指向 | ||
curr.next = node; | ||
node.next = behind; | ||
size++; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* 随机访问index位置上的元素 | ||
* | ||
* @param index——元素的位置 | ||
* @return——对应的元素 | ||
*/ | ||
public Object get(int index) { | ||
RangeCheck(index);// 检查索引是否越界 | ||
Node curr = head;// 得到头结点的引用 | ||
// 从头结点开始遍历到第index个元素 | ||
for (int i = 0; i <= index; i++) | ||
curr = curr.next; | ||
return curr; | ||
} | ||
|
||
/** | ||
* 删除第index个位置上的元素 | ||
* | ||
* @param index | ||
* @return | ||
*/ | ||
public Object remove(int index) { | ||
RangeCheck(index);// 检查索引是否越界 | ||
if (0 == index) { | ||
return removeFirst(); | ||
} else { | ||
Node toRemove = (Node) this.get(index);// 获得要删除的结点 | ||
Node preRemove = (Node) this.get(index - 1);// 获得前一个结点 | ||
preRemove.next = toRemove.next;// 将前一个结点指向要删除的结点的下一个结点 | ||
size--; | ||
return toRemove; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* 获取元素的大小 | ||
* | ||
* @return | ||
*/ | ||
public int size() { | ||
return size; | ||
} | ||
|
||
/** | ||
* 在链表头部增加元素 | ||
* | ||
* @param o——要增加的元素 | ||
*/ | ||
public void addFirst(Object o) { | ||
Node node = new Node(o, null);// 新建一个结点 | ||
node.next = head.next;// 结点指向第一个元素 | ||
head.next = node;// 将头结点指向它 | ||
size++; | ||
} | ||
|
||
/** | ||
* 在链表末尾添加元素 | ||
* | ||
* @param o——要添加的元素 | ||
*/ | ||
public void addLast(Object o) { | ||
Node node = new Node(o, null);// 新建一个结点 | ||
current.next.next = node;// 尾结点的next指向新建的结点 | ||
current.next = node;// 尾结点引用指向向新结点 | ||
size++; | ||
} | ||
|
||
/** | ||
* 移除第一个元素 | ||
* | ||
* @return——移除元素 | ||
*/ | ||
public Object removeFirst() { | ||
Node curr = head.next;// 新建一个引用记录第一个结点 | ||
head.next = curr.next;// 头指针移动到原第二个元素上 | ||
size--; | ||
return curr; | ||
} | ||
|
||
/** | ||
* 移除最后一个元素 | ||
* | ||
* @return——移除元素 | ||
*/ | ||
public Object removeLast() { | ||
Node remove = current.next; | ||
Node pre = (Node) this.get(size - 2);// 获得倒数第二个结点 | ||
current.next = pre; | ||
pre.next = null; | ||
size--; | ||
return remove; | ||
} | ||
|
||
/** | ||
* 就是检查一下是不是超出数组界限了,超出了就抛出IndexOutBoundsException异常。 | ||
* | ||
* @param index要用于检查的索引 | ||
*/ | ||
private void RangeCheck(int index) { | ||
if (index > size || index < 0) | ||
throw new IndexOutOfBoundsException("Index: " + index + " 超出访问范围"); | ||
} | ||
|
||
/** | ||
* 重写toString()方法 | ||
*/ | ||
@Override | ||
public String toString() { | ||
String linkedlist = "["; | ||
Node visit = head; | ||
while (visit.next != null) { | ||
visit = visit.next; | ||
if (visit.next == null) { | ||
linkedlist += visit.data.toString() + "]"; | ||
} else { | ||
linkedlist += visit.data.toString() + "--->"; | ||
} | ||
} | ||
return linkedlist; | ||
} | ||
|
||
/** | ||
* 结点内部类,主要要声明为static的 | ||
* | ||
* @author think | ||
* | ||
*/ | ||
private static class Node { | ||
Object data; | ||
Node next; | ||
|
||
public Node(Object data, Node next) { | ||
this.data = data; | ||
this.next = next; | ||
} | ||
|
||
} | ||
|
||
public static void main(String[] args) { | ||
LinkedList list = new LinkedList(); | ||
list.add("a"); | ||
list.add("b"); | ||
list.add("c"); | ||
list.add("d"); | ||
System.out.println(list); | ||
System.out.println(((Node) list.get(3)).data); | ||
list.add(4, "4"); | ||
System.out.println(list); | ||
list.add(0, "0"); | ||
System.out.println(list); | ||
list.addLast("last"); | ||
System.out.println(list); | ||
System.out.println("Removed:" + ((Node) list.remove(1)).data); | ||
System.out.println(list); | ||
System.out.println("Removed:" + ((Node) list.removeFirst()).data); | ||
System.out.println(list); | ||
System.out.println("Removed:" + ((Node) list.removeLast()).data); | ||
System.out.println(list); | ||
} | ||
} |
Oops, something went wrong.