forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
13 changed files
with
446 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 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> |
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>FirstHomework</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
group14/857999411/FirstHomework/.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 |
73 changes: 73 additions & 0 deletions
73
group14/857999411/FirstHomework/src/com/coding/basic/MyArrayList.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,73 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.*; | ||
|
||
public class MyArrayList implements MyList{ | ||
//定义Object类型数组 | ||
//定义数组元素个数 | ||
private int size=0; | ||
private Object [] elementData =new Object[10]; | ||
|
||
public void add(Object o) { | ||
ensureCapacity(size+1); | ||
elementData[size] = o; | ||
size++; | ||
} | ||
|
||
//添加指定位置的元 | ||
public void add (int index,Object element){ | ||
if(index > size || index < 0) | ||
throw new IndexOutOfBoundsException("数组角标越界"); | ||
ensureCapacity(size+1); | ||
//添加指定位置元素 | ||
//将该位置后的有元素右 | ||
System.arraycopy(elementData,index,elementData,index+1,size-index); | ||
elementData[index] =element; | ||
size++; | ||
} | ||
|
||
//可调整数组的容量 | ||
public void ensureCapacity (int mincapacity){ | ||
int oldlen =elementData.length; | ||
if(mincapacity > oldlen){ | ||
int newlen =(oldlen * 3)/2 + 1; | ||
if(mincapacity > newlen) | ||
newlen =mincapacity; | ||
elementData =Arrays.copyOf(elementData,newlen); | ||
} | ||
} | ||
|
||
|
||
//获取指定位置的元 | ||
public Object get(int index){ | ||
if(index < 0 || index >size-1){ | ||
throw new IndexOutOfBoundsException("数组角标越界"); | ||
} | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index){ | ||
if(index >=size || index < 0){ | ||
throw new IndexOutOfBoundsException("数组角标越界"); | ||
} | ||
Object oldelement =elementData[index]; | ||
int numMoved = size-index-1; | ||
if(numMoved > 0){ | ||
System.arraycopy(elementData,index+1,elementData,index,numMoved); | ||
} | ||
size--; | ||
return oldelement; | ||
} | ||
|
||
public void clear(){ | ||
elementData = null; | ||
} | ||
|
||
public boolean isEmpty (){ | ||
return size == 0; | ||
} | ||
|
||
public int size (){ | ||
return size; | ||
} | ||
} |
138 changes: 138 additions & 0 deletions
138
group14/857999411/FirstHomework/src/com/coding/basic/MyLinkedList.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,138 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.*; | ||
|
||
public class MyLinkedList implements MyList{ | ||
//用内部类定义链表中的节点 | ||
private class Node{ | ||
//节点中包含数据和引用 | ||
Object data; | ||
Node next; | ||
|
||
public Node (){ | ||
|
||
} | ||
|
||
//每个节点包含数据和引 | ||
public Node (Object data,Node next){ | ||
this.data =data; | ||
this.next =next; | ||
} | ||
} | ||
//定义头节点和尾节 | ||
public Node head; | ||
public Node tail; | ||
public int size; | ||
|
||
//无参数构造函数创建空链表 | ||
public MyLinkedList(){ | ||
head =null; | ||
tail =null; | ||
} | ||
|
||
//链表中传入元 | ||
public MyLinkedList(Object element){ | ||
head.data =element; | ||
head.next =tail; | ||
size++; | ||
} | ||
|
||
public void add(Object o){ | ||
addLast(o); | ||
} | ||
public void addFirst(Object element) { | ||
|
||
head =new Node(element,head); | ||
if(tail == null){ | ||
tail=head; | ||
} | ||
size++; | ||
} | ||
|
||
public void addLast(Object element) { | ||
if(head == null) { | ||
head =new Node (element,null); | ||
tail =head; | ||
}else{ | ||
Node newNode =new Node(element,null); | ||
tail.next =newNode; | ||
tail=newNode; | ||
} | ||
size++; | ||
|
||
} | ||
|
||
public void add(int index,Object element){ | ||
|
||
if(index < 0 || index > size) { | ||
throw new IndexOutOfBoundsException("索引越界"); | ||
} | ||
if(index == 0) { | ||
head =new Node(element,head); | ||
} | ||
Node frontNode =getNode(index-1); | ||
frontNode.next =new Node(element,frontNode.next); | ||
size++; | ||
} | ||
public Node getNode(int index) | ||
{ | ||
if(index < 0 || index > size-1) { | ||
|
||
throw new IndexOutOfBoundsException("索引越界"); | ||
} | ||
Node current=head; | ||
for(int i=0;i < size; i++,current =current.next) { | ||
if(i == index) { | ||
return current; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public Object get(int index){ | ||
return getNode(index).data; | ||
} | ||
|
||
public Object remove(int index){ | ||
if(index < 0 || index > size-1) { | ||
throw new IndexOutOfBoundsException("索引越界"); | ||
} | ||
Node delNode =null; | ||
if(index == 0) { | ||
delNode =head; | ||
head =head.next; | ||
}else{ | ||
Node frontNode =getNode(index-1); | ||
delNode =frontNode.next; | ||
frontNode.next =delNode.next; | ||
delNode.next =null; | ||
} | ||
size--; | ||
return delNode.data; | ||
} | ||
|
||
public Object removeFirst(){ | ||
if(head == null || head.next == null) | ||
throw new NoSuchElementException(); | ||
Node oldhead =head; | ||
head =head.next; | ||
oldhead.next =null; | ||
size--; | ||
return oldhead.data; | ||
|
||
} | ||
|
||
public Object removeLast(){ | ||
return remove(size - 1); | ||
|
||
} | ||
|
||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
|
||
} | ||
|
||
|
10 changes: 10 additions & 0 deletions
10
group14/857999411/FirstHomework/src/com/coding/basic/MyList.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,10 @@ | ||
package com.coding.basic; | ||
|
||
public interface MyList { | ||
|
||
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(); | ||
} |
23 changes: 23 additions & 0 deletions
23
group14/857999411/FirstHomework/src/com/coding/basic/MyQueue.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,23 @@ | ||
package com.coding.basic; | ||
|
||
public class MyQueue { | ||
|
||
MyLinkedList link =new MyLinkedList(); | ||
|
||
//入队 | ||
public void enQueue(Object o){ | ||
link.addLast(o); | ||
} | ||
//出队 | ||
public Object deQueue(){ | ||
return link.removeFirst(); | ||
} | ||
//判断是否为空 | ||
public boolean isEmpty(){ | ||
return link.size == 0; | ||
} | ||
//获取长度 | ||
public int size(){ | ||
return link.size; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
group14/857999411/FirstHomework/src/com/coding/basic/MyStack.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,44 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.*; | ||
|
||
public class MyStack { | ||
|
||
|
||
MyArrayList elementData=new MyArrayList(); | ||
|
||
//入栈 | ||
public void push(Object o){ | ||
elementData.add(o); | ||
} | ||
|
||
//出栈 | ||
public Object pop(){ | ||
|
||
Object element =elementData.get(elementData.size() - 1); | ||
elementData.remove(elementData.size()-1); | ||
return element; | ||
} | ||
|
||
//获取栈顶元素 | ||
public Object peek(){ | ||
int len =elementData.size(); | ||
if(len == 0) | ||
throw new EmptyStackException(); | ||
Object element =elementData.get(len - 1); | ||
return element; | ||
} | ||
|
||
public int size(){ | ||
return elementData.size(); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return elementData.size() == 0; | ||
} | ||
|
||
public boolean empty(){ | ||
return elementData.isEmpty(); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
group14/857999411/FirstHomework/src/com/coding/test/MyArrayListTest.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,34 @@ | ||
package com.coding.test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import org.junit.Test; | ||
|
||
import com.coding.basic.MyArrayList; | ||
|
||
public class MyArrayListTest { | ||
|
||
@Test | ||
public void test() { | ||
MyArrayList sa =new MyArrayList(); | ||
sa.add(0,0); | ||
sa.add(1,1); | ||
sa.add(2,2); | ||
sa.add(3,3); | ||
|
||
//System.out.println(sa.get(1)); | ||
|
||
for(int i=0; i<sa.size(); i++) | ||
{ | ||
System.out.print(sa.get(i)); | ||
} | ||
|
||
System.out.println(sa.remove(3)); | ||
|
||
for(int i=0; i<sa.size(); i++) | ||
{ | ||
System.out.print(sa.get(i)); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.