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 wa122as/master
基本数据结构作业
- Loading branch information
Showing
8 changed files
with
850 additions
and
0 deletions.
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
group15/1521_653895972/src/com/coding/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,150 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.Arrays; | ||
import java.util.NoSuchElementException; | ||
|
||
/** | ||
* Created by wanc on 2017/2/21. | ||
* 实现ArrayList | ||
*/ | ||
public class ArrayList implements List { | ||
/** | ||
* 实例化空数组 不用每次都new | ||
*/ | ||
private static final Object[] Empty_elementData = {}; | ||
/** | ||
* 计数 | ||
*/ | ||
private int size = 0; | ||
/** | ||
* 数据存放 | ||
*/ | ||
private Object[] elementData = new Object[100]; | ||
|
||
public ArrayList() { | ||
this.elementData = Empty_elementData; | ||
} | ||
|
||
/** | ||
* 检查是否越界 | ||
*/ | ||
private void checkLenght(int index) { | ||
if (index - size > 0) | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
|
||
/** | ||
* 增加数组容量 | ||
*/ | ||
private void kuorong() { | ||
elementData = Arrays.copyOf(elementData, size + 1); | ||
} | ||
|
||
/** | ||
* 添加数据 | ||
* | ||
* @param o | ||
*/ | ||
public void add(Object o) { | ||
//扩容 | ||
kuorong(); | ||
//添加数据 | ||
elementData[size++] = o; | ||
} | ||
|
||
/** | ||
* 在指定索引添加数据 | ||
* | ||
* @param index | ||
* @param o | ||
*/ | ||
public void add(int index, Object o) { | ||
//扩容 | ||
kuorong(); | ||
//移动数据 | ||
System.arraycopy(elementData, index, elementData, index + 1, size - index); | ||
//添加数据 | ||
elementData[index] = o; | ||
size++; | ||
} | ||
|
||
/** | ||
* 获取指定索引数据 | ||
* | ||
* @param index | ||
* @return | ||
*/ | ||
public Object get(int index) { | ||
//检查是否越界 | ||
checkLenght(index); | ||
return elementData[index]; | ||
} | ||
|
||
/** | ||
* 移除指定索引数据 | ||
* | ||
* @param index | ||
* @return | ||
*/ | ||
public Object remove(int index) { | ||
//检查是否越界 | ||
checkLenght(index); | ||
Object element = elementData[index]; | ||
//计算移除该元素后,要前移的个数 | ||
int movesize = size - index - 1; | ||
//移动数据 | ||
System.arraycopy(elementData, index + 1, elementData, index, movesize); | ||
//删除末尾元素 | ||
elementData[--size] = null; | ||
return element; | ||
} | ||
|
||
/** | ||
* 返回数量 | ||
* | ||
* @return | ||
*/ | ||
public int size() { | ||
return size; | ||
} | ||
|
||
/** | ||
* 获取迭代器 | ||
* | ||
* @return | ||
*/ | ||
public Iterator iterator() { | ||
return new ArrayItr(); | ||
} | ||
|
||
//迭代器实现类部类 | ||
private class ArrayItr implements Iterator { | ||
int cursor;//游标 | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return cursor != size; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
int i = cursor; | ||
if (i > size) throw new NoSuchElementException(); | ||
Object[] newElementData = ArrayList.this.elementData; | ||
if (i > newElementData.length) throw new IndexOutOfBoundsException(); | ||
cursor = i + 1; | ||
return newElementData[i]; | ||
} | ||
} | ||
|
||
/** | ||
* 重写toString 方便打印 | ||
* | ||
* @return | ||
*/ | ||
@Override | ||
public String toString() { | ||
Object[] s = Arrays.copyOf(elementData, size); | ||
return Arrays.toString(s); | ||
} | ||
} |
184 changes: 184 additions & 0 deletions
184
group15/1521_653895972/src/com/coding/basic/BasicTest.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,184 @@ | ||
package com.coding.basic; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Created by wanc on 2017/2/21. | ||
*/ | ||
public class BasicTest { | ||
|
||
@Test | ||
public void test() { | ||
//测试 | ||
testArrayList(); | ||
testLinkedList(); | ||
testBinaryTreeNode(); | ||
testStack(); | ||
testQueue(); | ||
} | ||
|
||
|
||
public void testQueue(){ | ||
Queue queue = new Queue(); | ||
queue.enQueue("S"); | ||
queue.enQueue("Y"); | ||
queue.enQueue(5); | ||
System.out.println(queue); | ||
System.out.println("queue.size()="+queue.size()); | ||
System.out.println("queue.deQueue()="+queue.deQueue()); | ||
System.out.println(queue); | ||
System.out.println("queue.isEmpty()="+queue.isEmpty()); | ||
System.out.println(queue); | ||
} | ||
public void testStack(){ | ||
Stack stack = new Stack(); | ||
stack.push("S"); | ||
stack.push("Y"); | ||
stack.push(5); | ||
System.out.println("stack.size()="+stack.size()); | ||
System.out.println("stack.peek()="+stack.peek()); | ||
System.out.println(stack); | ||
System.out.println("stack.isEmpty()="+stack.isEmpty()); | ||
stack.pop(); | ||
System.out.println(stack); | ||
} | ||
public void testBinaryTreeNode(){ | ||
System.out.println("-------------------BinaryTreeNode 测试开始-------------------"); | ||
System.out.println("new 一个实例"); | ||
BinaryTreeNode root = new BinaryTreeNode(); | ||
root.insert(5); | ||
root.insert(6); | ||
root.insert(9); | ||
root.insert(3); | ||
root.insert(3); | ||
root.insert(2); | ||
root.insert(10); | ||
System.out.println(root); | ||
System.out.println("-------------------LinkedList 测试结束-------------------"); | ||
} | ||
public void testLinkedList() { | ||
System.out.println("-------------------LinkedList 测试开始-------------------"); | ||
|
||
System.out.println("new 一个实例"); | ||
LinkedList list = new LinkedList(); | ||
|
||
System.out.println("添加元素----A"); | ||
list.add("A"); | ||
Assert.assertEquals(list.get(list.size()-1),"A"); | ||
System.out.println("结果:"+list); | ||
|
||
System.out.println(); | ||
System.out.println("添加元素----B"); | ||
list.add("B"); | ||
Assert.assertEquals(list.get(list.size()-1),"B"); | ||
System.out.println("结果:"+list); | ||
|
||
System.out.println(); | ||
System.out.println("添加元素----3"); | ||
list.add(3); | ||
Assert.assertEquals(list.get(list.size()-1),3); | ||
System.out.println("结果:"+list); | ||
|
||
System.out.println(); | ||
System.out.println("在下标1插入元素----3"); | ||
list.add(1, 3); | ||
Assert.assertEquals(list.get(1),3); | ||
System.out.println("结果:"+list); | ||
|
||
System.out.println(); | ||
System.out.println("在下标3插入元素----6"); | ||
list.add(3, 6); | ||
Assert.assertEquals(list.get(3),6); | ||
System.out.println("结果:"+list); | ||
|
||
System.out.println(); | ||
System.out.println("删除下标0元素"); | ||
list.remove(0); | ||
System.out.println("结果:"+list); | ||
|
||
System.out.println(); | ||
System.out.println("获取size"); | ||
System.out.println("结果:"+list.size()); | ||
|
||
System.out.println(); | ||
System.out.println("在首位前插入F"); | ||
list.addFirst("F"); | ||
Assert.assertEquals(list.get(0),"F"); | ||
System.out.println("结果:"+list); | ||
|
||
System.out.println(); | ||
System.out.println("在末位前插入K"); | ||
list.addLast("K"); | ||
Assert.assertEquals(list.get(list.size()-1),"K"); | ||
System.out.println("结果:"+list); | ||
|
||
System.out.println(); | ||
System.out.println("删除首位"); | ||
list.removeFirst(); | ||
System.out.println("结果:"+list); | ||
|
||
System.out.println(); | ||
System.out.println("删除末尾"); | ||
list.removeLast(); | ||
System.out.println("结果:"+list); | ||
|
||
System.out.println(); | ||
System.out.println("迭代器输出:"); | ||
Iterator i = list.iterator(); | ||
while (i.hasNext()){ | ||
System.out.print(i.next()+" "); | ||
} | ||
System.out.println("-------------------LinkedList 测试结束-------------------"); | ||
} | ||
|
||
/** | ||
* 测试 ArrayList | ||
*/ | ||
public void testArrayList() { | ||
System.out.println("-------------------ArrayList 测试开始-------------------"); | ||
|
||
System.out.println("new 一个实例"); | ||
ArrayList list = new ArrayList(); | ||
|
||
System.out.println("添加元素 A"); | ||
list.add("A"); | ||
Assert.assertEquals(list.get(list.size()-1),"A"); | ||
|
||
System.out.println("添加元素 B"); | ||
list.add("B"); | ||
Assert.assertEquals(list.get(list.size()-1),"B"); | ||
|
||
System.out.println("添加元素 3"); | ||
list.add(3); | ||
Assert.assertEquals(list.get(list.size()-1),3); | ||
System.out.println("输出:"+list); | ||
|
||
System.out.println("添加元素 3 到索引 1"); | ||
list.add(1, 3); | ||
Assert.assertEquals(list.get(1),3); | ||
System.out.println("输出:"+list); | ||
|
||
System.out.println("添加元素 6 到索引 3"); | ||
list.add(3, 6); | ||
Assert.assertEquals(list.get(3),6); | ||
System.out.println("输出:"+list); | ||
|
||
System.out.println("移除 索引 4 元素"); | ||
Object rm = list.remove(4); | ||
System.out.println("输出:"+list); | ||
|
||
System.out.println("获取 索引 4 元素"); | ||
Object get = list.get(4); | ||
Assert.assertNotEquals(rm,get); | ||
|
||
System.out.println("输出:"+list); | ||
System.out.println("数量:"+list.size()); | ||
Iterator i = list.iterator(); | ||
System.out.print("迭代器输出:"); | ||
while (i.hasNext()){ | ||
System.out.print(i.next()+" "); | ||
} | ||
System.out.println("-------------------ArrayList 测试结束-------------------"); | ||
} | ||
} |
Oops, something went wrong.