Skip to content

Commit

Permalink
作业1-数据结构及文章
Browse files Browse the repository at this point in the history
20170226需要交的作业,作业1-数据结构及文章,文章暂时是个空链接,待完善
  • Loading branch information
BigJoyce committed Feb 25, 2017
1 parent 07ddfc8 commit 29e1c61
Show file tree
Hide file tree
Showing 17 changed files with 659 additions and 0 deletions.
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
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>
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
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;
}
};
}

}
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;
}


}
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;
}
}
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();

}
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;
}
}
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();
}
Loading

0 comments on commit 29e1c61

Please sign in to comment.