forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 13
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 #12 from ericwang1984/master
home work
- Loading branch information
Showing
7 changed files
with
261 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,87 @@ | ||
import java.util.Arrays; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private int index =0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
public void add(Object o) { | ||
elementData[size] = o; | ||
size = size+1; | ||
|
||
} | ||
|
||
public void add(int index, Object o) { | ||
|
||
Object[] elementDataNew =null; | ||
if(size<elementData.length+1){ | ||
elementDataNew = new Object[elementData.length]; | ||
}else { | ||
elementDataNew = new Object[elementData.length+1]; | ||
} | ||
|
||
for(int i =0;i<index;i++){ | ||
elementDataNew[i]=elementData[i]; | ||
} | ||
|
||
elementDataNew[index]=o; | ||
|
||
for(int j= index+1;j<elementData.length;j++){ | ||
elementDataNew[j]=elementData[j]; | ||
} | ||
elementData = elementDataNew; | ||
size=size+1; | ||
|
||
return; | ||
|
||
} | ||
|
||
public Object get(int index) { | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index) { | ||
Object[] elementDataNew = new Object[elementData.length+1]; | ||
|
||
for(int i =0;i<index;i++){ | ||
elementDataNew[i]=elementData[i]; | ||
} | ||
|
||
for(int j= index+1;j<elementData.length;j++){ | ||
elementDataNew[j-1]=elementData[j]; | ||
} | ||
|
||
elementData = elementDataNew; | ||
size=size-1; | ||
return this; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
public Iterator iterator() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ArrayList{" + | ||
"size=" + size + | ||
", index=" + index + | ||
", elementData=" + Arrays.toString(elementData) + | ||
'}'; | ||
} | ||
|
||
public static void main(String[] args) { | ||
ArrayList arrayList = new ArrayList(); | ||
arrayList.add(1); | ||
arrayList.add(2,2); | ||
arrayList.remove(0); | ||
|
||
arrayList.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,35 @@ | ||
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,6 @@ | ||
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,51 @@ | ||
public class LinkedList implements List { | ||
|
||
private Node head; | ||
|
||
public void add(Object o) { | ||
|
||
} | ||
|
||
public void add(int index, Object o) { | ||
|
||
} | ||
|
||
public Object get(int index) { | ||
return null; | ||
} | ||
|
||
public Object remove(int index) { | ||
return null; | ||
} | ||
|
||
public int size() { | ||
return -1; | ||
} | ||
|
||
public void addFirst(Object o) { | ||
|
||
} | ||
|
||
public void addLast(Object o) { | ||
|
||
} | ||
|
||
public Object removeFirst() { | ||
return null; | ||
} | ||
|
||
public Object removeLast() { | ||
return null; | ||
} | ||
|
||
public Iterator iterator() { | ||
return null; | ||
} | ||
|
||
|
||
private static class Node { | ||
Object data; | ||
Node 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,11 @@ | ||
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,38 @@ | ||
public class Queue { | ||
|
||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
private int size =0; | ||
|
||
public void enQueue(Object o) { | ||
|
||
elementData[size]=o; | ||
size=size+1; | ||
} | ||
|
||
public Object deQueue() { | ||
|
||
Object a = elementData[0]; | ||
|
||
Object[] newElementData = new Object[100]; | ||
|
||
for (int i = 1; i < elementData.length; i++) | ||
{ | ||
newElementData[i-1]=elementData[i]; | ||
} | ||
elementData=newElementData; | ||
return a; | ||
} | ||
|
||
public boolean isEmpty() { | ||
if(size==0){ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public int size() { | ||
return elementData.length; | ||
} | ||
} |
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,33 @@ | ||
|
||
public class Stack { | ||
private Object[] elementData = new Object[100]; | ||
|
||
private int size =0; | ||
public void push(Object o) { | ||
elementData[size()]=o; | ||
size=size+1; | ||
} | ||
|
||
public Object pop() { | ||
Object a = elementData[size]; | ||
elementData[size]=null; | ||
size = size-1; | ||
return a; | ||
} | ||
|
||
public Object peek() { | ||
return elementData[size]; | ||
} | ||
|
||
public boolean isEmpty() { | ||
|
||
if(size==0){ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public int size() { | ||
return elementData.length; | ||
} | ||
} |