-
Notifications
You must be signed in to change notification settings - Fork 641
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 #11 from MysteriousAuntieCook/master
1925347167
- Loading branch information
Showing
9 changed files
with
299 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 @@ | ||
|
64 changes: 64 additions & 0 deletions
64
group01/1925347167/Week1 Basic Data Structure/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,64 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.Arrays; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
public void add(Object o){ | ||
if (fullCheck()) | ||
elementData = Arrays.copyOf(elementData, size*2); | ||
elementData[size++] = o; | ||
} | ||
public void add(int index, Object o){ | ||
|
||
if (fullCheck()) | ||
elementData = Arrays.copyOf(elementData, size*2); | ||
if (!rangeCheck(index)) | ||
throw new IndexOutOfBoundsException(); | ||
for (int i = size; i > index; --i) | ||
elementData[i] = elementData[i-1]; | ||
elementData[index] = o; | ||
size++; | ||
} | ||
|
||
public Object get(int index){ | ||
if (rangeCheck(index)) | ||
return elementData[index]; | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
|
||
public Object remove(int index){ | ||
if (!rangeCheck(index)) | ||
throw new IndexOutOfBoundsException(); | ||
Object rmo = elementData[index]; | ||
for (int i = index; i < size-1; ++i) | ||
elementData[i] = elementData[i-1]; | ||
size--; | ||
return rmo; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return null; | ||
} | ||
|
||
private boolean rangeCheck(int index) { | ||
if (index < 0 || index >= size) | ||
return false; | ||
return true; | ||
} | ||
|
||
private boolean fullCheck() { | ||
if (size >= elementData.length) | ||
return true; | ||
return false; | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
group01/1925347167/Week1 Basic Data Structure/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,38 @@ | ||
package com.coding.basic; | ||
|
||
public class BinaryTreeNode { | ||
|
||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public BinaryTreeNode(Object o) { | ||
data = o; | ||
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){ | ||
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(); | ||
|
||
} |
127 changes: 127 additions & 0 deletions
127
group01/1925347167/Week1 Basic Data Structure/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,127 @@ | ||
package com.coding.basic; | ||
|
||
public class LinkedList implements List { | ||
|
||
private Node head; | ||
|
||
private int size = 0; | ||
|
||
public void add(Object o){ | ||
Node tmp = head; | ||
while (tmp.next != null) | ||
tmp = tmp.next; | ||
|
||
Node n = new Node(o); | ||
tmp.next = n; | ||
size++; | ||
} | ||
public void add(int index , Object o){ | ||
if (!rangeCheck(index)) | ||
throw new IndexOutOfBoundsException(); | ||
|
||
if (index == 0) { | ||
Node newhead = new Node(o); | ||
newhead.next = head; | ||
head = newhead; | ||
} else { | ||
Node tmp = head; | ||
for (int i = 0; i < index - 1; ++i) | ||
tmp = tmp.next; | ||
Node node = new Node(o); | ||
node.next = tmp.next; | ||
tmp.next = node; | ||
} | ||
|
||
size++; | ||
} | ||
public Object get(int index){ | ||
if (!rangeCheck(index)) | ||
throw new IndexOutOfBoundsException(); | ||
Node tmp = head; | ||
for (int i = 0; i < index - 1; ++i) | ||
tmp = tmp.next; | ||
return tmp.data; | ||
|
||
} | ||
|
||
public Object remove(int index){ | ||
if (!rangeCheck(index)) | ||
throw new IndexOutOfBoundsException(); | ||
|
||
if (index == 0) { | ||
Node oldHead= head; | ||
head = head.next; | ||
size--; | ||
return oldHead.data; | ||
}else { | ||
Node tmp = head; | ||
for (int i = 0; i < index - 1; i++) { | ||
tmp = tmp.next; | ||
} | ||
Node node = tmp.next; | ||
tmp.next = node.next; | ||
size--; | ||
return node.data; | ||
} | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public void addFirst(Object o){ | ||
Node newHead = new Node(o); | ||
newHead.next = head; | ||
head = newHead; | ||
size++; | ||
} | ||
public void addLast(Object o){ | ||
Node tmp = head; | ||
while (tmp.next != null) { | ||
tmp = tmp.next; | ||
} | ||
Node node = new Node(o); | ||
tmp.next = node; | ||
size++; | ||
} | ||
public Object removeFirst(){ | ||
if (head == null) | ||
throw new IndexOutOfBoundsException(); | ||
Node oldHead = head; | ||
head = head.next; | ||
size--; | ||
return oldHead.data; | ||
} | ||
public Object removeLast(){ | ||
if (head == null) | ||
throw new IndexOutOfBoundsException(); | ||
Node tmp = head; | ||
while (tmp.next.next != null) { | ||
tmp = tmp.next; | ||
} | ||
Node node = tmp.next; | ||
tmp.next = null; | ||
size--; | ||
return node.data; | ||
} | ||
public Iterator iterator(){ | ||
return null; | ||
} | ||
|
||
private boolean rangeCheck(int index) { | ||
if (index < 0 || index >= size) | ||
return false; | ||
return true; | ||
} | ||
|
||
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,24 @@ | ||
package com.coding.basic; | ||
|
||
public class Queue { | ||
|
||
private LinkedList llist = new LinkedList(); | ||
|
||
public void enQueue(Object o){ | ||
llist.add(o); | ||
} | ||
|
||
public Object deQueue(){ | ||
if (isEmpty()) | ||
return null; | ||
return llist.removeFirst(); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return (llist.size()==0); | ||
} | ||
|
||
public int size(){ | ||
return -llist.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,28 @@ | ||
package com.coding.basic; | ||
|
||
public class Stack { | ||
|
||
private ArrayList elementData = new ArrayList(); | ||
|
||
public void push(Object o){ | ||
elementData.add(o); | ||
} | ||
|
||
public Object pop(){ | ||
if (isEmpty()) | ||
return null; | ||
return elementData.remove(elementData.size() - 1); | ||
} | ||
|
||
public Object peek(){ | ||
if (elementData.size() == 0) | ||
return null; | ||
return elementData.get(elementData.size() - 1); | ||
} | ||
public boolean isEmpty(){ | ||
return (elementData.size() == 0); | ||
} | ||
public int size(){ | ||
return elementData.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 @@ | ||
|