Skip to content

Commit

Permalink
Merge pull request #2 from eloiseSJTU/master
Browse files Browse the repository at this point in the history
add Test junit
  • Loading branch information
Ven13 authored Feb 26, 2017
2 parents 5c888ed + e539934 commit 3648573
Show file tree
Hide file tree
Showing 25 changed files with 1,297 additions and 161 deletions.
19 changes: 0 additions & 19 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,3 @@ hs_err_pid*
#ide config
.metadata
.recommenders
/bin/
.idea/workspace.xml
.idea/dictionaries/myj.xml
.idea/
.DS_Store
*.classpath
*.project
.settings
.project
.target
.classpath
**/.settings
**/.classpath
**/.eclipse
**/target/
target/
bin/
.svn
*.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.github.lhpmatlab.coding2017.basic;

/**
* Created by andy on 2017/2/18.
*/
public class MyArrayList<T> {

private Object[] initialArray = {};
private Object[] dataArray;
private int initSize = 10;
private int arraySize;
public MyArrayList() {
dataArray = initialArray;
}

public MyArrayList(int init) {
dataArray = new Object[init];
}

public void ensureCapacity(int newCapacity) {
if (newCapacity < arraySize)
return;

Object[] old = dataArray;
dataArray = new Object[newCapacity];
for (int i = 0; i < size(); i++) {
dataArray[i] = old[i];
}
}

public void add(T element) {
add(size(), element);
}

public void add(int index, T element) {
if (size() == dataArray.length) {
ensureCapacity(size()*2 + 1);
}
for(int i=arraySize;i>index;i--) {
dataArray[i] = dataArray[i - 1];
}
dataArray[index] = element;
arraySize++;
}

public T delete(int index) {
if (index < 0 || index > arraySize) {
throw new ArrayIndexOutOfBoundsException();
}
T removeElement = (T)dataArray[index];
for (int i = index; i < size() -1; i++) {
dataArray[i] = dataArray[i + 1];
}
arraySize--;
return removeElement;
}

public T get(int index) {
if (index < 0 || index > arraySize) {
throw new ArrayIndexOutOfBoundsException();
}
return (T)dataArray[index];
}

public T set(int index, T newElement) {
if (index < 0 || index > arraySize) {
throw new ArrayIndexOutOfBoundsException();
}
T oldElement = (T) dataArray[index];
dataArray[index] = newElement;

return oldElement;
}

public int size() {
return arraySize;
}

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.github.lhpmatlab.coding2017.basic;

/**
* Created by andy on 2017/2/18.
*/
public class MyLinkedList<T> {
private class Node<T> {
public Node<T> pre;
public Node<T> next;
public T data;

public Node(Node<T> pre,Node<T> next,T data) {
this.pre = pre;
this.next = next;
this.data = data;
}
}

private int dataSize;

private Node head;
private Node tail;

public MyLinkedList() {
head = new Node<T>(null,null,null);
tail = new Node(head, null, null);
head.next = tail;
dataSize = 0;
}

public void add(T t) {
// add(size(), t);
Node<T> newNode = new Node<>(null, tail, t);
newNode.pre = tail.pre;
tail.pre.next = newNode;
tail.pre = newNode;
dataSize++;

}

/**
* 根据索引添加没有实现
* @param index
* @param element
*/
public void add(int index,T element) {
//TODO 根据索引添加元素
// addBefore(getNode(index,0,size()-1),element);
// if (index == dataSize) {
// add(element);
// } else {
//
// }
}

public T get(int index) {
return getNode(index).data;
}

public T set(int index, T newValue) {
Node<T> node = getNode(index);
T oldData = node.data;
node.data = newValue;
return oldData;
}

public T remove(int index) {
Node<T> node = getNode(index);
node.next.pre = node.pre;
node.pre.next = node.next;
dataSize--;

return node.data;

}

private void addBefore(Node<T> node, T element) {
// newNode.pre.next = newNode;
// node.pre = newNode;
Node<T> pre = node.pre;
Node<T> newNode = new Node<>(node.pre, node, element);
node.pre = newNode;
pre.next = newNode;

dataSize++;
}

private Node<T> getNode(int index) {
return getNode(index, 0, size());
}

private Node<T> getNode(int index, int lower, int upper) {
Node<T> p;
if (index < lower || index > upper) {
throw new IndexOutOfBoundsException();
}

if (index < size() / 2) {
p = head.next;
for (int i = 0; i < index; i++) {
p = p.next;
}
} else {
p = tail.pre;
for (int i = size()-1; i > index; i--) {
p = p.pre;
}
}
return p;
}

public int size() {
return dataSize;
}

public boolean isEmpty() {
return size() == 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.github.lhpmatlab.coding2017.basic;

/**
* Created by andy on 2017/2/22.
*/
public class MyQueue<T> {
private MyLinkedList<T> link = new MyLinkedList<>();

public void enQueue(T t) {
link.add(t);
}

public T deQueue() {
if (size() <= 0) {
return null;
}
T t = link.get(0);
link.remove(0);
return t;
}

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

public int size() {
return link.size();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.lhpmatlab.coding2017.basic;

/**
* Created by andy on 2017/2/22.
*/
public class MyStack<T> {
private MyArrayList<T> list = new MyArrayList<>();

public void push(T t) {
list.add(t);
}

public T pop() {
if (size() <= 0) {
throw new IndexOutOfBoundsException();
}
return list.delete(size() - 1);
}

public T peek() {
return list.get(size() - 1);
}

public boolean isEmpty() {
return list.size() == 0;
}
public int size() {
return list.size();
}
}
Loading

0 comments on commit 3648573

Please sign in to comment.