forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 13
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 #10 from gogoloda/master
Submit my first homework to group owner
- Loading branch information
Showing
12 changed files
with
483 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,7 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> | ||
<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>564451732</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> |
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,114 @@ | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.junit.Test; | ||
|
||
import hw1.ArrayListImpl; | ||
import hw1.BinaryTreeNode; | ||
import hw1.LinkedList; | ||
import hw1.QueueImpl; | ||
import hw1.StackImpl; | ||
|
||
public class DataStructureTest { | ||
|
||
@Test | ||
public void ArrayListTest() { | ||
ArrayListImpl al = new ArrayListImpl(); | ||
Object o1 = new Object(); | ||
Object o2 = new Object(); | ||
Object o3 = new Object(); | ||
al.add(new Object()); | ||
al.add(new Object()); | ||
assertTrue(al.size() == 2); | ||
al.add(5); | ||
assertFalse(al.size() == 2); | ||
assertTrue(al.size() == 3); | ||
al.add(2, 3); | ||
//System.out.println((int)al.get(2)); | ||
assertTrue((int)al.get(2) == 3); | ||
assertTrue((int)al.get(3) == 5); | ||
} | ||
|
||
@Test | ||
public void LinkedListTest() { | ||
LinkedList ll = new LinkedList(); | ||
Object o1 = new Object(); | ||
Object o2 = new Object(); | ||
Object o3 = new Object(); | ||
Object o4 = new Object(); | ||
Object o5 = new Object(); | ||
Object o6 = new Object(); | ||
ll.add(o1); | ||
ll.add(o2); | ||
//System.out.println(ll.size()); | ||
assertTrue(ll.size() == 2); | ||
ll.add(o3); | ||
ll.add(1,1); | ||
assertTrue((int)ll.get(1) == 1); | ||
assertTrue(ll.size() == 4); | ||
ll.addFirst(2); | ||
ll.addLast(4); | ||
assertTrue((int)ll.get(0) == 2); | ||
assertTrue((int)ll.get(ll.size() - 1) == 4); | ||
assertTrue((int)ll.get(2) == 1); | ||
ll.remove(2); | ||
assertTrue(ll.get(2) == o2); | ||
//System.out.print((int)ll.remove(2)); | ||
//System.out.println((int)ll.get(2)); | ||
//assertFalse((int)ll.get(2) == 1); | ||
assertTrue(ll.size() == 5); | ||
assertTrue((int)ll.removeFirst() == 2); | ||
assertTrue(ll.size() == 4); | ||
assertTrue((int)ll.removeLast() == 4); | ||
assertTrue(ll.size() == 3); | ||
} | ||
|
||
@Test | ||
public void QueueTest(){ | ||
QueueImpl qi = new QueueImpl(); | ||
assertTrue(qi.isEmpty()); | ||
qi.enQueue(1); | ||
qi.enQueue(2); | ||
qi.enQueue(3); | ||
assertTrue(qi.size() == 3); | ||
assertTrue((int)qi.deQueue() == 1); | ||
assertTrue(qi.size() == 2); | ||
assertFalse(qi.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void StackTest() { | ||
StackImpl stack = new StackImpl(); | ||
assertTrue(stack.isEmpty()); | ||
stack.push(1); | ||
stack.push(2); | ||
stack.push(3); | ||
assertTrue(stack.size() == 3); | ||
assertTrue((int)stack.pop() == 3); | ||
assertTrue(stack.size() == 2); | ||
assertTrue((int)stack.peek() == 2); | ||
assertFalse(stack.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void BinaryTreeTest() { | ||
BinaryTreeNode bt = new BinaryTreeNode(); | ||
Integer i1 = 1; | ||
Integer i2 = 3; | ||
Integer i3 = 4; | ||
Integer i4 = 6; | ||
Integer i5 = -1; | ||
bt.insert(i3); | ||
bt.insert(i1); | ||
bt.insert(i4); | ||
bt.insert(i2); | ||
bt.insert(i5); | ||
assertTrue((int)bt.getRoot().getData() == 4); | ||
assertTrue((int)bt.getRoot().getLeft().getData() == 1); | ||
assertTrue((int)bt.getRoot().getRight().getData() == 6); | ||
assertTrue((int)bt.getRoot().getLeft().getLeft().getData() == -1); | ||
assertTrue((int)bt.getRoot().getLeft().getRight().getData() == 3); | ||
|
||
} | ||
|
||
} |
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 @@ | ||
|
||
public class Test { | ||
|
||
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,88 @@ | ||
package hw1; | ||
|
||
//import java.util.ArrayList; | ||
import java.util.Iterator; | ||
//import java.util.List; | ||
|
||
import com.sun.xml.internal.ws.api.pipe.ThrowableContainerPropertySet; | ||
|
||
public class ArrayListImpl implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
public void add(Object o){ | ||
//size++; | ||
try{ | ||
if (size < elementData.length) { | ||
elementData[size] = o; | ||
} else { | ||
elementData = grow(elementData, elementData.length); | ||
elementData[size] = o; | ||
} | ||
size++; | ||
} catch (IndexOutOfBoundsException e) { | ||
System.out.println("The added index is out of bound"); | ||
} | ||
} | ||
public void add(int index, Object o){ | ||
if (index < 0) { | ||
throw new IndexOutOfBoundsException("Index cannot be less than 0"); | ||
} | ||
while (index >= elementData.length) { | ||
elementData = grow(elementData, elementData.length); | ||
} | ||
if (index >= size) { | ||
elementData[index] = o; | ||
size++; | ||
} else { | ||
if (size + 1 >= elementData.length) { | ||
elementData = grow(elementData, elementData.length); | ||
} | ||
for (int i = size-1; i >= index;i--) { | ||
elementData[i+1] = elementData[i]; | ||
} | ||
elementData[index] = o; | ||
size++; | ||
|
||
} | ||
} | ||
|
||
public Object get(int index){ | ||
if (index >= size ||index < 0) { | ||
throw new IndexOutOfBoundsException("Index entered is out of bounds"); | ||
} else { | ||
return elementData[index]; | ||
} | ||
|
||
} | ||
|
||
public Object remove(int index){ | ||
if (index >= size ||index < 0) { | ||
throw new IndexOutOfBoundsException("Index entered is out of bounds"); | ||
} else { | ||
Object result = elementData[index]; | ||
for (int i = index; i < size - 1; i++) { | ||
elementData[i] = elementData[i+1]; | ||
} | ||
elementData[size - 1] = 0; | ||
size--; | ||
return result; | ||
} | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return null; | ||
} | ||
|
||
private Object[] grow (Object[] src, int increase) { | ||
Object[] target = new Object[src.length + increase]; | ||
System.arraycopy(src, 0, target, 0, src.length); | ||
return target; | ||
} | ||
} |
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,71 @@ | ||
package hw1; | ||
|
||
public class BinaryTreeNode { | ||
private Comparable data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
private BinaryTreeNode root; | ||
|
||
public BinaryTreeNode (Comparable data) { | ||
this.data = data; | ||
} | ||
public BinaryTreeNode () { | ||
|
||
} | ||
public Object getData() { | ||
return data; | ||
} | ||
public void setData(Comparable 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; | ||
return; | ||
} | ||
|
||
|
||
|
||
public BinaryTreeNode getRoot() { | ||
return root; | ||
} | ||
public void setRoot(BinaryTreeNode root) { | ||
this.root = root; | ||
} | ||
public BinaryTreeNode insert(Comparable o){ | ||
BinaryTreeNode result = new BinaryTreeNode(o); | ||
if (root == null) { | ||
root = result; | ||
} else { | ||
BinaryTreeNode dummy = root; | ||
insertHelper(o,dummy); | ||
|
||
} | ||
return result; | ||
} | ||
|
||
private void insertHelper (Comparable o, BinaryTreeNode root) { | ||
if (o.compareTo(root.data) < 0) { | ||
if (root.left == null) { | ||
root.left = new BinaryTreeNode(o); | ||
} else { | ||
insertHelper (o, root.left); | ||
} | ||
} else { | ||
if (root.right == null) { | ||
root.right = new BinaryTreeNode(o); | ||
|
||
} else { | ||
insertHelper (o, root.right); | ||
} | ||
} | ||
} | ||
} |
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 hw1; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
Oops, something went wrong.