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 BigJoyce/master
作业1-数据结构及文章
- Loading branch information
Showing
17 changed files
with
659 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
group14/1091149131/20170226_作业1_数据结构及文章/basicstructuredemo/.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,7 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
1 change: 1 addition & 0 deletions
1
group14/1091149131/20170226_作业1_数据结构及文章/basicstructuredemo/.gitignore
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/ |
17 changes: 17 additions & 0 deletions
17
group14/1091149131/20170226_作业1_数据结构及文章/basicstructuredemo/.project
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>basicstructuredemo</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> |
11 changes: 11 additions & 0 deletions
11
...4/1091149131/20170226_作业1_数据结构及文章/basicstructuredemo/.settings/org.eclipse.jdt.core.prefs
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,11 @@ | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 | ||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
org.eclipse.jdt.core.compiler.compliance=1.8 | ||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate | ||
org.eclipse.jdt.core.compiler.debug.localVariable=generate | ||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.source=1.8 |
66 changes: 66 additions & 0 deletions
66
...p14/1091149131/20170226_作业1_数据结构及文章/basicstructuredemo/src/com/maple/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,66 @@ | ||
package com.maple.basic; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
public void add(Object o){ | ||
//不够了怎么扩容 | ||
elementData[size++]=o; | ||
} | ||
public void add(int index, Object o){ | ||
if(index<0||index>size){ | ||
throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size); | ||
} | ||
for(int i=size;i>index;i--){ | ||
elementData[i-1]=elementData[i]; | ||
} | ||
elementData[index]=o; | ||
size++; | ||
} | ||
|
||
public Object get(int index){ | ||
if(index<0||index>=size){ | ||
throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size); | ||
} | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index){ | ||
if(index<0||index>=size){ | ||
throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size); | ||
} | ||
Object removeObj=elementData[index]; | ||
for(int i=index;i<size-1;i++){ | ||
elementData[i]=elementData[i+1]; | ||
} | ||
size--; | ||
return removeObj; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return new Iterator() { | ||
int ref=0; | ||
@Override | ||
public Object next() { | ||
if(hasNext()){ | ||
return elementData[ref++]; | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if(ref<0||ref>=size) return false; | ||
return true; | ||
} | ||
}; | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
...14/1091149131/20170226_作业1_数据结构及文章/basicstructuredemo/src/com/maple/basic/BinaryTree.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,45 @@ | ||
package com.maple.basic; | ||
|
||
import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateName; | ||
|
||
public class BinaryTree<T extends Comparable<T>> { | ||
private BinaryTreeNode<T> root; | ||
|
||
public void traversal(BinaryTreeNode<T> node){ | ||
if(node.getLeft()!=null){ | ||
traversal(node.getLeft()); | ||
} | ||
System.out.println("--"+node.getData()+"--"); | ||
if(node.getRight()!=null){ | ||
traversal(node.getRight()); | ||
} | ||
} | ||
/** | ||
* 如果根节点为null,则作为根节点,否则遍历下去插值 | ||
* @param o | ||
* @return | ||
* 2017年2月23日 下午4:21:51 | ||
* @Author Joy | ||
*/ | ||
public BinaryTreeNode insert(T o){ | ||
if(root==null){ | ||
BinaryTreeNode<T> newB=new BinaryTreeNode<T>(); | ||
newB.setData(o); | ||
newB.setLeft(null); | ||
newB.setRight(null); | ||
root=newB; | ||
return root; | ||
} | ||
|
||
return root.insert(o); | ||
} | ||
public BinaryTreeNode<T> getRoot() { | ||
return root; | ||
} | ||
|
||
public void setRoot(BinaryTreeNode<T> root) { | ||
this.root = root; | ||
} | ||
|
||
|
||
} |
69 changes: 69 additions & 0 deletions
69
...091149131/20170226_作业1_数据结构及文章/basicstructuredemo/src/com/maple/basic/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,69 @@ | ||
package com.maple.basic; | ||
|
||
import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateName; | ||
|
||
public class BinaryTreeNode<T extends Comparable<T>>{ | ||
|
||
private T data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
/** | ||
* 如果待插入的值等于节点的值,则抛出异常:duplicate value | ||
* 如果小于节点的值,则往左边遍历 | ||
* 如果大于节点的值,则往右边遍历 | ||
* @param o | ||
* @return | ||
* 2017年2月23日 下午4:22:50 | ||
* @Author Joy | ||
*/ | ||
public BinaryTreeNode insert(T o){ | ||
//assume that no duplicate key | ||
|
||
if(o.compareTo(data)==0){ | ||
try { | ||
throw new DuplicateName("duplicate value: "+o); | ||
} catch (DuplicateName e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
BinaryTreeNode<T> newB=new BinaryTreeNode<T>(); | ||
newB.setData(o); | ||
newB.setLeft(null); | ||
newB.setRight(null); | ||
//o更大,在右边 | ||
if(o.compareTo(data)>0){ | ||
if(this.getRight()!=null){ | ||
this.getRight().insert(o); | ||
}else{ | ||
this.setRight(newB); | ||
} | ||
}else if(o.compareTo(data)<0){ | ||
if(this.getLeft()!=null){ | ||
this.getLeft().insert(o); | ||
}else{ | ||
this.setLeft(newB); | ||
} | ||
} | ||
return newB; | ||
} | ||
|
||
public T getData() { | ||
return data; | ||
} | ||
public void setData(T 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; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
group14/1091149131/20170226_作业1_数据结构及文章/basicstructuredemo/src/com/maple/basic/Iterator.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,7 @@ | ||
package com.maple.basic; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
154 changes: 154 additions & 0 deletions
154
...14/1091149131/20170226_作业1_数据结构及文章/basicstructuredemo/src/com/maple/basic/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,154 @@ | ||
package com.maple.basic; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
public class LinkedList implements List { | ||
|
||
private Node head; | ||
private int size = 0;//自己加的,觉得需要 | ||
/** | ||
* 与addList()是一样的 | ||
*/ | ||
public void add(Object o){ | ||
addLast(o); | ||
} | ||
public void add(int index , Object o){ | ||
if(index<0||index>size){ | ||
throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size); | ||
} | ||
Node prevNode=head; | ||
Node curNode=head.next; | ||
int count=0; | ||
while(count<=index){ | ||
if(count==index){ | ||
Node newNode=new Node(); | ||
newNode.data=o; | ||
|
||
newNode.next=curNode; | ||
prevNode.next=newNode; | ||
size++; | ||
break; | ||
} | ||
curNode=curNode.next; | ||
prevNode=prevNode.next; | ||
count++; | ||
} | ||
|
||
|
||
} | ||
public Object get(int index){ | ||
if(index<0||index>=size) | ||
throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size); | ||
|
||
Node curNode=head.next; | ||
int count=0; | ||
while(count<=index){ | ||
if(count==index){ | ||
return curNode.data; | ||
} | ||
curNode=curNode.next; | ||
count++; | ||
} | ||
return null; | ||
} | ||
public Object remove(int index){ | ||
if(index<0||index>=size) | ||
throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size); | ||
Node prevNode=head; | ||
Node curNode=head.next; | ||
int count=0; | ||
while(count<=index){ | ||
if(count==index){ | ||
prevNode.next=curNode.next; | ||
size--; | ||
return curNode.data; | ||
} | ||
curNode=curNode.next; | ||
prevNode=prevNode.next; | ||
count++; | ||
} | ||
return null; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public void addFirst(Object o){ | ||
Node objNode=new Node(); | ||
objNode.data=o; | ||
if(head==null) head=new Node(); | ||
objNode.next=head.next; | ||
size++; | ||
head.next=objNode; | ||
} | ||
public void addLast(Object o){ | ||
Node objNode=new Node(); | ||
objNode.data=o; | ||
if(head==null) head=new Node(); | ||
|
||
//也可以用iterator迭代,先不用吧 | ||
Node curNode=head; | ||
while(curNode.next!=null){ | ||
curNode=curNode.next; | ||
} | ||
objNode.next=curNode.next; | ||
curNode.next=objNode; | ||
size++; | ||
|
||
} | ||
public Object removeFirst(){ | ||
if(head==null||head.next==null) | ||
throw new NoSuchElementException(); | ||
Node delNode=head.next; | ||
head.next=delNode.next; | ||
size--; | ||
return delNode.data; | ||
} | ||
public Object removeLast(){ | ||
if(head==null||head.next==null) | ||
throw new NoSuchElementException(); | ||
Node prevNode=head; | ||
Node curNode=head.next; | ||
while(curNode!=null){ | ||
if(curNode.next==null){//说明是尾节点 | ||
prevNode.next=curNode.next; | ||
size--; | ||
return curNode.data; | ||
} | ||
curNode=curNode.next; | ||
prevNode=prevNode.next; | ||
} | ||
return null; | ||
} | ||
public Iterator iterator(){ | ||
return new Iterator() { | ||
private Node cur=head!=null?head.next:head; | ||
@Override | ||
public Object next() { | ||
if(cur==null){ | ||
throw new NoSuchElementException(); | ||
} | ||
Object object=cur.data; | ||
cur=cur.next; | ||
return object; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if(cur==null){ | ||
return false; | ||
}else{ | ||
return true; | ||
} | ||
|
||
} | ||
}; | ||
} | ||
|
||
|
||
private static class Node{ | ||
Object data; | ||
Node next; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
group14/1091149131/20170226_作业1_数据结构及文章/basicstructuredemo/src/com/maple/basic/List.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,9 @@ | ||
package com.maple.basic; | ||
|
||
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(); | ||
} |
Oops, something went wrong.