Skip to content

Commit

Permalink
Merge pull request #3 from ddyblackhat/master
Browse files Browse the repository at this point in the history
基本数据结构
  • Loading branch information
844028312 authored Feb 24, 2017
2 parents 8f54992 + ea616bc commit 32485cc
Show file tree
Hide file tree
Showing 15 changed files with 849 additions and 0 deletions.
138 changes: 138 additions & 0 deletions group04/1796244932/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
.metadata/
RemoteSystemsTempFiles/
.recommenders/

*.iml

# Created by https://www.gitignore.io/api/eclipse,intellij,java

### Eclipse ###

.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
.recommenders

# Eclipse Core
.project

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# PyDev specific (Python IDE for Eclipse)
*.pydevproject

# CDT-specific (C/C++ Development Tooling)
.cproject

# JDT-specific (Eclipse Java Development Tools)
.classpath

# Java annotation processor (APT)
.factorypath

# PDT-specific (PHP Development Tools)
.buildpath

# sbteclipse plugin
.target

# Tern plugin
.tern-project

# TeXlipse plugin
.texlipse

# STS (Spring Tool Suite)
.springBeans

# Code Recommenders
.recommenders/

### Intellij ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
.idea/**/workspace.xml
.idea/**/tasks.xml
*/.idea/
# Sensitive or high-churn files:
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.xml
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml

# Gradle:
.idea/**/gradle.xml
.idea/**/libraries

# Mongo Explorer plugin:
.idea/**/mongoSettings.xml

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

### Intellij Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721

# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr

### Java ###
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# End of https://www.gitignore.io/api/eclipse,intellij,java
6 changes: 6 additions & 0 deletions group04/1796244932/learn01/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.settings/
/target/
.classpath
.project
/bin/

42 changes: 42 additions & 0 deletions group04/1796244932/learn01/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<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">
<modelVersion>4.0.0</modelVersion>

<groupId>com.dudy</groupId>
<artifactId>learn01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>learn01</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>



<profiles>
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.dudy.learn01.base;

import java.util.Arrays;

public class MyArrayList implements MyList {

private int size = 0;

private Object[] elementData = new Object[16];

/**
* 增加元素: ①数组没满之前,直接添加到最后,满了扩容添加
*/
public void add(Object o) {
// 检查是否需要扩容
this.checkGrow(size + 1);
elementData[size++] = o;
}

/**
* 检查是否需要扩容
*
* @param newSize
*/
private void checkGrow(int newSize) {
if (newSize > elementData.length) {
this.grow(elementData);
}
}

/**
* 扩容
*
* @param oldElementData
*/
private void grow(Object[] oldElementData) {
int lenth = (int) (oldElementData.length * 1.5);
elementData = Arrays.copyOf(oldElementData, lenth);
}

/**
* 根据索引添加:①同 add ② 可能会出现 index 超出当前位置的情况 ③往 中间插入时需要移位
*/
public void add(int index, Object o) {
// 检查是否需要扩容
if (index > size || index < 0) {
throw new RuntimeException("Index: " + index + ", Size: " + size);
}
this.checkGrow(size + 1);
// 循环移位
int tmp = size;
for (int i = 0; i < size - index; i++) {
elementData[tmp] = elementData[tmp - 1];
tmp--;
}
// 索引位置赋值
elementData[index] = o;
size++;
}

/**
* 直接返回相应数组下标就好
*/
public Object get(int index) {
return elementData[index];
}

/**
* 删除元素:①注意移位
*/
public Object remove(int index) {
// 检查是否需要扩容
if (index > size || index < 0) {
throw new RuntimeException("Index: " + index + ", Size: " + size);
}
Object desc = elementData[index];

for (int i = 0; i < size - index; i++) {
elementData[index] = elementData[index+1];
index++;
}
size--;
return desc;
}

public int size() {
return this.size;
}

public MyIterator iterator() {


return new MyItr();
}

public class MyItr implements MyIterator{

int cursor;



public boolean hasNext() {
return cursor != size;
}

public Object next() {

return elementData[cursor++];
}
}



@Override
public String toString() {
StringBuffer str = new StringBuffer();
str.append("[");
for (int i = 0; i < size; i++) {
str.append(elementData[i]+",");
}
str.append("]");
return str.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.dudy.learn01.base;

public class MyBinaryTree {

private Node root;

public Node getRoot() {
return root;
}

static class Node {
private int data;
private Node left;
private Node right;

public Node(int data) {
this.data = data;
}

}

public Node insert(int o) {
Node newNode = new Node(o);

if (root == null) {
root = newNode;
return newNode;
}
Node currentNode = root;

while (true) {
// System.out.println("currentNode: " + currentNode.data );
if (o <= currentNode.data) { // left

if (currentNode.left != null) {
currentNode = currentNode.left;
} else {
currentNode.left = newNode;
// System.out.println("left return ...");
// System.out.println("");
return newNode;
}
} else { // right
if (currentNode.right != null) {
currentNode = currentNode.right;
} else {
currentNode.right = newNode;
// System.out.println("right return ...");
// System.out.println("");
return newNode;
}
}

}

}

public void display(Node root) {

System.out.print(root.data + " ");
if (root.left != null) {
display(root.left);
}
if (root.right != null) {
display(root.right);
}
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.dudy.learn01.base;
public interface MyIterator {
public boolean hasNext();
public Object next();

}
Loading

0 comments on commit 32485cc

Please sign in to comment.