Skip to content

Commit 55f2072

Browse files
authored
Merge pull request onlyliuxin#25 from heyucool/master
十四组第一周作业
2 parents 7c65027 + 3505a81 commit 55f2072

File tree

140 files changed

+7520
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

140 files changed

+7520
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
6+
<classpathentry kind="output" path="bin"/>
7+
</classpath>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>basicstructuredemo</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.maple.basic;
2+
3+
public class ArrayList implements List {
4+
5+
private int size = 0;
6+
7+
private Object[] elementData = new Object[100];
8+
9+
public void add(Object o){
10+
//不够了怎么扩容
11+
elementData[size++]=o;
12+
}
13+
public void add(int index, Object o){
14+
if(index<0||index>size){
15+
throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size);
16+
}
17+
for(int i=size;i>index;i--){
18+
elementData[i-1]=elementData[i];
19+
}
20+
elementData[index]=o;
21+
size++;
22+
}
23+
24+
public Object get(int index){
25+
if(index<0||index>=size){
26+
throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size);
27+
}
28+
return elementData[index];
29+
}
30+
31+
public Object remove(int index){
32+
if(index<0||index>=size){
33+
throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size);
34+
}
35+
Object removeObj=elementData[index];
36+
for(int i=index;i<size-1;i++){
37+
elementData[i]=elementData[i+1];
38+
}
39+
size--;
40+
return removeObj;
41+
}
42+
43+
public int size(){
44+
return size;
45+
}
46+
47+
public Iterator iterator(){
48+
return new Iterator() {
49+
int ref=0;
50+
@Override
51+
public Object next() {
52+
if(hasNext()){
53+
return elementData[ref++];
54+
}
55+
return null;
56+
}
57+
58+
@Override
59+
public boolean hasNext() {
60+
if(ref<0||ref>=size) return false;
61+
return true;
62+
}
63+
};
64+
}
65+
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.maple.basic;
2+
3+
import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateName;
4+
5+
public class BinaryTree<T extends Comparable<T>> {
6+
private BinaryTreeNode<T> root;
7+
8+
public void traversal(BinaryTreeNode<T> node){
9+
if(node.getLeft()!=null){
10+
traversal(node.getLeft());
11+
}
12+
System.out.println("--"+node.getData()+"--");
13+
if(node.getRight()!=null){
14+
traversal(node.getRight());
15+
}
16+
}
17+
/**
18+
* 如果根节点为null,则作为根节点,否则遍历下去插值
19+
* @param o
20+
* @return
21+
* 2017年2月23日 下午4:21:51
22+
* @Author Joy
23+
*/
24+
public BinaryTreeNode insert(T o){
25+
if(root==null){
26+
BinaryTreeNode<T> newB=new BinaryTreeNode<T>();
27+
newB.setData(o);
28+
newB.setLeft(null);
29+
newB.setRight(null);
30+
root=newB;
31+
return root;
32+
}
33+
34+
return root.insert(o);
35+
}
36+
public BinaryTreeNode<T> getRoot() {
37+
return root;
38+
}
39+
40+
public void setRoot(BinaryTreeNode<T> root) {
41+
this.root = root;
42+
}
43+
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.maple.basic;
2+
3+
import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateName;
4+
5+
public class BinaryTreeNode<T extends Comparable<T>>{
6+
7+
private T data;
8+
private BinaryTreeNode left;
9+
private BinaryTreeNode right;
10+
11+
/**
12+
* 如果待插入的值等于节点的值,则抛出异常:duplicate value
13+
* 如果小于节点的值,则往左边遍历
14+
* 如果大于节点的值,则往右边遍历
15+
* @param o
16+
* @return
17+
* 2017年2月23日 下午4:22:50
18+
* @Author Joy
19+
*/
20+
public BinaryTreeNode insert(T o){
21+
//assume that no duplicate key
22+
23+
if(o.compareTo(data)==0){
24+
try {
25+
throw new DuplicateName("duplicate value: "+o);
26+
} catch (DuplicateName e) {
27+
e.printStackTrace();
28+
}
29+
}
30+
BinaryTreeNode<T> newB=new BinaryTreeNode<T>();
31+
newB.setData(o);
32+
newB.setLeft(null);
33+
newB.setRight(null);
34+
//o更大,在右边
35+
if(o.compareTo(data)>0){
36+
if(this.getRight()!=null){
37+
this.getRight().insert(o);
38+
}else{
39+
this.setRight(newB);
40+
}
41+
}else if(o.compareTo(data)<0){
42+
if(this.getLeft()!=null){
43+
this.getLeft().insert(o);
44+
}else{
45+
this.setLeft(newB);
46+
}
47+
}
48+
return newB;
49+
}
50+
51+
public T getData() {
52+
return data;
53+
}
54+
public void setData(T data) {
55+
this.data = data;
56+
}
57+
public BinaryTreeNode getLeft() {
58+
return left;
59+
}
60+
public void setLeft(BinaryTreeNode left) {
61+
this.left = left;
62+
}
63+
public BinaryTreeNode getRight() {
64+
return right;
65+
}
66+
public void setRight(BinaryTreeNode right) {
67+
this.right = right;
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.maple.basic;
2+
3+
public interface Iterator {
4+
public boolean hasNext();
5+
public Object next();
6+
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package com.maple.basic;
2+
3+
import java.util.NoSuchElementException;
4+
5+
public class LinkedList implements List {
6+
7+
private Node head;
8+
private int size = 0;//自己加的,觉得需要
9+
/**
10+
* 与addList()是一样的
11+
*/
12+
public void add(Object o){
13+
addLast(o);
14+
}
15+
public void add(int index , Object o){
16+
if(index<0||index>size){
17+
throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size);
18+
}
19+
Node prevNode=head;
20+
Node curNode=head.next;
21+
int count=0;
22+
while(count<=index){
23+
if(count==index){
24+
Node newNode=new Node();
25+
newNode.data=o;
26+
27+
newNode.next=curNode;
28+
prevNode.next=newNode;
29+
size++;
30+
break;
31+
}
32+
curNode=curNode.next;
33+
prevNode=prevNode.next;
34+
count++;
35+
}
36+
37+
38+
}
39+
public Object get(int index){
40+
if(index<0||index>=size)
41+
throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size);
42+
43+
Node curNode=head.next;
44+
int count=0;
45+
while(count<=index){
46+
if(count==index){
47+
return curNode.data;
48+
}
49+
curNode=curNode.next;
50+
count++;
51+
}
52+
return null;
53+
}
54+
public Object remove(int index){
55+
if(index<0||index>=size)
56+
throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size);
57+
Node prevNode=head;
58+
Node curNode=head.next;
59+
int count=0;
60+
while(count<=index){
61+
if(count==index){
62+
prevNode.next=curNode.next;
63+
size--;
64+
return curNode.data;
65+
}
66+
curNode=curNode.next;
67+
prevNode=prevNode.next;
68+
count++;
69+
}
70+
return null;
71+
}
72+
73+
public int size(){
74+
return size;
75+
}
76+
77+
public void addFirst(Object o){
78+
Node objNode=new Node();
79+
objNode.data=o;
80+
if(head==null) head=new Node();
81+
objNode.next=head.next;
82+
size++;
83+
head.next=objNode;
84+
}
85+
public void addLast(Object o){
86+
Node objNode=new Node();
87+
objNode.data=o;
88+
if(head==null) head=new Node();
89+
90+
//也可以用iterator迭代,先不用吧
91+
Node curNode=head;
92+
while(curNode.next!=null){
93+
curNode=curNode.next;
94+
}
95+
objNode.next=curNode.next;
96+
curNode.next=objNode;
97+
size++;
98+
99+
}
100+
public Object removeFirst(){
101+
if(head==null||head.next==null)
102+
throw new NoSuchElementException();
103+
Node delNode=head.next;
104+
head.next=delNode.next;
105+
size--;
106+
return delNode.data;
107+
}
108+
public Object removeLast(){
109+
if(head==null||head.next==null)
110+
throw new NoSuchElementException();
111+
Node prevNode=head;
112+
Node curNode=head.next;
113+
while(curNode!=null){
114+
if(curNode.next==null){//说明是尾节点
115+
prevNode.next=curNode.next;
116+
size--;
117+
return curNode.data;
118+
}
119+
curNode=curNode.next;
120+
prevNode=prevNode.next;
121+
}
122+
return null;
123+
}
124+
public Iterator iterator(){
125+
return new Iterator() {
126+
private Node cur=head!=null?head.next:head;
127+
@Override
128+
public Object next() {
129+
if(cur==null){
130+
throw new NoSuchElementException();
131+
}
132+
Object object=cur.data;
133+
cur=cur.next;
134+
return object;
135+
}
136+
137+
@Override
138+
public boolean hasNext() {
139+
if(cur==null){
140+
return false;
141+
}else{
142+
return true;
143+
}
144+
145+
}
146+
};
147+
}
148+
149+
150+
private static class Node{
151+
Object data;
152+
Node next;
153+
}
154+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.maple.basic;
2+
3+
public interface List {
4+
public void add(Object o);
5+
public void add(int index, Object o);
6+
public Object get(int index);
7+
public Object remove(int index);
8+
public int size();
9+
}

0 commit comments

Comments
 (0)