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.
Merge pull request #14 from liyanyang0316/master
first homework
- Loading branch information
Showing
11 changed files
with
386 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.7"/> | ||
<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>2017Leaning</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/630254746/2017Leaning/.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 |
87 changes: 87 additions & 0 deletions
87
group14/630254746/2017Leaning/src/com/leaning/code/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,87 @@ | ||
package com.leaning.code; | ||
|
||
import java.util.Arrays; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size; // 记录集合中的元素个数 | ||
|
||
private Object[] elementsData; | ||
|
||
private int totalCount = 1; // 记录集合的大小 | ||
|
||
public ArrayList() { | ||
this.elementsData = new Object[totalCount]; | ||
} | ||
|
||
private void grow() { | ||
if (size >= totalCount) { | ||
// 进行扩容 | ||
int oldCapacity = size; | ||
int newCapacity = oldCapacity + oldCapacity << 1; | ||
totalCount = newCapacity; | ||
elementsData = Arrays.copyOf(elementsData, newCapacity); | ||
} | ||
} | ||
|
||
@Override | ||
public void add(Object o) { | ||
if (totalCount > size) { | ||
elementsData[size++] = o; | ||
} else { | ||
grow(); | ||
elementsData[size++] = o; | ||
} | ||
} | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
if (index < size) { | ||
if (totalCount <= size + 1) { | ||
grow(); | ||
} | ||
System.arraycopy(elementsData, index, elementsData, index + 1, size | ||
- index); | ||
elementsData[index] = 0; | ||
|
||
} else { | ||
throw new RuntimeException("数组下标越界"); | ||
} | ||
size++; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
if (index < size) | ||
return elementsData[index]; | ||
else | ||
throw new RuntimeException("数组下标越界"); | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
if (index >= size || index < 0) { | ||
throw new RuntimeException("数组下标越界"); | ||
} | ||
Object o = elementsData[index]; | ||
|
||
int numMoved = size - index - 1; | ||
if (numMoved > 0) | ||
System.arraycopy(elementsData, index + 1, elementsData, index, | ||
numMoved); | ||
elementsData[--size] = null; | ||
|
||
return o; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ArrayList [elementsData=" + Arrays.toString(elementsData) + "]"; | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
group14/630254746/2017Leaning/src/com/leaning/code/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,8 @@ | ||
package com.leaning.code; | ||
|
||
public interface Iterator { | ||
|
||
public boolean hasNext(); | ||
|
||
public Object Next(); | ||
} |
150 changes: 150 additions & 0 deletions
150
group14/630254746/2017Leaning/src/com/leaning/code/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,150 @@ | ||
package com.leaning.code; | ||
|
||
|
||
|
||
|
||
|
||
|
||
public class LinkedList implements List { | ||
|
||
private Node head; | ||
|
||
private Node last; | ||
|
||
private int size; | ||
|
||
|
||
void linkLast(Object o){ | ||
Node lastNode = last; | ||
Node newNode = new Node(lastNode, o, null); | ||
last = newNode; | ||
if (lastNode == null) | ||
head = newNode; | ||
else | ||
lastNode.next = newNode; | ||
size++; | ||
} | ||
|
||
void linkHead(Object o){ | ||
Node headNode = head; | ||
Node newNode = new Node(null, o, headNode); | ||
head = newNode; | ||
if (head == null) | ||
last = newNode; | ||
else | ||
head.prev = newNode; | ||
size++; | ||
} | ||
|
||
@Override | ||
public void add(Object o) { | ||
linkLast(o); | ||
} | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
if (index < 0 || index >= size) { | ||
throw new RuntimeException("Êý×éϱêÔ½½ç"); | ||
} | ||
Node n = find(index); | ||
Node pred = n.prev; | ||
Node newNode = new Node(pred, o, n); | ||
n.prev = newNode; | ||
if (pred == null) | ||
head = newNode; | ||
else | ||
pred.next = newNode; | ||
size++; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
return find(index).item; | ||
} | ||
|
||
Node find(int index){ | ||
if (index < (size >> 1)) { | ||
Node n = head; | ||
for (int i = 0; i < index; i++) | ||
n = n.next; | ||
return n; | ||
} else { | ||
Node n = last; | ||
for (int i = size - 1; i > index; i--) | ||
n = n.prev; | ||
return n; | ||
} | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
Node n = find(index); | ||
Object o = n.item; | ||
final Node prev = n.prev; | ||
final Node next = n.next; | ||
if (null != prev) { | ||
prev.next = next; | ||
} | ||
if (null != next) { | ||
next.prev = prev; | ||
} | ||
n.item = null; | ||
n.next = null; | ||
n.prev = null; | ||
size-- ; | ||
return o; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
public void addFrist(Object o){ | ||
linkHead(o); | ||
} | ||
|
||
public void addLast(Object o){ | ||
linkLast(o); | ||
} | ||
|
||
public Object removeFirst(){ | ||
Object o = head.item; | ||
Node n = head.next; | ||
head = n; | ||
if (n == null) | ||
last = null; | ||
else | ||
n.prev = null; | ||
size --; | ||
return o; | ||
} | ||
|
||
public Object removeLaset(){ | ||
Object o = last.item; | ||
Node p = last.prev; | ||
last = p; | ||
if (p == null) | ||
head = null; | ||
else | ||
p.next = null; | ||
size --; | ||
return o; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return null; | ||
} | ||
|
||
public static class Node{ | ||
Object item; | ||
Node next; | ||
Node prev; | ||
|
||
Node(Node prev, Object element, Node next) { | ||
this.item = element; | ||
this.next = next; | ||
this.prev = prev; | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
group14/630254746/2017Leaning/src/com/leaning/code/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,15 @@ | ||
package com.leaning.code; | ||
|
||
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(); | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
group14/630254746/2017Leaning/src/com/leaning/code/Queue.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,22 @@ | ||
package com.leaning.code; | ||
|
||
public class Queue { | ||
|
||
private LinkedList list = new LinkedList(); | ||
|
||
public void enQueue(Object o) { | ||
list.add(o); | ||
} | ||
|
||
public Object deQueue() { | ||
return list.removeFirst(); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return list.size() == 0; | ||
} | ||
|
||
public int size(){ | ||
return list.size(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
group14/630254746/2017Leaning/src/com/leaning/code/Stack.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,31 @@ | ||
package com.leaning.code; | ||
|
||
public class Stack { | ||
|
||
private ArrayList list = new ArrayList(); | ||
|
||
private int size; | ||
|
||
|
||
public void push(Object o){ | ||
list.add(o); | ||
size ++; | ||
} | ||
|
||
public Object pop(){ | ||
return list.get(--size); | ||
} | ||
|
||
public Object peek(){ | ||
return list.get(size-1); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return size == 0; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
group14/630254746/2017Leaning/src/com/leaning/code/test/ArrayListTest.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,37 @@ | ||
package com.leaning.code.test; | ||
|
||
import org.junit.Test; | ||
|
||
import com.leaning.code.ArrayList; | ||
import com.leaning.code.LinkedList; | ||
|
||
public class ArrayListTest { | ||
|
||
@Test | ||
public void test01(){ | ||
ArrayList list = new ArrayList(); | ||
list.add("a"); | ||
list.add("b"); | ||
list.add("c"); | ||
|
||
System.out.println(list.remove(0)); | ||
System.out.println(list); | ||
|
||
} | ||
|
||
@Test | ||
public void test02(){ | ||
LinkedList list = new LinkedList(); | ||
list.add("a"); | ||
list.add("b"); | ||
list.add("c"); | ||
|
||
list.add(2, "d"); | ||
|
||
System.out.println(list.remove(0)); | ||
System.out.println(list.get(0)); | ||
System.out.println(list.get(2)); | ||
|
||
|
||
} | ||
} |