forked from diliuzuzhanghao/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
390 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,99 @@ | ||
import java.util.Arrays; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
private int length=3; | ||
private Object[] elementData = new Object[length]; | ||
|
||
public void add(Object o){ | ||
if(size>=length){ | ||
grow(100); | ||
} | ||
elementData[size]=o; | ||
size++; | ||
} | ||
public void add(int index, Object o){ | ||
size++; | ||
if(size>=length){ | ||
grow(100); | ||
} | ||
System.arraycopy(elementData,index,elementData,index+1,size-index-1); | ||
elementData[index]=o; | ||
} | ||
|
||
public Object get(int index){ | ||
if(index<size) | ||
return elementData[index]; | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
|
||
public Object remove(int index){ | ||
//越界 | ||
if(index>=size) | ||
throw new IndexOutOfBoundsException(); | ||
size--; | ||
Object a=elementData[index]; | ||
//刚好最后一个 | ||
if (index+1==size){ | ||
return a; | ||
} | ||
System.arraycopy(elementData,index+1,elementData,index,size); | ||
return a; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return new ArrayListIterator(); | ||
} | ||
private class ArrayListIterator implements Iterator{ | ||
private int index=0; | ||
@Override | ||
public boolean hasNext() { | ||
if(index+1<size){ | ||
index++; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
return elementData[index]; | ||
} | ||
} | ||
private void grow(int increase){ | ||
//return Arrays.copyOf(src,src.length+size); | ||
Object[] target=new Object[length+increase]; | ||
System.arraycopy(elementData,0,target,0,length); | ||
elementData= target; | ||
length=length+increase; | ||
} | ||
public String toString(){ | ||
return Arrays.toString(Arrays.copyOf(elementData,size)); | ||
} | ||
public static void main(String[] arg){ | ||
ArrayList a=new ArrayList(); | ||
a.add(0); | ||
a.add(1); | ||
a.add("2"); | ||
a.add("3"); | ||
a.add("4"); | ||
a.add("5"); | ||
a.add("six"); | ||
a.add("七"); | ||
a.add(8); | ||
Iterator iterator=a.iterator(); | ||
while (iterator.hasNext()) | ||
System.out.println(iterator.next()); | ||
/*System.out.println(a.remove(3)); | ||
System.out.println(a.size()); | ||
System.out.println(a);*/ | ||
|
||
//System.out.println(a.get(3)); | ||
//System.out.println(a.get(99)); | ||
} | ||
} |
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,5 @@ | ||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
182 changes: 182 additions & 0 deletions
182
group15/1503_1311822904/myCollection/src/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,182 @@ | ||
public class LinkedList implements List { | ||
private int size = 0; | ||
private Node head=new Node(null); | ||
|
||
public void add(Object o){ | ||
if(head.data==null){ | ||
head.data=o; | ||
return; | ||
} | ||
Node n=head; | ||
while (n.next!=null){ | ||
n=n.next; | ||
} | ||
n.next=new Node(o); | ||
size++; | ||
} | ||
public void add(int index , Object o){ | ||
Node n=getNode(index); | ||
Node newN=new Node(o); | ||
newN.next=n.next; | ||
n.next=newN; | ||
size++; | ||
} | ||
public Object get(int index){ | ||
return getNode(index).data; | ||
} | ||
private Node getNode(int index){ | ||
Node n=head; | ||
for (int i=0;i<index;i++){ | ||
if(n==null) | ||
throw new IndexOutOfBoundsException(); | ||
n= n.next; | ||
} | ||
return n; | ||
} | ||
public Object remove(int index){ | ||
Object o; | ||
if (index==0){ | ||
o=head.data; | ||
head.data=null; | ||
size--; | ||
return o; | ||
} | ||
Node n=getNode(index-1); | ||
o=n.next.data; | ||
n.next=n.next.next; | ||
size--; | ||
return o; | ||
} | ||
|
||
public int size(){ | ||
|
||
return size; | ||
} | ||
|
||
public void addFirst(Object o){ | ||
size++; | ||
if(head.data==null){ | ||
head.data=o; | ||
return; | ||
} | ||
Node n=new Node(o); | ||
n.next=head; | ||
head=n; | ||
|
||
} | ||
public void addLast(Object o){ | ||
size++; | ||
//要是还没有值 就是头 | ||
if (head.data==null){ | ||
head.data=o; | ||
} | ||
Node n=head; | ||
//找到最后一个 | ||
while (n.next!=null){ | ||
n=n.next; | ||
} | ||
//接上去 | ||
n.next=new Node(o); | ||
|
||
} | ||
public Object removeFirst(){ | ||
//没有头 | ||
if(head.data==null) | ||
throw new IndexOutOfBoundsException(); | ||
Object o=head.data; | ||
head=head.next; | ||
size--; | ||
return o; | ||
} | ||
public Object removeLast(){ | ||
//还没有值 异常 | ||
if (head.data==null){ | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
size--; | ||
Object o; | ||
//只有头有值 | ||
if(head.next==null){ | ||
o=head.data; | ||
head.data=o; | ||
return o; | ||
} | ||
Node n=head; | ||
//找到最后第二个 | ||
while (n.next.next!=null) | ||
n=n.next; | ||
//拿到最后一个值 | ||
o=n.next.data; | ||
//去掉最后一个节点 | ||
n.next=null; | ||
return o; | ||
} | ||
public Iterator iterator(){ | ||
return new LinkedIterator(); | ||
} | ||
private class LinkedIterator implements Iterator{ | ||
private Node n=head; | ||
@Override | ||
public boolean hasNext() { | ||
if(n!=null&&n.next!=null){ | ||
n=n.next; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
return n.data; | ||
} | ||
} | ||
|
||
private static class Node{ | ||
Object data; | ||
Node next; | ||
Node(Object o){ | ||
data=o; | ||
} | ||
|
||
|
||
} | ||
|
||
public String toString(){ | ||
String s="["; | ||
for (int i=0;i<size;i++) | ||
s+=get(i)+", "; | ||
s+="]"; | ||
return s; | ||
} | ||
|
||
public static void main(String[] arg){ | ||
LinkedList a=new LinkedList(); | ||
// a.removeFirst(); | ||
a.addFirst("first"); | ||
a.addLast("ll"); | ||
a.add(0); | ||
a.add(1); | ||
a.add("2"); | ||
a.add("3"); | ||
a.add("4"); | ||
a.add("5"); | ||
a.add("six"); | ||
a.add("七"); | ||
a.add(8); | ||
System.out.println(a); | ||
Iterator iterator=a.iterator(); | ||
while (iterator.hasNext()) | ||
System.out.println(iterator.next()); | ||
/*System.out.println(a.size); | ||
System.out.println(a); | ||
System.out.println(a.remove(3)); | ||
System.out.println(a.remove(3)); | ||
a.removeFirst(); | ||
a.removeLast(); | ||
System.out.println(a);*/ | ||
|
||
|
||
//System.out.println(a.get(3)); | ||
//System.out.println(a.get(99)); | ||
} | ||
} |
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 @@ | ||
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(); | ||
} |
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 @@ | ||
public class Queue { | ||
private LinkedList elementData = new LinkedList(); | ||
public void enQueue(Object o){ | ||
elementData.add(o); | ||
} | ||
|
||
public Object deQueue(){ | ||
return elementData.remove(0); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return elementData.size()==0; | ||
} | ||
|
||
public int size(){ | ||
return elementData.size(); | ||
} | ||
|
||
public String toString(){ | ||
return elementData.toString(); | ||
} | ||
public static void main(String[] arg){ | ||
Queue a=new Queue(); | ||
a.enQueue(0); | ||
a.enQueue(1); | ||
a.enQueue("2"); | ||
a.enQueue("3"); | ||
a.enQueue("4"); | ||
System.out.println(a); | ||
System.out.println(a.deQueue()); | ||
System.out.println(a.deQueue()); | ||
System.out.println(a); | ||
System.out.println(a.deQueue()); | ||
System.out.println(a.deQueue()); | ||
System.out.println(a.deQueue()); | ||
System.out.println(a); | ||
System.out.println(a.size()); | ||
System.out.println(a.isEmpty()); | ||
|
||
|
||
//System.out.println(a.get(3)); | ||
//System.out.println(a.get(99)); | ||
} | ||
} |
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,53 @@ | ||
public class Stack { | ||
private LinkedList elementData = new LinkedList(); | ||
|
||
public void push(Object o){ | ||
elementData.add(o); | ||
} | ||
|
||
public Object pop(){ | ||
return elementData.remove(elementData.size()-1); | ||
|
||
} | ||
|
||
public Object peek(){ | ||
return elementData.get(elementData.size()-1); | ||
|
||
} | ||
public boolean isEmpty(){ | ||
if (elementData.size()==0) | ||
return true; | ||
return false; | ||
} | ||
public int size(){ | ||
return elementData.size(); | ||
} | ||
|
||
public String toString(){ | ||
return elementData.toString(); | ||
} | ||
|
||
public static void main(String[] arg){ | ||
Stack a=new Stack(); | ||
a.push(0); | ||
a.push(1); | ||
a.push("2"); | ||
a.push("3"); | ||
System.out.println(a); | ||
System.out.println(a.peek()); | ||
System.out.println(a.pop()); | ||
System.out.println(a); | ||
System.out.println(a.size()); | ||
System.out.println(a.isEmpty()); | ||
|
||
System.out.println(a.pop()); | ||
System.out.println(a.pop()); | ||
System.out.println(a.pop()); | ||
System.out.println(a.isEmpty()); | ||
System.out.println(a.peek()); | ||
System.out.println(a.pop()); | ||
System.out.println(a.isEmpty()); | ||
|
||
|
||
} | ||
} |