Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions group14/254659936/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*.iml
.gradle
.idea
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.externalNativeBuild
7 changes: 7 additions & 0 deletions group14/254659936/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

public class Main {

public static void main(String[] args) {
System.out.println("Hello World!");
}
}
73 changes: 73 additions & 0 deletions group14/254659936/src/com/coding/basic/ArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.coding.basic;

import java.util.Objects;

public class ArrayList implements List {

private int size = 0;

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

public void add(Object o) {
if (size == elementData.length) {
Object[] newArr = new Object[elementData.length * 2];
System.arraycopy(newArr, 0, elementData, 0, elementData.length);
elementData = newArr;
}
elementData[size] = o;
size++;
}

public void add(int index, Object o) {
if (index >= size) {
throw new RuntimeException("the ArrayList size is short than index");
}
elementData[index] = o;
}

public Object get(int index) {
if (index >= size) {
throw new RuntimeException("the ArrayList size is short than index");
}
return elementData[index];
}

public Object remove(int index) {
if (index >= size) {
throw new RuntimeException("the ArrayList size is short than index");
}
Object resultObj = elementData[index];
size--;
for (int i = index; i < size; i++) {
elementData[index] = elementData[index + 1];
}
return resultObj;
}

public int size() {
return size;
}

public Iterator iterator() {
return null;
}

private class ArrayIterator implements Iterator {

private int iteratorIndex = 0;

@Override
public boolean hasNext() {
return iteratorIndex < size;
}

@Override
public Object next() {
if (iteratorIndex >= size) {
throw new RuntimeException("the index is out of the list");
}
return elementData[iteratorIndex++];
}
}

}
69 changes: 69 additions & 0 deletions group14/254659936/src/com/coding/basic/BinaryTreeNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.coding.basic;

public class BinaryTreeNode<T extends BinaryTreeNode.Compare> {

private T data;
private BinaryTreeNode left;
private BinaryTreeNode right;

public T getData() {
return data;
}

public void setData(T 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(T o) {
BinaryTreeNode node = new BinaryTreeNode();
node.setData(o);
insert(this, node);
return node;
}

private void insert(BinaryTreeNode parent, BinaryTreeNode child) {
BinaryTreeNode node;
if (child.getData().isLargeThanTarget(parent.getData())) {
// 子节点比父节点大,需要向右插入
node = getRight();
if (null == node) {
// 右节点为空则可以直接插入
parent.setRight(node);
} else {
// 递归检查右边子树的插入位置
insert(node, child);
}
} else {
// 子节点比父节点小,或者等于父节点,需要向左插入
node = getLeft();
if (null == node) {
// 左节点为空,则直接插入
parent.setLeft(node);
} else {
// 递归检查左子树的插入位置
insert(node, child);
}
}
}

public interface Compare {
boolean isLargeThanTarget(Compare target);
}

}
8 changes: 8 additions & 0 deletions group14/254659936/src/com/coding/basic/Iterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.coding.basic;

public interface Iterator {
public boolean hasNext();

public Object next();

}
155 changes: 155 additions & 0 deletions group14/254659936/src/com/coding/basic/LinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package com.coding.basic;

public class LinkedList implements List {

private Node head;
private int size = 0;

public void add(Object o) {
addLast(o);
}

public void add(int index, Object o) {
if (index >= size) {
throw new RuntimeException("the LinkedList size is short than index");
}
Node node = new Node();
node.data = 0;
if (index == 0) {
node.next = head;
head = node;
}
Node next = head;
int i = 0;
while (null != next) {
i++;
if (index == i) {
node.next = next.next;
next.next = node;
break;
}
next.next = next.next;
}
size++;
}

public Object get(int index) {
if (index >= size) {
throw new RuntimeException("the LinkedList size is short than index");
}
Node next = head;
int i = 0;
while (null != next) {
if (i == index) {
return next;
}
next = next.next;
i++;
}
return null;
}

public Object remove(int index) {
if (index >= size) {
throw new RuntimeException("the LinkedList size is short than index");
}
size--;
Node resultNode = null;
if (index == 0) {
resultNode = head;
head = head.next;
return resultNode.data;
}

Node next = head;
int i = 0;
while (null != next) {
if (i == index) {
resultNode = next.next;
next.next = resultNode.next;
}
}
return resultNode.data;
}

public int size() {
return size;
}

public void addFirst(Object o) {
size++;
Node node = new Node();
node.data = 0;
node.next = head;
head = node;
}

public void addLast(Object o) {
size++;
Node node = new Node();
node.data = o;
if (null == head) {
head = node;
return;
}
Node next = head;
while (null != next.next) {
next = next.next;
}
next.next = node;
}

public Object removeFirst() {
if (size == 0) {
throw new RuntimeException("the LinkedList is null");
}
size--;
Object obj = head.data;
head = head.next;
return obj;
}

public Object removeLast() {
if (size == 0) {
throw new RuntimeException("the LinkedList is null");
}
Node next = head;
Object obj = null;
for (int i = 0; i < size - 1; i++) {
next = next.next;
}
next.next = null;
size--;
return obj;
}

public Iterator iterator() {
return null;
}


private static class Node {
Object data;
Node next;
}

private class LinkedListIterator implements Iterator {

private Node next = head;

@Override
public boolean hasNext() {
return null != next;
}

@Override
public Object next() {
if (null == next) {
throw new RuntimeException("the LinkedList is out of index");
}
Object obj = next.data;
next = next.next;
return obj;
}
}
}
13 changes: 13 additions & 0 deletions group14/254659936/src/com/coding/basic/List.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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();
}
45 changes: 45 additions & 0 deletions group14/254659936/src/com/coding/basic/Queue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.coding.basic;

public class Queue {

private Node head;
private Node tail;
private int size;

public void enQueue(Object o) {
Node node = new Node();
node.data = o;
if (null == head) {
head = node;
tail = node;
} else {
tail.next = node;
tail = tail.next;
}
size++;
}

public Object deQueue() {
if (size <= 0) {
throw new RuntimeException("the queue is empty");
}
Object obj = head.data;
head = head.next;
size--;
return obj;
}

public boolean isEmpty() {
return size == 0;
}

public int size() {
return size;
}

private static class Node {
Object data;
Node next;
Node pre;
}
}
Loading