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
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