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 #11 from PG123GG/master
1067041567第一次作业,实现简单的数据结构
- Loading branch information
Showing
11 changed files
with
510 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,12 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>RemoteSystemsTempFiles</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.rse.ui.remoteSystemsTempNature</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,6 @@ | ||
<?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.7"/> | ||
<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>TestColleaction</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
group27/1067041567/TestColleaction/.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.7 | ||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
org.eclipse.jdt.core.compiler.compliance=1.7 | ||
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.7 |
100 changes: 100 additions & 0 deletions
100
group27/1067041567/TestColleaction/src/cn/task1/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,100 @@ | ||
package cn.task1; | ||
|
||
import java.util.LinkedList; | ||
|
||
public class ArrayList implements List { | ||
|
||
int size; | ||
Object[] obj; | ||
|
||
public ArrayList(){ | ||
size = 0; | ||
obj = new Object[size]; | ||
} | ||
|
||
@Override | ||
public void add(Object object) { | ||
if(size==0){ | ||
obj = new Object[1]; | ||
obj[0] = object; | ||
size = 1; | ||
}else{ | ||
Object[] temp = new Object[size+1]; | ||
// for(int k =0;k<size;k++){ | ||
// temp[k] = obj[k]; | ||
// } | ||
System.arraycopy(obj, 0, temp, 0, size); | ||
temp[size] = object; | ||
size = size +1; | ||
obj = temp; | ||
} | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
// TODO Auto-generated method stub | ||
return obj[index]; | ||
} | ||
|
||
@Override | ||
public void remove(int index) { | ||
// TODO Auto-generated method stub | ||
Object[] temp = new Object[size-1]; | ||
for(int k=0;k<size-1;k++){ | ||
if(k<index){ | ||
temp[k] = obj[k]; | ||
}if(k>index){ | ||
temp[k-1] = obj[k]; | ||
} | ||
} | ||
obj = temp; | ||
size--; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
if(size>0){ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public void set(int index,Object object){ | ||
Object[] temp = new Object[size+1]; | ||
for(int k=0;k<size+1;k++){ | ||
if(k<index){ | ||
temp[k] = obj[k]; | ||
}if(k==index){ | ||
temp[k] = object; | ||
}if(k>index){ | ||
temp[k] = obj[k-1]; | ||
} | ||
} | ||
obj = temp; | ||
size++; | ||
} | ||
|
||
|
||
public static void main(String[] args) { | ||
ArrayList list = new ArrayList(); | ||
|
||
list.add("d"); | ||
list.add("dd"); | ||
list.add("cc"); | ||
list.remove(2); | ||
list.set(1, "dwe"); | ||
|
||
System.out.println(list.get(0)); | ||
System.out.println(list.get(1)); | ||
System.out.println(list.get(2)); | ||
System.out.println(list.size()); | ||
System.out.println(list.isEmpty()); | ||
|
||
} | ||
|
||
} |
146 changes: 146 additions & 0 deletions
146
group27/1067041567/TestColleaction/src/cn/task1/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,146 @@ | ||
package cn.task1; | ||
|
||
public class LinkedList{ | ||
|
||
Node head; | ||
int size; | ||
|
||
public LinkedList(){ | ||
head = new Node(); | ||
this.size = 0; | ||
} | ||
|
||
public void add(Object data) { | ||
Node temp = head; | ||
while(temp.next != null){ | ||
temp = temp.next; | ||
} | ||
temp.next = new Node(data); | ||
size++; | ||
} | ||
|
||
public void set(int index,Object obj){ | ||
try { | ||
Node temp = new Node(obj); | ||
Node pre = null; | ||
if (index > 0) { | ||
pre = getNode(index - 1); | ||
} else { | ||
pre = head; | ||
} | ||
Node last = getNode(index); | ||
pre.next = temp; | ||
temp.next = last; | ||
size++; | ||
} catch (Exception e) { | ||
// TODO: handle exception | ||
try { | ||
throw new Exception("存在异常错误!"); | ||
} catch (Exception e1) { | ||
// TODO Auto-generated catch block | ||
e1.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
|
||
public Object get(int index) { | ||
Node temp = head; | ||
if(index>=size){ | ||
throw new ArrayIndexOutOfBoundsException("超出范围!"); | ||
} | ||
for(int k=0;k<index;k++){ | ||
temp = temp.next; | ||
} | ||
return temp.next.obj; | ||
} | ||
|
||
public Node getNode(int index) { | ||
Node temp = head; | ||
if(index>=size){ | ||
throw new ArrayIndexOutOfBoundsException("超出范围!"); | ||
} | ||
for(int k=0;k<index;k++){ | ||
temp = temp.next; | ||
} | ||
return temp.next; | ||
} | ||
|
||
|
||
public void remove(int index) { | ||
// TODO Auto-generated method stub | ||
Node temp = head; | ||
if(index>=size){ | ||
throw new ArrayIndexOutOfBoundsException("超出范围!"); | ||
} | ||
for(int k=0;k<size;k++){ | ||
if(k==index){ | ||
temp.next = temp.next.next; | ||
size--; | ||
break; | ||
}else{ | ||
temp = temp.next; | ||
} | ||
} | ||
} | ||
|
||
|
||
public int size() { | ||
// TODO Auto-generated method stub | ||
return size; | ||
} | ||
|
||
|
||
public boolean isEmpty() { | ||
if(size>0){ | ||
return true; | ||
}else{ | ||
return false; | ||
} | ||
} | ||
|
||
public void clear(){ | ||
head.next = null; | ||
size=0; | ||
} | ||
|
||
public static void main(String[] args) { | ||
LinkedList list = new LinkedList(); | ||
list.add("aa"); | ||
list.add("bb"); | ||
list.add("cc"); | ||
list.add("dd"); | ||
list.add("ff"); | ||
list.set(4, "4546"); | ||
// list.remove(2);list.remove(2); | ||
System.out.println(list.size()); | ||
System.out.println(list.get(0)); | ||
System.out.println(list.get(1)); | ||
System.out.println(list.get(2)); | ||
System.out.println(list.get(3)); | ||
System.out.println(list.get(4)); | ||
System.out.println(list.get(5)); | ||
System.out.println(list.isEmpty()); | ||
list.clear(); | ||
System.out.println(list.isEmpty()); | ||
} | ||
|
||
} | ||
|
||
|
||
class Node{ | ||
|
||
Object obj; | ||
Node next; | ||
|
||
public Node(){ | ||
this.obj = null; | ||
this.next = null; | ||
} | ||
|
||
public Node(Object obj){ | ||
this.obj = obj; | ||
this.next = null; | ||
} | ||
|
||
} |
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,14 @@ | ||
package cn.task1; | ||
|
||
public interface List { | ||
|
||
public void add(Object obj); | ||
|
||
public Object get(int index); | ||
|
||
public void remove(int index); | ||
|
||
public int size(); | ||
|
||
public boolean isEmpty(); | ||
} |
Oops, something went wrong.