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 joy32812/master
homework for 542087872
- Loading branch information
Showing
8 changed files
with
571 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,88 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.Arrays; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[10]; | ||
|
||
// 每次乘2增长 | ||
private void grow() { | ||
elementData = Arrays.copyOf(elementData, elementData.length * 2); | ||
} | ||
|
||
|
||
public void add(Object o){ | ||
if (size >= elementData.length) { | ||
this.grow(); | ||
} | ||
|
||
elementData[size++] = o; | ||
} | ||
public void add(int index, Object o){ | ||
if (size >= elementData.length) { | ||
this.grow(); | ||
} | ||
System.arraycopy(elementData, index, elementData, index + 1, size - index); | ||
|
||
elementData[index] = o; | ||
size++; | ||
} | ||
|
||
public Object get(int index){ | ||
if (index >= size) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index){ | ||
if (index >= size) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
|
||
Object el = elementData[index]; | ||
System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); | ||
|
||
size--; | ||
return el; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
private class ArrIter implements Iterator { | ||
int cursor = 0; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return cursor < size; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
return elementData[cursor++]; | ||
} | ||
} | ||
|
||
public Iterator iterator(){ | ||
return new ArrIter(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("["); | ||
for (int i = 0; i < size; i++) { | ||
sb.append(elementData[i]); | ||
if (i < size - 1) { | ||
sb.append(","); | ||
} | ||
} | ||
sb.append("]"); | ||
return sb.toString(); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
group16/542087872/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,62 @@ | ||
package com.coding.basic; | ||
|
||
public class BinaryTreeNode { | ||
|
||
private int data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public int getData() { | ||
return data; | ||
} | ||
public void setData(int 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(int data) { | ||
this.data = data; | ||
} | ||
|
||
private BinaryTreeNode insertAt(BinaryTreeNode node, int o) { | ||
if (o < node.getData()) { | ||
if (node.getLeft() != null) { | ||
return insertAt(node.getLeft(), o); | ||
} else { | ||
BinaryTreeNode nowNode = new BinaryTreeNode(o); | ||
node.setLeft(nowNode); | ||
|
||
return nowNode; | ||
} | ||
} else { | ||
if (node.getRight() != null) { | ||
return insertAt(node.getRight(), o); | ||
} else { | ||
BinaryTreeNode nowNode = new BinaryTreeNode(o); | ||
node.setRight(nowNode); | ||
return nowNode; | ||
} | ||
} | ||
} | ||
|
||
public BinaryTreeNode insert(int o){ | ||
return insertAt(this, o); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "data: " + data; | ||
} | ||
} |
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,192 @@ | ||
package com.coding.basic; | ||
|
||
public class LinkedList implements List { | ||
|
||
private Node head; | ||
private Node tail; | ||
|
||
public void add(Object o){ | ||
Node nowNode = new Node(o); | ||
if (head == null) { | ||
head = nowNode; | ||
} else { | ||
tail.next = nowNode; | ||
} | ||
tail = nowNode; | ||
} | ||
public void add(int index , Object o){ | ||
int count = 0; | ||
Node lastOne = null; | ||
Node tpHead = head; | ||
while (tpHead != null && count != index) { | ||
count++; | ||
lastOne = tpHead; | ||
tpHead = tpHead.next; | ||
} | ||
if (count != index) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
|
||
|
||
Node nowNode = new Node(o); | ||
if (lastOne == null) { | ||
head = nowNode; | ||
head.next = tpHead; | ||
} else { | ||
lastOne.next = nowNode; | ||
nowNode.next = tpHead; | ||
} | ||
} | ||
public Object get(int index){ | ||
int count = 0; | ||
Node tpHead = head; | ||
while (tpHead != null && count != index) { | ||
count++; | ||
tpHead = tpHead.next; | ||
} | ||
if (count != index) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
|
||
return tpHead.data; | ||
} | ||
public Object remove(int index){ | ||
int count = 0; | ||
Node lastOne = null; | ||
Node tpHead = head; | ||
while (tpHead != null && count != index) { | ||
count++; | ||
lastOne = tpHead; | ||
tpHead = tpHead.next; | ||
} | ||
if (count != index) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
|
||
if (lastOne == null) { | ||
head = tpHead.next; | ||
} else { | ||
lastOne.next = tpHead.next; | ||
} | ||
|
||
if (tpHead.next == null) { | ||
tail = lastOne; | ||
} | ||
|
||
return tpHead.data; | ||
} | ||
|
||
public int size(){ | ||
int count = 0; | ||
Node tpHead = head; | ||
while (tpHead != null) { | ||
count ++; | ||
tpHead = tpHead.next; | ||
} | ||
|
||
return count; | ||
} | ||
|
||
public void addFirst(Object o){ | ||
Node nowNode = new Node(o); | ||
if (head == null) { | ||
head = nowNode; | ||
tail = nowNode; | ||
} else { | ||
nowNode.next = head; | ||
head = nowNode; | ||
} | ||
} | ||
public void addLast(Object o){ | ||
Node nowNode = new Node(o); | ||
if (head == null) { | ||
head = nowNode; | ||
tail = nowNode; | ||
} else { | ||
tail.next = nowNode; | ||
tail = nowNode; | ||
} | ||
} | ||
public Object removeFirst(){ | ||
if (head == null) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
|
||
Node nowValue = head; | ||
|
||
Node nextNode = head.next; | ||
if (nextNode == null) { | ||
tail = null; | ||
} | ||
head = nextNode; | ||
|
||
return nowValue.data; | ||
} | ||
public Object removeLast(){ | ||
if (head == null) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
|
||
Node nowValue = tail; | ||
|
||
Node lastOne = null; | ||
Node tpHead = head; | ||
while (tpHead != tail) { | ||
lastOne = tpHead; | ||
tpHead = tpHead.next; | ||
} | ||
if (lastOne == null) { | ||
head = null; | ||
} else { | ||
lastOne.next = null; | ||
} | ||
tail = lastOne; | ||
|
||
return nowValue.data; | ||
} | ||
|
||
private class LinkIter implements Iterator { | ||
|
||
Node cursor = head; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return cursor != null; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
Node ret = cursor; | ||
cursor = cursor.next; | ||
return ret.data; | ||
} | ||
} | ||
|
||
public Iterator iterator(){ | ||
return new LinkIter(); | ||
} | ||
|
||
|
||
private static class Node{ | ||
Object data; | ||
Node next; | ||
|
||
public Node(Object data) { | ||
this.data = data; | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
Node tpHead = head; | ||
StringBuilder sb = new StringBuilder("["); | ||
while (tpHead != null) { | ||
sb.append(tpHead.data); | ||
sb.append(","); | ||
tpHead = tpHead.next; | ||
} | ||
sb.append("]"); | ||
return sb.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,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,22 @@ | ||
package com.coding.basic; | ||
|
||
public class Queue { | ||
|
||
private LinkedList linkedList = new LinkedList(); | ||
|
||
public void enQueue(Object o){ | ||
linkedList.addLast(o); | ||
} | ||
|
||
public Object deQueue(){ | ||
return linkedList.removeFirst(); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return linkedList.size() == 0; | ||
} | ||
|
||
public int size(){ | ||
return linkedList.size(); | ||
} | ||
} |
Oops, something went wrong.