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 #2 from shlugood/master
这是第一次作业的代码部分。
- Loading branch information
Showing
9 changed files
with
387 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,106 @@ | ||
package com.coding.basic; | ||
|
||
public class ArrayList implements List, Iterator { | ||
|
||
private int size; | ||
private Object[] data; | ||
public ArrayList() { | ||
data = new Object[10]; | ||
} | ||
|
||
@Override | ||
public void add(Object o) { | ||
size = size(); | ||
if(data.length <= size){ | ||
grow(); | ||
} | ||
data[size] = o; | ||
size++; | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see dataStructure.List#add(java.lang.Object, int) | ||
* 在第index元素前插入元素 | ||
*/ | ||
@Override | ||
public void add(int index, Object o){ | ||
if (index >= size()){ | ||
return; | ||
} | ||
size = size(); | ||
if(data.length <= size){ | ||
grow(); | ||
} | ||
for(int i = size , len = size - index; i < len; i -- ){ | ||
data[i] = data[i -1]; | ||
} | ||
data[index] = o; | ||
size++; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
return data[index]; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
if (index >= size()){ | ||
return null; | ||
}else{ | ||
Object o = data[index]; | ||
for(int i = index; i < size; i ++){ | ||
data[i] = data[i + 1]; | ||
} | ||
data[size] = null; | ||
size--; | ||
return o; | ||
} | ||
|
||
} | ||
|
||
private void grow(){ | ||
size = size(); | ||
int length = 0; | ||
if(size < 10000){ | ||
length = size * 2; | ||
}else{ | ||
length = (int)(size * 1.5); | ||
} | ||
size = length; | ||
|
||
Object[] temData = new Object[length]; | ||
for(int i = 0, j = data.length; i < j; i ++){ | ||
temData[i] = data[i]; | ||
} | ||
data = temData; | ||
} | ||
|
||
private int index = 0; | ||
@Override | ||
public boolean hasNext() { | ||
return index < size; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
index++; | ||
return data[index - 1]; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder("["); | ||
for(int i = 0; i < size; i++){ | ||
sb.append(data[i].toString() + ","); | ||
} | ||
sb.append("]"); | ||
return sb.toString(); | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
group19/972815123/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,52 @@ | ||
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){ | ||
Comparable co = (Comparable)o; | ||
Comparable coData = (Comparable)data; | ||
BinaryTreeNode result = null; | ||
if(co.compareTo(data) > 0){ | ||
if(null == right){ | ||
right = new BinaryTreeNode(); | ||
right.data = o; | ||
result = right; | ||
return right; | ||
}else{ | ||
right.insert(o); | ||
} | ||
}else{ | ||
if(null == left){ | ||
left = new BinaryTreeNode(); | ||
left.data = o; | ||
result = left; | ||
return left; | ||
}else{ | ||
left.insert(o); | ||
} | ||
} | ||
return result; | ||
} | ||
} |
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 @@ | ||
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,134 @@ | ||
package com.coding.basic; | ||
|
||
public class LinkedList implements List,Iterator { | ||
|
||
private Node head; | ||
private Node last; | ||
private int size = 0; | ||
|
||
public LinkedList() { | ||
head = new Node(); | ||
} | ||
|
||
@Override | ||
public void add(Object o) { | ||
Node newNode = new Node(); | ||
Node last = head; | ||
while(last.next != null){ | ||
last = last.next; | ||
} | ||
last.next = newNode; | ||
newNode.prev = last; | ||
last = newNode; | ||
size++; | ||
} | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
Node newNode = new Node(); | ||
Node indexNode = head ; | ||
int i = 0; | ||
while(i == index){ | ||
indexNode = indexNode.next; | ||
i++; | ||
} | ||
Node indexNextNode = indexNode.next; | ||
indexNode.next = newNode; | ||
newNode.prev = indexNode; | ||
newNode.next = indexNextNode; | ||
indexNextNode.prev = newNode; | ||
size ++; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
Node indexNode = head; | ||
int i = 0; | ||
while(i == index){ | ||
indexNode = indexNode.next; | ||
i++; | ||
} | ||
return indexNode; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
Node indexNode = head ; | ||
int i = 0; | ||
while(i == index){ | ||
|
||
indexNode = indexNode.next; | ||
i++; | ||
} | ||
Object o = indexNode.prev; | ||
Node indexNextNode = indexNode.next; | ||
Node indexPrevNode = indexNode.prev; | ||
|
||
indexNextNode.prev = indexPrevNode; | ||
indexPrevNode.next = indexNextNode; | ||
|
||
indexNode.next = null; | ||
indexNode.prev = null; | ||
size--; | ||
return o; | ||
} | ||
|
||
public void addFirst(Object o){ | ||
Node newNode = new Node(); | ||
newNode.data = o; | ||
newNode.next = head; | ||
head.prev = newNode; | ||
head = newNode; | ||
size ++; | ||
} | ||
public void addLast(Object o){ | ||
Node newNode = new Node(); | ||
newNode.data = o; | ||
newNode.prev = last; | ||
last.next = newNode; | ||
last = newNode; | ||
size ++; | ||
} | ||
public Object removeFirst(){ | ||
Node ret = head; | ||
head = head.next; | ||
head.prev = null; | ||
size--; | ||
return ret; | ||
} | ||
public Object removeLast(){ | ||
Node ret = last; | ||
last = last.prev; | ||
last.next = null; | ||
size--; | ||
return ret; | ||
} | ||
public Iterator iterator(){ | ||
return null; | ||
} | ||
|
||
|
||
private static class Node{ | ||
Object data; | ||
Node next; | ||
Node prev; | ||
} | ||
|
||
private Node index = head; | ||
@Override | ||
public boolean hasNext() { | ||
return index != null; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
Node tem = index; | ||
index = index.next; | ||
return tem; | ||
} | ||
} |
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,21 @@ | ||
package com.coding.basic; | ||
|
||
public class MainTest { | ||
|
||
public static void main(String[] args) { | ||
ArrayList list = new ArrayList(); | ||
list.add(1); | ||
list.add(2); | ||
list.add(3); | ||
list.add(4); | ||
list.add("this is the fifth"); | ||
System.out.println(list); | ||
System.out.println(list.size()); | ||
System.out.println(list.get(3)); | ||
|
||
while(list.hasNext()){ | ||
System.out.println(list.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,28 @@ | ||
package com.coding.basic; | ||
|
||
public class Queue { | ||
private LinkedList data; | ||
private int size; | ||
|
||
public Queue(){ | ||
data = new LinkedList(); | ||
} | ||
|
||
public void enQueue(Object o){ | ||
data.addLast(o); | ||
size++; | ||
} | ||
|
||
public Object deQueue(){ | ||
size --; | ||
return data.removeFirst(); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return size == 0; | ||
} | ||
|
||
public int size(){ | ||
return 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,31 @@ | ||
package com.coding.basic; | ||
|
||
public class Stack { | ||
|
||
private ArrayList elementData = new ArrayList(); | ||
private int size = 0; | ||
|
||
public void push(Object o){ | ||
elementData.add(o); | ||
size ++; | ||
} | ||
|
||
public Object pop(){ | ||
if(size > 0){ | ||
size--; | ||
return elementData.remove(size); | ||
}else{ | ||
return null; | ||
} | ||
} | ||
|
||
public Object peek(){ | ||
return elementData.get(size); | ||
} | ||
public boolean isEmpty(){ | ||
return size == 0; | ||
} | ||
public int size(){ | ||
return size; | ||
} | ||
} |
Empty file.