Skip to content

Commit

Permalink
implement basic data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
joy32812 committed Feb 25, 2017
1 parent 8f21b65 commit 9ef84dc
Show file tree
Hide file tree
Showing 8 changed files with 571 additions and 0 deletions.
88 changes: 88 additions & 0 deletions group16/542087872/src/com/coding/basic/ArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.coding.basic;

import java.util.Arrays;

public class ArrayList implements List {

private int size = 0;

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

// 每次乘2增长
private void grow() {
elementData = Arrays.copyOf(elementData, elementData.length * 2);
}


public void add(Object o){
if (size >= elementData.length) {
this.grow();
}

elementData[size++] = o;
}
public void add(int index, Object o){
if (size >= elementData.length) {
this.grow();
}
System.arraycopy(elementData, index, elementData, index + 1, size - index);

elementData[index] = o;
size++;
}

public Object get(int index){
if (index >= size) {
throw new IndexOutOfBoundsException();
}
return elementData[index];
}

public Object remove(int index){
if (index >= size) {
throw new IndexOutOfBoundsException();
}

Object el = elementData[index];
System.arraycopy(elementData, index + 1, elementData, index, size - index - 1);

size--;
return el;
}

public int size(){
return size;
}

private class ArrIter implements Iterator {
int cursor = 0;

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

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

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

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < size; i++) {
sb.append(elementData[i]);
if (i < size - 1) {
sb.append(",");
}
}
sb.append("]");
return sb.toString();
}
}
62 changes: 62 additions & 0 deletions group16/542087872/src/com/coding/basic/BinaryTreeNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.coding.basic;

public class BinaryTreeNode {

private int data;
private BinaryTreeNode left;
private BinaryTreeNode right;

public int getData() {
return data;
}
public void setData(int 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(int data) {
this.data = data;
}

private BinaryTreeNode insertAt(BinaryTreeNode node, int o) {
if (o < node.getData()) {
if (node.getLeft() != null) {
return insertAt(node.getLeft(), o);
} else {
BinaryTreeNode nowNode = new BinaryTreeNode(o);
node.setLeft(nowNode);

return nowNode;
}
} else {
if (node.getRight() != null) {
return insertAt(node.getRight(), o);
} else {
BinaryTreeNode nowNode = new BinaryTreeNode(o);
node.setRight(nowNode);
return nowNode;
}
}
}

public BinaryTreeNode insert(int o){
return insertAt(this, o);
}

@Override
public String toString() {
return "data: " + data;
}
}
7 changes: 7 additions & 0 deletions group16/542087872/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();

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

public class LinkedList implements List {

private Node head;
private Node tail;

public void add(Object o){
Node nowNode = new Node(o);
if (head == null) {
head = nowNode;
} else {
tail.next = nowNode;
}
tail = nowNode;
}
public void add(int index , Object o){
int count = 0;
Node lastOne = null;
Node tpHead = head;
while (tpHead != null && count != index) {
count++;
lastOne = tpHead;
tpHead = tpHead.next;
}
if (count != index) {
throw new IndexOutOfBoundsException();
}


Node nowNode = new Node(o);
if (lastOne == null) {
head = nowNode;
head.next = tpHead;
} else {
lastOne.next = nowNode;
nowNode.next = tpHead;
}
}
public Object get(int index){
int count = 0;
Node tpHead = head;
while (tpHead != null && count != index) {
count++;
tpHead = tpHead.next;
}
if (count != index) {
throw new IndexOutOfBoundsException();
}

return tpHead.data;
}
public Object remove(int index){
int count = 0;
Node lastOne = null;
Node tpHead = head;
while (tpHead != null && count != index) {
count++;
lastOne = tpHead;
tpHead = tpHead.next;
}
if (count != index) {
throw new IndexOutOfBoundsException();
}

if (lastOne == null) {
head = tpHead.next;
} else {
lastOne.next = tpHead.next;
}

if (tpHead.next == null) {
tail = lastOne;
}

return tpHead.data;
}

public int size(){
int count = 0;
Node tpHead = head;
while (tpHead != null) {
count ++;
tpHead = tpHead.next;
}

return count;
}

public void addFirst(Object o){
Node nowNode = new Node(o);
if (head == null) {
head = nowNode;
tail = nowNode;
} else {
nowNode.next = head;
head = nowNode;
}
}
public void addLast(Object o){
Node nowNode = new Node(o);
if (head == null) {
head = nowNode;
tail = nowNode;
} else {
tail.next = nowNode;
tail = nowNode;
}
}
public Object removeFirst(){
if (head == null) {
throw new IndexOutOfBoundsException();
}

Node nowValue = head;

Node nextNode = head.next;
if (nextNode == null) {
tail = null;
}
head = nextNode;

return nowValue.data;
}
public Object removeLast(){
if (head == null) {
throw new IndexOutOfBoundsException();
}

Node nowValue = tail;

Node lastOne = null;
Node tpHead = head;
while (tpHead != tail) {
lastOne = tpHead;
tpHead = tpHead.next;
}
if (lastOne == null) {
head = null;
} else {
lastOne.next = null;
}
tail = lastOne;

return nowValue.data;
}

private class LinkIter implements Iterator {

Node cursor = head;

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

@Override
public Object next() {
Node ret = cursor;
cursor = cursor.next;
return ret.data;
}
}

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


private static class Node{
Object data;
Node next;

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

@Override
public String toString() {
Node tpHead = head;
StringBuilder sb = new StringBuilder("[");
while (tpHead != null) {
sb.append(tpHead.data);
sb.append(",");
tpHead = tpHead.next;
}
sb.append("]");
return sb.toString();
}

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

public class Queue {

private LinkedList linkedList = new LinkedList();

public void enQueue(Object o){
linkedList.addLast(o);
}

public Object deQueue(){
return linkedList.removeFirst();
}

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

public int size(){
return linkedList.size();
}
}
Loading

0 comments on commit 9ef84dc

Please sign in to comment.