forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 8
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 #15 from wys2017/master
第一次作业
- Loading branch information
Showing
13 changed files
with
608 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"/> | ||
<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,6 @@ | ||
/bin/ | ||
|
||
/.setting/ | ||
/.classpath | ||
/.project | ||
|
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>2471486765Learning</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> |
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,110 @@ | ||
package com.mycoding; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
private Object[] elementData = new Object[10]; | ||
|
||
|
||
//增加元素 | ||
public void add(Object o) { | ||
if (size()>=elementData.length) { | ||
elementData = ArrayList.grow(elementData,elementData.length); | ||
} | ||
elementData[size++] = o; | ||
} | ||
|
||
//在指定位置上增加元素 | ||
public void add(int index, Object o) { | ||
if(index < 0 || index > size()) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
else if(index == 0 || index > 0 && index < size()) { | ||
elementData = ArrayList.grow(elementData,elementData.length); | ||
for(int i=size()-1;i>=index;i--) { | ||
elementData[i+1] = elementData[i]; | ||
} | ||
elementData[index] = o; | ||
size++; | ||
} | ||
else if(index == size()) { | ||
elementData = ArrayList.grow(elementData,elementData.length); | ||
elementData[size++] = o; | ||
} | ||
} | ||
|
||
//返回此列表中指定位置上的元素 | ||
public Object get(int index) { | ||
if (index < 0 || index >= size()) { | ||
throw new IndexOutOfBoundsException(); | ||
} else { | ||
return elementData[index]; | ||
} | ||
} | ||
|
||
//删除指定位置的元素 | ||
public Object remove(int index) { | ||
if (index < 0 || index >= size()) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
if(index == 0 || index > 0 && index < size()) { | ||
for(int i=index;i<size()-1;i++){ | ||
elementData[i] = elementData[i+1]; | ||
} | ||
size--; | ||
} | ||
return elementData; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
//扩容 | ||
public static Object[] grow(Object[] src,int size) { | ||
Object[] target = new Object[2*size]; | ||
System.arraycopy(src, 0, target, 0, size); | ||
return target; | ||
} | ||
|
||
public Iterator iterator() { | ||
return new ArrayListIterator(this); | ||
|
||
} | ||
|
||
private class ArrayListIterator implements Iterator { | ||
@SuppressWarnings("unused") | ||
ArrayList array = null; | ||
int cursor=0; | ||
private ArrayListIterator(ArrayList array) { | ||
this.array = array; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return cursor < size; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
if(!hasNext()) { | ||
throw new NoSuchElementException(); | ||
} | ||
return elementData[cursor++]; | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuffer str = new StringBuffer(); | ||
str.append("["); | ||
for (int i = 0; i < size(); i++) { | ||
str.append(elementData[i]+","); | ||
} | ||
str.deleteCharAt(str.length()-1); | ||
str.append("]"); | ||
return str.toString(); | ||
} | ||
} |
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 @@ | ||
package com.mycoding; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
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,170 @@ | ||
package com.mycoding; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
public class LinkedList implements List { | ||
private Node head; | ||
private int size; | ||
|
||
|
||
public LinkedList() { | ||
this.head = null; | ||
this.size = 0; | ||
} | ||
//添加新元素 | ||
public void add(Object o){ | ||
Node newNode = new Node(o); | ||
if(head == null) { | ||
head = newNode; | ||
} else { | ||
Node nextNode = head; | ||
while(nextNode.next != null) { | ||
nextNode = nextNode.next; | ||
} | ||
nextNode.next = new Node(o); | ||
} | ||
size++; | ||
} | ||
|
||
//在指定位置添加新元素 | ||
public void add(int index , Object o){ | ||
checkAddElementIndex(index); | ||
Node addNode = head; | ||
if(index == 0) { | ||
addFirst(o); | ||
} else { | ||
for(int i=0;i<index-1;i++) { | ||
addNode = addNode.next; | ||
} | ||
Node insertNode = new Node(o); | ||
insertNode.next = addNode.next; | ||
addNode.next = insertNode; | ||
size++; | ||
} | ||
} | ||
|
||
public Object get(int index){ | ||
checkGetElementIndex(index); | ||
Node getNode = head; | ||
for(int i=0;i<index;i++) { | ||
getNode = getNode.next; | ||
} | ||
return getNode.data; | ||
} | ||
|
||
public Object remove(int index){ | ||
checkGetElementIndex(index); | ||
Node indexNode = head; | ||
|
||
if(index == 0) { | ||
Node laterNode = head; | ||
while (indexNode.next != null) { | ||
laterNode.data = indexNode.next.data; | ||
laterNode = laterNode.next; | ||
indexNode = indexNode.next; | ||
} | ||
size--; | ||
return laterNode.data; | ||
} else { | ||
if(index > 0 && index < size()) { | ||
for (int i=0;i<index-1;i++) { | ||
indexNode = indexNode.next; | ||
} | ||
indexNode.next = indexNode.next.next; | ||
size--; | ||
} | ||
return indexNode.data; | ||
} | ||
} | ||
|
||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public void addFirst(Object o){ | ||
Node addFirstNode = new Node(o); | ||
addFirstNode.next = head; | ||
head = addFirstNode; | ||
size++; | ||
} | ||
|
||
|
||
public void addLast(Object o){ | ||
add(o); | ||
} | ||
|
||
|
||
public Object removeFirst(){ | ||
isEmpty(); | ||
return remove(0); | ||
} | ||
public Object removeLast(){ | ||
return remove(size()-1); | ||
} | ||
|
||
public Iterator iterator(){ | ||
return new LinkedListIterator(this); | ||
} | ||
private class LinkedListIterator implements Iterator { | ||
@SuppressWarnings("unused") | ||
LinkedList linked = null; | ||
int cursor=0; | ||
private LinkedListIterator(LinkedList linked) { | ||
this.linked = linked; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return cursor < size; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
if(!hasNext()) { | ||
throw new NoSuchElementException(); | ||
} | ||
return get(cursor++); | ||
} | ||
} | ||
|
||
public void checkGetElementIndex(int index) { | ||
if (index < 0 || index >= size()){ | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
} | ||
|
||
public void checkAddElementIndex(int index) { | ||
if (index < 0 || index > size()) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
} | ||
public void isEmpty() { | ||
if(head == null) { | ||
throw new NoSuchElementException(); | ||
} | ||
} | ||
|
||
private static class Node{ | ||
Object data; | ||
Node next; | ||
|
||
public Node(Object o){ | ||
data = o; | ||
next = null; | ||
} | ||
} | ||
|
||
|
||
@Override | ||
public String toString() { | ||
StringBuffer str = new StringBuffer(); | ||
str.append("["); | ||
for(int i=0; i<size; i++){ | ||
str.append( this.get(i) + ","); | ||
} | ||
str.deleteCharAt(str.length()-1); | ||
str.append("]"); | ||
return str.toString(); | ||
} | ||
} |
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 @@ | ||
package com.mycoding; | ||
|
||
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(); | ||
public Iterator iterator(); | ||
} |
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.mycoding; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
public class Queue { | ||
|
||
private LinkedList linked = new LinkedList(); | ||
|
||
public void enQueue(Object o) { | ||
linked.add(o); | ||
} | ||
|
||
public Object deQueue() { | ||
|
||
if(isEmpty()) { | ||
throw new NoSuchElementException(); | ||
} | ||
return linked.remove(size()-1); | ||
} | ||
|
||
public boolean isEmpty() { | ||
if(size()-1 == 0) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
public int size() { | ||
return linked.size(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return linked.toString(); | ||
} | ||
} |
Oops, something went wrong.