forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 7
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 #9 from Gropingzy/master
0226_homework
- Loading branch information
Showing
10 changed files
with
371 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
group15/1517_279137987/my/collection/linear/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 my.collection.linear; | ||
|
||
import java.util.Arrays; | ||
|
||
public class MyArrayList implements MyList{ | ||
|
||
private int size = 0; | ||
private int CAPACITY = 10; | ||
private Object[] elementData = new Object[CAPACITY]; | ||
|
||
public MyArrayList(int len){ | ||
this.elementData = new Object[len]; | ||
} | ||
|
||
public void add(Object obj){ | ||
add(size,obj); | ||
} | ||
|
||
public void add(int index, Object obj){ | ||
if(index < 0){ | ||
System.out.println("insert position illegal."); | ||
}else{ | ||
if(size + 1 < elementData.length){ | ||
System.arraycopy(elementData, 0, this.elementData, 0, index); | ||
System.arraycopy(elementData, index, this.elementData, index+1, elementData.length-index-1); | ||
this.elementData[index] = obj; | ||
}else{ | ||
Object[] newElementData = Arrays.copyOf(elementData, elementData.length + CAPACITY); | ||
System.arraycopy(elementData, index, newElementData, index+1, elementData.length - index); | ||
newElementData[index] = obj; | ||
this.elementData = newElementData; | ||
} | ||
size++; | ||
} | ||
} | ||
|
||
public Object remove(int index){ | ||
if(index == 0){ | ||
System.arraycopy(elementData, 1, this.elementData, 0, elementData.length-1); | ||
}else{ | ||
System.arraycopy(elementData, 0, this.elementData, 0, index); | ||
System.arraycopy(elementData, index + 1, this.elementData, index, elementData.length-index-1); | ||
} | ||
size--; | ||
return elementData[index]; | ||
} | ||
|
||
public Object get(int index){ | ||
if(index < 0 || index > elementData.length-1){ | ||
return "get position illegal."; | ||
}else{ | ||
return elementData[index]; | ||
} | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public String toString(){ | ||
String str ="toString():"; | ||
for(int i=0; i<size; i++){ | ||
str += elementData[i] + "\t"; | ||
} | ||
return str; | ||
} | ||
|
||
/*private void grow(int stepLen){ | ||
Object[] largerElement = new Object[CAPACITY + stepLen]; | ||
System.arraycopy(elementData, 0, largerElement, 0, elementData.length); | ||
elementData = largerElement; | ||
}*/ | ||
} |
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 my.collection.linear; | ||
|
||
public interface MyIterator { | ||
|
||
public boolean hasNext(); //if has next element | ||
|
||
public Object next(); //get next element | ||
} |
95 changes: 95 additions & 0 deletions
95
group15/1517_279137987/my/collection/linear/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,95 @@ | ||
package my.collection.linear; | ||
|
||
public class MyLinkedList implements MyList { | ||
|
||
private Node head; | ||
|
||
private int size = 0; | ||
|
||
public void add(Object obj) { | ||
add(this.size, obj); | ||
} | ||
|
||
public void add(int index, Object obj) { | ||
Node curNode = head; | ||
Node addNode = new Node(obj); | ||
if(index == 0){ | ||
addNode.next = head; | ||
head = addNode; | ||
}else{ | ||
for(int i=0; i<index-1; i++){ | ||
curNode = curNode.next; | ||
} | ||
addNode.next = curNode.next; | ||
curNode.next = addNode; | ||
} | ||
size++; | ||
} | ||
|
||
public Object remove(int index) { | ||
Node curNode = head; | ||
Node remNode = new Node(); | ||
if(index == 0){ | ||
remNode = head; | ||
head = head.next; | ||
}else{ | ||
for(int i=0; i<index-1; i++){ | ||
curNode = curNode.next; | ||
} | ||
curNode.next = curNode.next.next; | ||
} | ||
size--; | ||
return remNode; | ||
} | ||
|
||
public int size() { | ||
return this.size; | ||
} | ||
|
||
public Object get(int index) { | ||
Node curNode = head; | ||
for(int i=0; i<index; i++){ | ||
curNode = curNode.next; | ||
} | ||
return curNode.data; | ||
} | ||
|
||
public void addFirst(Object obj){ | ||
add(0,obj); | ||
} | ||
|
||
public void addLast(Object obj){ | ||
add(size,obj); | ||
} | ||
|
||
public Object removeFirst(){ | ||
return remove(0); | ||
} | ||
|
||
public Object removeLast(){ | ||
return remove(size-1); | ||
} | ||
|
||
public String toString(){ | ||
Node curNode = head; | ||
String str = "\n" + "toString():"; | ||
while(curNode != null){ | ||
str += curNode.data + "\t"; | ||
curNode = curNode.next; | ||
} | ||
return str; | ||
} | ||
|
||
private static class Node{ | ||
Object data; | ||
Node next; | ||
|
||
private Node(){ | ||
} | ||
|
||
private Node(Object obj){ | ||
this.data = 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 my.collection.linear; | ||
|
||
public interface MyList { | ||
|
||
public void add(Object obj); //add to end | ||
|
||
public void add(int index, Object obj); //add to specific position | ||
|
||
public Object get(int index); //return specific element | ||
|
||
public Object remove(int index); //return deleted specific element | ||
|
||
public int size(); //return the size of ArrayList | ||
} |
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,26 @@ | ||
package my.collection.linear; | ||
|
||
public class MyQueue { | ||
MyLinkedList queue = new MyLinkedList(); | ||
|
||
public void enQueue(Object obj){ | ||
queue.add(obj); | ||
} | ||
|
||
public Object deQueue(){ | ||
return queue.removeFirst(); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
if(queue.size() == 0){ | ||
return true; | ||
}else{ | ||
return false; | ||
} | ||
} | ||
|
||
public int size(){ | ||
return queue.size(); | ||
} | ||
|
||
} |
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,36 @@ | ||
package my.collection.linear; | ||
|
||
public class MyStack { | ||
private int capacity = 10; | ||
|
||
private MyArrayList elementData = new MyArrayList(capacity); | ||
|
||
public void push(Object obj){ | ||
elementData.add(obj); | ||
} | ||
|
||
public Object pop(){ | ||
int tmpSize = elementData.size()-1; | ||
Object tmpElement = elementData.get(tmpSize); | ||
elementData.remove(tmpSize); | ||
return tmpElement; | ||
} | ||
|
||
public Object peek(){ | ||
return elementData.get(elementData.size()-1); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
boolean isEmp; | ||
if(elementData.size() == 0){ | ||
isEmp = true; | ||
}else{ | ||
isEmp = false; | ||
} | ||
return isEmp; | ||
} | ||
|
||
public int size(){ | ||
return elementData.size(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
group15/1517_279137987/my/collection/linearTest/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,38 @@ | ||
package my.collection.linearTest; | ||
|
||
import my.collection.linear.MyArrayList; | ||
|
||
public class MyArrayListTest { | ||
|
||
public static void main(String[] args) { | ||
Test(); | ||
} | ||
|
||
public static void Test(){ | ||
MyArrayList list = new MyArrayList(5); | ||
list.add("a"); | ||
list.add("b"); | ||
list.add("c"); | ||
list.add("d"); | ||
System.out.println("size=" + list.size() + "\t" +"get(3)=" + list.get(3)); //abcd | ||
list.remove(2); //abd | ||
System.out.println(list.toString()); | ||
|
||
System.out.println("size=" + list.size()); | ||
list.add(2, "e"); //abed | ||
System.out.println(list.toString()); | ||
|
||
list.add("f"); | ||
list.add("g"); //abedfg | ||
System.out.println(list.toString()); | ||
|
||
list.add(2, "h"); //abhedfg | ||
System.out.println(list.toString()); | ||
|
||
list.remove(0); //bhedfg | ||
System.out.println(list.toString()); | ||
|
||
System.out.println("get(3)=" + list.get(3)); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
group15/1517_279137987/my/collection/linearTest/MyLinkedListTest.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,32 @@ | ||
package my.collection.linearTest; | ||
|
||
import my.collection.linear.MyLinkedList; | ||
|
||
public class MyLinkedListTest { | ||
|
||
public static void main(String[] args) { | ||
MyLinkedList mll = new MyLinkedList(); | ||
mll.add("a"); | ||
mll.add("b"); | ||
mll.add("c"); //abc | ||
mll.add(1, "o"); //aobc | ||
mll.add(2, "v"); //aovbc | ||
mll.add("x"); //aovbcx | ||
mll.remove(2); //aobcx | ||
mll.remove(0); //obcx | ||
mll.addFirst("y"); //yobcx | ||
mll.addLast("m"); //yobcxm | ||
mll.removeFirst(); //obcxm | ||
mll.removeLast(); //obcx | ||
|
||
System.out.println("size = " + mll.size()); | ||
|
||
for(int i=0; i<mll.size(); i++){ | ||
System.out.print(mll.get(i) + "\t"); | ||
} | ||
|
||
System.out.println(mll.toString()); | ||
|
||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
group15/1517_279137987/my/collection/linearTest/MyQueueTest.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,25 @@ | ||
package my.collection.linearTest; | ||
|
||
import my.collection.linear.MyQueue; | ||
|
||
public class MyQueueTest { | ||
|
||
public static void main(String[] args) { | ||
MyQueue mq = new MyQueue(); | ||
|
||
System.out.println(mq.isEmpty()); | ||
|
||
mq.enQueue("a"); | ||
System.out.println(mq.isEmpty()); | ||
|
||
mq.enQueue("s"); | ||
mq.enQueue("d"); | ||
|
||
System.out.println(mq.size()); | ||
|
||
mq.deQueue(); | ||
System.out.println(mq.size()); | ||
|
||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
group15/1517_279137987/my/collection/linearTest/MyStackTest.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,24 @@ | ||
package my.collection.linearTest; | ||
|
||
import my.collection.linear.MyStack; | ||
|
||
public class MyStackTest { | ||
|
||
public static void main(String[] args) { | ||
MyStack ms = new MyStack(); | ||
|
||
System.out.println(ms.isEmpty()); | ||
ms.push("a"); | ||
System.out.println(ms.isEmpty()); | ||
ms.push("b"); //a,b | ||
System.out.println(ms.size()); | ||
ms.pop(); | ||
ms.pop(); //null | ||
System.out.println(ms.isEmpty()); | ||
ms.push("zzz"); //zzz | ||
System.out.println(ms.peek()); | ||
ms.push("yyy"); //zzz,yyy | ||
System.out.println(ms.peek()); | ||
} | ||
|
||
} |