Skip to content

Commit

Permalink
Merge pull request #14 from sargeles/master
Browse files Browse the repository at this point in the history
只写了linkedlist和arraylist的简单实现,这周实在太忙,要上线T T
  • Loading branch information
DonaldY authored Mar 12, 2017
2 parents 39c8170 + 59d8962 commit 26c29e3
Show file tree
Hide file tree
Showing 10 changed files with 302 additions and 0 deletions.
7 changes: 7 additions & 0 deletions group24/330657387/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>
5 changes: 5 additions & 0 deletions group24/330657387/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/bin/

#ide config
.settings/

17 changes: 17 additions & 0 deletions group24/330657387/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>2017Learning</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
62 changes: 62 additions & 0 deletions group24/330657387/src/com/coding/basic/ArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.coding.basic;

import java.util.Arrays;

public class ArrayList implements List {

private int size = 0;

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

public void ensureCapacity(int input) {
if (input > elementData.length) {
growCapacity();
}
}

private void growCapacity(){
elementData = Arrays.copyOf(elementData, size * 2);
}

public void add(Object o){
ensureCapacity(size + 1);
elementData[size++] = o;
}


public void add(int index, Object o){
rangeCheck(index);
ensureCapacity(size + 1);
System.arraycopy(elementData,index, elementData, index + 1, size - index);
elementData[index] = o;
size ++;
}

private void rangeCheck(int index){
if (index > size || index < 0){
throw new IndexOutOfBoundsException();
}
}

public Object get(int index){
rangeCheck(index);
return elementData[index];
}

public Object remove(int index){
rangeCheck(index);
Object dest = elementData[index];
System.arraycopy(elementData, index +1, elementData, index, size-index-1);
size --;
return dest;
}

public int size(){
return size;
}

public Iterator iterator(){
return null;
}

}
32 changes: 32 additions & 0 deletions group24/330657387/src/com/coding/basic/BinaryTreeNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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){
return null;
}

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

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

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

import java.util.NoSuchElementException;

public class LinkedList implements List {

private Node head;
private int size = 0;

public void add(Object o) {
if (isEmpty()) {
head = new Node(o);
} else {
Node tail = (Node)get(size-1);
Node node = new Node(o);
tail.next = node;
}
size++;
}

public boolean isEmpty() {
return (size == 0) ? true : false;
}

public void add(int index, Object o) {
rangeCheck(index);
if (index ==0) {
Node node = new Node(o);
node.next = head;
head = node;
} else {
Node pre = (Node)get(index-1);
Node node = new Node(o);
node.next = pre.next;
pre.next = node;
}
}

private void rangeCheck(int index){
if (index >= size || index <0){
throw new IndexOutOfBoundsException();
}
}

public Object get(int index) {
rangeCheck(index);
Node dest = head;
for (int i = 0; i< index; i++){
dest = dest.next;
}
return dest.data;
}

public Object remove(int index) {
rangeCheck(index);
Node pre = (Node)get(index);
Node dest = pre.next;
pre.next = dest.next;
size --;
return dest;
}

public int size() {
return size;
}

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

public void addLast(Object o) {
Node last = (Node)get(size-1);
Node node = new Node(o);
last.next = node;
size++;
}

public Object removeFirst() {
if (head == null) {
throw new NoSuchElementException();
}
Node newhead = head;
head = head.next;
size --;
return newhead;
}

public Object removeLast() {
if (head == null) {
throw new NoSuchElementException();
}
if (head.next == null) {
Node tmp = head;
head = null;
size --;
return tmp;
}
Node newLastNode = (Node)get(size-2);
Node oldLastNode = newLastNode.next;
newLastNode.next = null;
size --;
return oldLastNode;
}

public Iterator iterator() {
return null;
}

private static class Node {
Object data;
Node next;

Node(Object data) {
this.data = data;
next = null;
}
}

}
9 changes: 9 additions & 0 deletions group24/330657387/src/com/coding/basic/List.java
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();
}
19 changes: 19 additions & 0 deletions group24/330657387/src/com/coding/basic/Queue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.coding.basic;

public class Queue {

public void enQueue(Object o){
}

public Object deQueue(){
return null;
}

public boolean isEmpty(){
return false;
}

public int size(){
return -1;
}
}
22 changes: 22 additions & 0 deletions group24/330657387/src/com/coding/basic/Stack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.coding.basic;

public class Stack {
private ArrayList elementData = new ArrayList();

public void push(Object o){
}

public Object pop(){
return null;
}

public Object peek(){
return null;
}
public boolean isEmpty(){
return false;
}
public int size(){
return -1;
}
}

0 comments on commit 26c29e3

Please sign in to comment.