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 #9 from congcongcong250/master
609990377_liren:DataStructure version 1
- Loading branch information
Showing
8 changed files
with
426 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,3 @@ | ||
/bin/ | ||
.classpath | ||
.project |
113 changes: 113 additions & 0 deletions
113
...02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayList.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,113 @@ | ||
package com.github.congcongcong250.coding2017.basic; | ||
|
||
import java.util.Arrays; | ||
import java.util.InputMismatchException; | ||
import java.util.NoSuchElementException; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[10]; | ||
|
||
public void add(Object o){ | ||
|
||
//check size limit | ||
if(size + 1 > elementData.length){ | ||
int newlength = elementData.length * 3 / 2 + 1; | ||
elementData = Arrays.copyOf(elementData, newlength); | ||
} | ||
|
||
elementData[size++] = o; | ||
} | ||
|
||
public void add(int index, Object o){ | ||
//Check Object class | ||
if(size >= 1){ | ||
if(o.getClass() != elementData[0].getClass()) | ||
throw new InputMismatchException("Index:"+index+" Size:"+size); | ||
} | ||
|
||
//index Check | ||
if(index >= size || index < 0){ | ||
throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); | ||
} | ||
|
||
//check size limit | ||
if(size + 1 > elementData.length){ | ||
Object oldData[] = elementData; | ||
int newlength = elementData.length * 3 / 2 + 1; | ||
elementData = Arrays.copyOf(elementData, newlength); | ||
} | ||
|
||
for(int i = size-1; i >= index; i-- ){ | ||
elementData[i] = elementData[i-1]; | ||
} | ||
|
||
elementData[index] = o; | ||
|
||
} | ||
|
||
public Object get(int index){ | ||
//index Check | ||
if(index >= size || index < 0){ | ||
throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); | ||
} | ||
|
||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index){ | ||
//index Check | ||
if(index >= size || index < 0){ | ||
throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); | ||
} | ||
|
||
Object old = elementData[index]; | ||
for(int i = index; i < size-1 ; i++ ){ | ||
elementData[i] = elementData[i+1]; | ||
} | ||
elementData[--size] = null; | ||
return old; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return new Itr(); | ||
} | ||
|
||
private class Itr implements Iterator{ | ||
//index for next element to visit | ||
private int cursor = 0; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return cursor != size; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
if(cursor >= size){ | ||
throw new NoSuchElementException(); | ||
} | ||
return elementData[cursor++]; | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
//Check bound | ||
if(cursor == 0){ | ||
throw new NoSuchElementException(); | ||
} | ||
|
||
ArrayList.this.remove(--cursor); | ||
|
||
} | ||
|
||
|
||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
...9990377/DataStructure/src/com/github/congcongcong250/coding2017/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,72 @@ | ||
package com.github.congcongcong250.coding2017.basic; | ||
|
||
public class BinaryTreeNode <Object extends Comparable<Object>>{ | ||
|
||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public BinaryTreeNode(){ | ||
data = null; | ||
left = null; | ||
right = null; | ||
} | ||
|
||
public BinaryTreeNode(Object obj){ | ||
data = obj; | ||
left = null; | ||
right = null; | ||
} | ||
|
||
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){ | ||
//If is empty root | ||
if(data == null){ | ||
data = o; | ||
return this; | ||
} | ||
|
||
//If it is a normal root | ||
BinaryTreeNode in; | ||
|
||
if(o.compareTo(data) <= 0){ | ||
if(left == null){ | ||
in = new BinaryTreeNode(o); | ||
left = in; | ||
}else{ | ||
in = left.insert(o); | ||
} | ||
}else{ | ||
if(right == null){ | ||
in = new BinaryTreeNode(o); | ||
right = in; | ||
}else{ | ||
in = right.insert(o); | ||
} | ||
} | ||
|
||
assert (in == null):"Insert error"; | ||
return in; | ||
} | ||
|
||
|
||
|
||
} |
8 changes: 8 additions & 0 deletions
8
...p02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Iterator.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,8 @@ | ||
package com.github.congcongcong250.coding2017.basic; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
public void remove(); | ||
|
||
} |
163 changes: 163 additions & 0 deletions
163
...2/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/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,163 @@ | ||
package com.github.congcongcong250.coding2017.basic; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
public class LinkedList implements List { | ||
|
||
private Node head; | ||
private int size; | ||
|
||
public LinkedList(){ | ||
head.next = head; | ||
head.previous = head; | ||
size = 0; | ||
} | ||
|
||
public void add(Object o){ | ||
addLast(o); | ||
} | ||
|
||
public void add(int index , Object o){ | ||
//Check bound | ||
if(index >= size){ | ||
throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); | ||
} | ||
|
||
Node nx = this.find(index); | ||
Node pr = nx.previous; | ||
Node in = new Node(o,pr,nx); | ||
nx.previous = in; | ||
pr.next = in; | ||
size++; | ||
} | ||
|
||
public Object get(int index){ | ||
//Check bound | ||
if(index >= size){ | ||
throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); | ||
} | ||
return this.find(index); | ||
} | ||
|
||
public Object remove(int index){ | ||
//Check bound | ||
if(index >= size){ | ||
throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); | ||
} | ||
Node rem = this.find(index); | ||
|
||
Node pr = rem.previous; | ||
Node nx = rem.next; | ||
pr.next = nx; | ||
nx.previous = pr; | ||
|
||
Object ret = rem.data; | ||
rem.previous = null; | ||
rem.next = null; | ||
rem.data = null; | ||
size--; | ||
return ret; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public void addFirst(Object o){ | ||
Node nx = head.next; | ||
Node in = new Node(o,head, nx); | ||
head.next = in; | ||
nx.previous = in; | ||
size++; | ||
} | ||
|
||
public void addLast(Object o){ | ||
Node last = head.previous; | ||
Node in = new Node(o,last,head); | ||
last.next = in; | ||
head.previous = in; | ||
|
||
size++; | ||
} | ||
public Object removeFirst(){ | ||
return remove(0); | ||
} | ||
public Object removeLast(){ | ||
return remove(size-1); | ||
} | ||
public Iterator iterator(){ | ||
return new ListItr(); | ||
} | ||
|
||
private Node find(int index){ | ||
Node tra = head; | ||
|
||
//If index < size/2 | ||
if( index < (size >> 1)){ | ||
for(int i = 0; i <= index; i++){ | ||
tra = tra.next; | ||
} | ||
}else{ | ||
for(int i = size; i >= index; i--){ | ||
tra = tra.previous; | ||
} | ||
} | ||
return tra; | ||
} | ||
|
||
private static class Node{ | ||
Object data; | ||
Node next; | ||
Node previous; | ||
|
||
public Node(Object obj,Node pre, Node nx){ | ||
data = obj; | ||
next = nx; | ||
previous = pre; | ||
} | ||
|
||
|
||
} | ||
|
||
private class ListItr implements Iterator{ | ||
//Point to next node | ||
Node cursor; | ||
int nextIndex; | ||
|
||
public ListItr(){ | ||
cursor = head.next; | ||
nextIndex = 0; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return nextIndex < size; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
Node re = cursor; | ||
cursor = cursor.next; | ||
nextIndex++; | ||
return re; | ||
} | ||
|
||
public Object previous() { | ||
Node re = cursor.previous.previous; | ||
cursor = cursor.previous; | ||
nextIndex--; | ||
return re; | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
//Check bound | ||
if(nextIndex > size){ | ||
throw new NoSuchElementException("Iterates to the end"); | ||
} | ||
LinkedList.this.remove(--nextIndex); | ||
|
||
} | ||
|
||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/List.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,9 @@ | ||
package com.github.congcongcong250.coding2017.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(); | ||
} |
Oops, something went wrong.