Skip to content

Commit

Permalink
Merge pull request #10 from xukaide77/20170226-collection
Browse files Browse the repository at this point in the history
实现arraylist、linkedlist、stack、queue和binarytreenode插入
  • Loading branch information
zeyuanpinghe authored Feb 26, 2017
2 parents 6ab3255 + 371b7ce commit 86a73e5
Show file tree
Hide file tree
Showing 15 changed files with 1,219 additions and 0 deletions.
8 changes: 8 additions & 0 deletions group19/527220084/xukai_coding/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*target
*.classpath
*.project
*.settings
*iml
.idea
*gen-*
rebel.xml
107 changes: 107 additions & 0 deletions group19/527220084/xukai_coding/coding-common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>xukai.coding</artifactId>
<groupId>org.xukai.coding</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>coding.common</artifactId>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>net.sf.log4jdbc</groupId>-->
<!--<artifactId>log4jdbc4</artifactId>-->
<!--<version>1.1</version>-->
<!--</dependency>-->
<!-- https://mvnrepository.com/artifact/com.google.code.findbugs/jsr305 -->
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>



<!--<dependency>-->
<!--<groupId>org.slf4j</groupId>-->
<!--<artifactId>slf4j-nop</artifactId>-->
<!--<version>1.7.12</version>-->
<!--</dependency>-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package org.xukai.common;



public class ArrayList implements List {

private int size = 0;

private static final int DEFAULT_CAPICITY = 10;

public ArrayList() {
elementData = new Object[DEFAULT_CAPICITY];
}

public ArrayList(int size) {
elementData = new Object[size];
}

private Object[] elementData ;

public void add(Object o){
elementData[size] = o;
size++;
}
public void add(int index, Object o){
if (index < 0 || index > size ) {
throw new ArrayIndexOutOfBoundsException();
} else {
if (isNeedResize(index)) {
elementData = grown(elementData, Math.max(elementData.length << 1, index + 1));
}
if(index < size){
System.arraycopy(elementData,index,elementData,index+1,size-index);
}
elementData[index] = o;
}
size++;
}



public Object get(int index){
if (index > size-1 || index < 0) {
throw new ArrayIndexOutOfBoundsException();
}
return elementData[index];
}

public Object remove(int index){
if (index > size-1 || index < 0) {
throw new ArrayIndexOutOfBoundsException();
}
Object result = elementData[index];
size--;
System.arraycopy(elementData,index+1,elementData,index,size-index);
return result;
}

public int size(){
return size;
}

public Iterator iterator(){
return new ArrayListIterator();
}

class ArrayListIterator implements Iterator {

private int pos = -1;

@Override
public boolean hasNext() {
return pos < size - 1;
}

@Override
public Object next() {
pos++;
return elementData[pos];
}
}

public void display(){
System.out.print("[");
Iterator iterator = iterator();
while (iterator.hasNext()){
System.out.print(" " + iterator.next());
}
System.out.print(" ]");
}

private boolean isNeedResize(int index){
return index > elementData.length-1 || size > elementData.length-1;
}

private Object[] grown(Object[] src,int capacity){
Object[] des = new Object[capacity];
System.arraycopy(elementData,0,des,0,elementData.length);
return des;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.xukai.common;

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(Comparable o){
if (data == null) {
data = o;
return this;
} else {
BinaryTreeNode node = new BinaryTreeNode();
node.data = o;
if (o.compareTo(data) < 0) {
if (left == null) {
left = node;
return node;
} else {
return left.insert(o);
}
} else {
if (right == null) {
right = node;
return node;
} else {
return right.insert(o);
}
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.xukai.common;

public interface Iterator {
public boolean hasNext();
public Object next();

}
Loading

0 comments on commit 86a73e5

Please sign in to comment.