Skip to content

Commit 3648573

Browse files
authored
Merge pull request onlyliuxin#2 from eloiseSJTU/master
add Test junit
2 parents 5c888ed + e539934 commit 3648573

File tree

25 files changed

+1297
-161
lines changed

25 files changed

+1297
-161
lines changed

.gitignore

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,3 @@ hs_err_pid*
1414
#ide config
1515
.metadata
1616
.recommenders
17-
/bin/
18-
.idea/workspace.xml
19-
.idea/dictionaries/myj.xml
20-
.idea/
21-
.DS_Store
22-
*.classpath
23-
*.project
24-
.settings
25-
.project
26-
.target
27-
.classpath
28-
**/.settings
29-
**/.classpath
30-
**/.eclipse
31-
**/target/
32-
target/
33-
bin/
34-
.svn
35-
*.iml
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.github.lhpmatlab.coding2017.basic;
2+
3+
/**
4+
* Created by andy on 2017/2/18.
5+
*/
6+
public class MyArrayList<T> {
7+
8+
private Object[] initialArray = {};
9+
private Object[] dataArray;
10+
private int initSize = 10;
11+
private int arraySize;
12+
public MyArrayList() {
13+
dataArray = initialArray;
14+
}
15+
16+
public MyArrayList(int init) {
17+
dataArray = new Object[init];
18+
}
19+
20+
public void ensureCapacity(int newCapacity) {
21+
if (newCapacity < arraySize)
22+
return;
23+
24+
Object[] old = dataArray;
25+
dataArray = new Object[newCapacity];
26+
for (int i = 0; i < size(); i++) {
27+
dataArray[i] = old[i];
28+
}
29+
}
30+
31+
public void add(T element) {
32+
add(size(), element);
33+
}
34+
35+
public void add(int index, T element) {
36+
if (size() == dataArray.length) {
37+
ensureCapacity(size()*2 + 1);
38+
}
39+
for(int i=arraySize;i>index;i--) {
40+
dataArray[i] = dataArray[i - 1];
41+
}
42+
dataArray[index] = element;
43+
arraySize++;
44+
}
45+
46+
public T delete(int index) {
47+
if (index < 0 || index > arraySize) {
48+
throw new ArrayIndexOutOfBoundsException();
49+
}
50+
T removeElement = (T)dataArray[index];
51+
for (int i = index; i < size() -1; i++) {
52+
dataArray[i] = dataArray[i + 1];
53+
}
54+
arraySize--;
55+
return removeElement;
56+
}
57+
58+
public T get(int index) {
59+
if (index < 0 || index > arraySize) {
60+
throw new ArrayIndexOutOfBoundsException();
61+
}
62+
return (T)dataArray[index];
63+
}
64+
65+
public T set(int index, T newElement) {
66+
if (index < 0 || index > arraySize) {
67+
throw new ArrayIndexOutOfBoundsException();
68+
}
69+
T oldElement = (T) dataArray[index];
70+
dataArray[index] = newElement;
71+
72+
return oldElement;
73+
}
74+
75+
public int size() {
76+
return arraySize;
77+
}
78+
79+
public boolean isEmpty() {
80+
return size() == 0;
81+
}
82+
83+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package com.github.lhpmatlab.coding2017.basic;
2+
3+
/**
4+
* Created by andy on 2017/2/18.
5+
*/
6+
public class MyLinkedList<T> {
7+
private class Node<T> {
8+
public Node<T> pre;
9+
public Node<T> next;
10+
public T data;
11+
12+
public Node(Node<T> pre,Node<T> next,T data) {
13+
this.pre = pre;
14+
this.next = next;
15+
this.data = data;
16+
}
17+
}
18+
19+
private int dataSize;
20+
21+
private Node head;
22+
private Node tail;
23+
24+
public MyLinkedList() {
25+
head = new Node<T>(null,null,null);
26+
tail = new Node(head, null, null);
27+
head.next = tail;
28+
dataSize = 0;
29+
}
30+
31+
public void add(T t) {
32+
// add(size(), t);
33+
Node<T> newNode = new Node<>(null, tail, t);
34+
newNode.pre = tail.pre;
35+
tail.pre.next = newNode;
36+
tail.pre = newNode;
37+
dataSize++;
38+
39+
}
40+
41+
/**
42+
* 根据索引添加没有实现
43+
* @param index
44+
* @param element
45+
*/
46+
public void add(int index,T element) {
47+
//TODO 根据索引添加元素
48+
// addBefore(getNode(index,0,size()-1),element);
49+
// if (index == dataSize) {
50+
// add(element);
51+
// } else {
52+
//
53+
// }
54+
}
55+
56+
public T get(int index) {
57+
return getNode(index).data;
58+
}
59+
60+
public T set(int index, T newValue) {
61+
Node<T> node = getNode(index);
62+
T oldData = node.data;
63+
node.data = newValue;
64+
return oldData;
65+
}
66+
67+
public T remove(int index) {
68+
Node<T> node = getNode(index);
69+
node.next.pre = node.pre;
70+
node.pre.next = node.next;
71+
dataSize--;
72+
73+
return node.data;
74+
75+
}
76+
77+
private void addBefore(Node<T> node, T element) {
78+
// newNode.pre.next = newNode;
79+
// node.pre = newNode;
80+
Node<T> pre = node.pre;
81+
Node<T> newNode = new Node<>(node.pre, node, element);
82+
node.pre = newNode;
83+
pre.next = newNode;
84+
85+
dataSize++;
86+
}
87+
88+
private Node<T> getNode(int index) {
89+
return getNode(index, 0, size());
90+
}
91+
92+
private Node<T> getNode(int index, int lower, int upper) {
93+
Node<T> p;
94+
if (index < lower || index > upper) {
95+
throw new IndexOutOfBoundsException();
96+
}
97+
98+
if (index < size() / 2) {
99+
p = head.next;
100+
for (int i = 0; i < index; i++) {
101+
p = p.next;
102+
}
103+
} else {
104+
p = tail.pre;
105+
for (int i = size()-1; i > index; i--) {
106+
p = p.pre;
107+
}
108+
}
109+
return p;
110+
}
111+
112+
public int size() {
113+
return dataSize;
114+
}
115+
116+
public boolean isEmpty() {
117+
return size() == 0;
118+
}
119+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.github.lhpmatlab.coding2017.basic;
2+
3+
/**
4+
* Created by andy on 2017/2/22.
5+
*/
6+
public class MyQueue<T> {
7+
private MyLinkedList<T> link = new MyLinkedList<>();
8+
9+
public void enQueue(T t) {
10+
link.add(t);
11+
}
12+
13+
public T deQueue() {
14+
if (size() <= 0) {
15+
return null;
16+
}
17+
T t = link.get(0);
18+
link.remove(0);
19+
return t;
20+
}
21+
22+
public boolean isEmpty() {
23+
return size() == 0;
24+
}
25+
26+
public int size() {
27+
return link.size();
28+
}
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.github.lhpmatlab.coding2017.basic;
2+
3+
/**
4+
* Created by andy on 2017/2/22.
5+
*/
6+
public class MyStack<T> {
7+
private MyArrayList<T> list = new MyArrayList<>();
8+
9+
public void push(T t) {
10+
list.add(t);
11+
}
12+
13+
public T pop() {
14+
if (size() <= 0) {
15+
throw new IndexOutOfBoundsException();
16+
}
17+
return list.delete(size() - 1);
18+
}
19+
20+
public T peek() {
21+
return list.get(size() - 1);
22+
}
23+
24+
public boolean isEmpty() {
25+
return list.size() == 0;
26+
}
27+
public int size() {
28+
return list.size();
29+
}
30+
}

0 commit comments

Comments
 (0)