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 sargeles/master
只写了linkedlist和arraylist的简单实现,这周实在太忙,要上线T T
- Loading branch information
Showing
10 changed files
with
302 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,5 @@ | ||
/bin/ | ||
|
||
#ide config | ||
.settings/ | ||
|
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>2017Learning</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,62 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.Arrays; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[10]; | ||
|
||
public void ensureCapacity(int input) { | ||
if (input > elementData.length) { | ||
growCapacity(); | ||
} | ||
} | ||
|
||
private void growCapacity(){ | ||
elementData = Arrays.copyOf(elementData, size * 2); | ||
} | ||
|
||
public void add(Object o){ | ||
ensureCapacity(size + 1); | ||
elementData[size++] = o; | ||
} | ||
|
||
|
||
public void add(int index, Object o){ | ||
rangeCheck(index); | ||
ensureCapacity(size + 1); | ||
System.arraycopy(elementData,index, elementData, index + 1, size - index); | ||
elementData[index] = o; | ||
size ++; | ||
} | ||
|
||
private void rangeCheck(int index){ | ||
if (index > size || index < 0){ | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
} | ||
|
||
public Object get(int index){ | ||
rangeCheck(index); | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index){ | ||
rangeCheck(index); | ||
Object dest = elementData[index]; | ||
System.arraycopy(elementData, index +1, elementData, index, size-index-1); | ||
size --; | ||
return dest; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return null; | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
group24/330657387/src/com/coding/basic/BinaryTreeNode.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 com.coding.basic; | ||
|
||
public class BinaryTreeNode { | ||
|
||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public Object getData() { | ||
return data; | ||
} | ||
public void setData(Object data) { | ||
this.data = data; | ||
} | ||
public BinaryTreeNode getLeft() { | ||
return left; | ||
} | ||
public void setLeft(BinaryTreeNode left) { | ||
this.left = left; | ||
} | ||
public BinaryTreeNode getRight() { | ||
return right; | ||
} | ||
public void setRight(BinaryTreeNode right) { | ||
this.right = right; | ||
} | ||
|
||
public BinaryTreeNode insert(Object o){ | ||
return 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,7 @@ | ||
package com.coding.basic; | ||
|
||
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,122 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
public class LinkedList implements List { | ||
|
||
private Node head; | ||
private int size = 0; | ||
|
||
public void add(Object o) { | ||
if (isEmpty()) { | ||
head = new Node(o); | ||
} else { | ||
Node tail = (Node)get(size-1); | ||
Node node = new Node(o); | ||
tail.next = node; | ||
} | ||
size++; | ||
} | ||
|
||
public boolean isEmpty() { | ||
return (size == 0) ? true : false; | ||
} | ||
|
||
public void add(int index, Object o) { | ||
rangeCheck(index); | ||
if (index ==0) { | ||
Node node = new Node(o); | ||
node.next = head; | ||
head = node; | ||
} else { | ||
Node pre = (Node)get(index-1); | ||
Node node = new Node(o); | ||
node.next = pre.next; | ||
pre.next = node; | ||
} | ||
} | ||
|
||
private void rangeCheck(int index){ | ||
if (index >= size || index <0){ | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
} | ||
|
||
public Object get(int index) { | ||
rangeCheck(index); | ||
Node dest = head; | ||
for (int i = 0; i< index; i++){ | ||
dest = dest.next; | ||
} | ||
return dest.data; | ||
} | ||
|
||
public Object remove(int index) { | ||
rangeCheck(index); | ||
Node pre = (Node)get(index); | ||
Node dest = pre.next; | ||
pre.next = dest.next; | ||
size --; | ||
return dest; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
public void addFirst(Object o) { | ||
Node node = new Node(o); | ||
node.next = head; | ||
head = node; | ||
size ++; | ||
} | ||
|
||
public void addLast(Object o) { | ||
Node last = (Node)get(size-1); | ||
Node node = new Node(o); | ||
last.next = node; | ||
size++; | ||
} | ||
|
||
public Object removeFirst() { | ||
if (head == null) { | ||
throw new NoSuchElementException(); | ||
} | ||
Node newhead = head; | ||
head = head.next; | ||
size --; | ||
return newhead; | ||
} | ||
|
||
public Object removeLast() { | ||
if (head == null) { | ||
throw new NoSuchElementException(); | ||
} | ||
if (head.next == null) { | ||
Node tmp = head; | ||
head = null; | ||
size --; | ||
return tmp; | ||
} | ||
Node newLastNode = (Node)get(size-2); | ||
Node oldLastNode = newLastNode.next; | ||
newLastNode.next = null; | ||
size --; | ||
return oldLastNode; | ||
} | ||
|
||
public Iterator iterator() { | ||
return null; | ||
} | ||
|
||
private static class Node { | ||
Object data; | ||
Node next; | ||
|
||
Node(Object data) { | ||
this.data = data; | ||
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,9 @@ | ||
package com.coding.basic; | ||
|
||
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,19 @@ | ||
package com.coding.basic; | ||
|
||
public class Queue { | ||
|
||
public void enQueue(Object o){ | ||
} | ||
|
||
public Object deQueue(){ | ||
return null; | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return false; | ||
} | ||
|
||
public int size(){ | ||
return -1; | ||
} | ||
} |
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.coding.basic; | ||
|
||
public class Stack { | ||
private ArrayList elementData = new ArrayList(); | ||
|
||
public void push(Object o){ | ||
} | ||
|
||
public Object pop(){ | ||
return null; | ||
} | ||
|
||
public Object peek(){ | ||
return null; | ||
} | ||
public boolean isEmpty(){ | ||
return false; | ||
} | ||
public int size(){ | ||
return -1; | ||
} | ||
} |