forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
--ztc-dev
committed
Feb 25, 2017
1 parent
3c2800e
commit 81c40c9
Showing
9 changed files
with
537 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,35 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.ztc</groupId> | ||
<artifactId>JavaLevelUp</artifactId> | ||
<packaging>war</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<name>JavaLevelUp Maven Webapp</name> | ||
<url>http://maven.apache.org</url> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>3.8.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<version>RELEASE</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<finalName>JavaLevelUp</finalName> | ||
</build> | ||
|
||
</project> |
108 changes: 108 additions & 0 deletions
108
group03/569045298/src/main/com/coding/basic/datastructure/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,108 @@ | ||
package com.coding.basic.datastructure; | ||
|
||
|
||
/** | ||
* Created by zt on 2017/2/19. | ||
*/ | ||
public class ArrayList implements List { | ||
|
||
private static final int DEFAULT_CAPACITY = 10; | ||
private int size = 0; | ||
private Object[] elementData = null; | ||
|
||
public ArrayList() { | ||
elementData = new Object[DEFAULT_CAPACITY]; | ||
} | ||
|
||
public ArrayList(int initialCapacity) { | ||
if (initialCapacity < 0) { | ||
throw new RuntimeException("initialCapacity is smaller than zero"); | ||
} | ||
elementData = new Object[initialCapacity]; | ||
} | ||
|
||
@Override | ||
public void add(Object o) { | ||
checkCapacity(size + 1); | ||
elementData[size] = o; | ||
size++; | ||
} | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
checkCapacity(size + 1); | ||
System.arraycopy(elementData, index, elementData, index + 1, size - index); | ||
elementData[index] = o; | ||
size++; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
checkRange(index); | ||
return elementData[index]; | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
Object removedObject = elementData[index]; | ||
System.arraycopy(elementData, index + 1, elementData, index, elementData.length - index - 1); | ||
elementData[--size] = null; | ||
return removedObject; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
private void checkRange(int index) { | ||
if (index < 0 || index > elementData.length) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
} | ||
|
||
private void checkCapacity(int size) { | ||
if (size > elementData.length) { | ||
int newLength = elementData.length * 2; | ||
Object[] newObject = new Object[newLength]; | ||
System.arraycopy(elementData, 0, newObject, 0, elementData.length); | ||
elementData = newObject; | ||
} | ||
} | ||
|
||
public Iterator iterator() { | ||
return new ArrayListIterator(this); | ||
} | ||
|
||
private class ArrayListIterator implements Iterator { | ||
|
||
ArrayList arrayList = null; | ||
int pos = 0; | ||
|
||
private ArrayListIterator(ArrayList arrayList) { | ||
this.arrayList = arrayList; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
// TODO | ||
pos++; | ||
if (pos > size) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
// TODO | ||
return elementData[pos]; | ||
} | ||
|
||
@Override | ||
public Object remove() { | ||
// TODO | ||
return null; | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
group03/569045298/src/main/com/coding/basic/datastructure/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,41 @@ | ||
package com.coding.basic.datastructure; | ||
|
||
/** | ||
* Created by zt on 2017/2/19. | ||
*/ | ||
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 object) { | ||
return null; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
group03/569045298/src/main/com/coding/basic/datastructure/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,13 @@ | ||
package com.coding.basic.datastructure; | ||
|
||
/** | ||
* Created by zt on 2017/2/19. | ||
*/ | ||
public interface Iterator { | ||
|
||
boolean hasNext(); | ||
|
||
Object next(); | ||
|
||
Object remove(); | ||
} |
167 changes: 167 additions & 0 deletions
167
group03/569045298/src/main/com/coding/basic/datastructure/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,167 @@ | ||
package com.coding.basic.datastructure; | ||
|
||
/** | ||
* Created by zt on 2017/2/19. | ||
*/ | ||
public class LinkedList implements List { | ||
|
||
private Node head; | ||
|
||
private Node tail; | ||
|
||
private int size = 0; | ||
|
||
public LinkedList() { | ||
|
||
} | ||
|
||
@Override | ||
public void add(Object object) { | ||
if (null == head) { | ||
head = new Node(object); | ||
head.next = null; | ||
tail = head; | ||
size++; | ||
} else { | ||
// 尾插法 | ||
Node newNode = new Node(object); | ||
tail.next = newNode; | ||
tail = newNode; | ||
tail.next = null; | ||
size++; | ||
} | ||
} | ||
|
||
@Override | ||
public void add(int index, Object object) { | ||
checkRange(index); | ||
if (null == head) { | ||
add(object); | ||
return; | ||
} | ||
if (index == 0) { | ||
addFirst(object); | ||
return; | ||
} | ||
Node pre = node(index - 1); | ||
Node newNode = new Node(object); | ||
newNode.next = pre.next; | ||
pre.next = newNode; | ||
size++; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
checkRange(index); | ||
checkNodeNotNull(); | ||
Node node = node(index); | ||
return node.data; | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
checkRange(index); | ||
checkNodeNotNull(); | ||
if (index == 0) { | ||
removeFirst(); | ||
return head; | ||
} | ||
Node pre = node(index - 1); | ||
pre.next = pre.next.next; | ||
size--; | ||
return head; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
public void addFirst(Object object) { | ||
if (null == head) { | ||
head = new Node(object); | ||
head.next = null; | ||
size++; | ||
} else { | ||
Node firstNode = new Node(object); | ||
firstNode.next = head; | ||
head = firstNode; | ||
size++; | ||
} | ||
} | ||
|
||
public Object removeFirst() { | ||
checkNodeNotNull(); | ||
head = head.next; | ||
size--; | ||
return head; | ||
} | ||
|
||
public Object removeLast() { | ||
checkNodeNotNull(); | ||
if (size == 1) { | ||
head = null; | ||
} | ||
Node pre = node(size() - 2); | ||
pre.next = null; | ||
size--; | ||
return head; | ||
} | ||
|
||
private void checkRange(int index) { | ||
if (index > size - 1 || index < 0) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
} | ||
|
||
private void checkNodeNotNull() { | ||
if (null == head) { | ||
throw new NullPointerException(); | ||
} | ||
} | ||
|
||
private Node node(int index) { | ||
Node node = head; | ||
for (int i = 0; i < index; i++) { | ||
node = node.next; | ||
} | ||
return node; | ||
} | ||
|
||
private static class Node { | ||
Node next; | ||
private Object data; | ||
|
||
public Node() { | ||
|
||
} | ||
|
||
public Node(Object data) { | ||
this.data = data; | ||
} | ||
|
||
public Node(Object data, Node next) { | ||
this.data = data; | ||
this.next = next; | ||
} | ||
|
||
public Node(Object data, Node next, Node prev) { | ||
this.data = data; | ||
this.next = next; | ||
} | ||
} | ||
|
||
/*@Override | ||
public void add(Object object) { | ||
if (null == head) { | ||
head = new Node(object); | ||
head.next = null; | ||
} else { | ||
// 头插法 | ||
Node nextNode = new Node(object); | ||
nextNode.next = head.next; | ||
head.next = nextNode; | ||
} | ||
}*/ | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
group03/569045298/src/main/com/coding/basic/datastructure/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,17 @@ | ||
package com.coding.basic.datastructure; | ||
|
||
/** | ||
* Created by zt on 2017/2/19. | ||
*/ | ||
public interface List { | ||
|
||
void add(Object o); | ||
|
||
void add(int index, Object o); | ||
|
||
Object get(int index); | ||
|
||
Object remove(int index); | ||
|
||
int size(); | ||
} |
Oops, something went wrong.