Skip to content

Commit 8b08c37

Browse files
authored
Merge pull request onlyliuxin#22 from camilesing/dev
push first homework at here
2 parents eb7981e + da4430d commit 8b08c37

File tree

13 files changed

+702
-1
lines changed

13 files changed

+702
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ hs_err_pid*
1313

1414
#ide config
1515
.metadata
16-
.recommenders
16+
.recommenders
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package Impl;
2+
3+
import Interface.ArrayList;
4+
import Interface.Iterator;
5+
import ex.MyArrest;
6+
7+
/**
8+
* Created by Administrator on 2017/2/25.
9+
*/
10+
public class MyArraryList extends ArrayList {
11+
private Object[] objArr;
12+
private int size;
13+
private int postion;
14+
15+
public MyArraryList() {
16+
this.objArr = new Object[10];
17+
this.size = 10;
18+
this.postion = 0;
19+
}
20+
21+
22+
public MyArraryList(int size) {
23+
this.objArr = new Object[size];
24+
this.size = size;
25+
this.postion = 0;
26+
}
27+
28+
public MyArraryList(Object[] objArr) {
29+
this.objArr = objArr;
30+
this.size = objArr.length;
31+
this.postion = objArr.length - 1;
32+
}
33+
34+
@Override
35+
public void add(Object o) {
36+
int limit = this.size + (this.size / 2);
37+
Object[] newObjArr = new Object[limit];
38+
//public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)从指定源数组中复制一个数组,
39+
// 复制从指定的位置开始,到目标数组的指定位置结束。从src引用的源数组到dest引用的目标数组,
40+
// 数组组件的一个子序列被复制下来。被复制的组件的编号等于length参数。
41+
// 源数组中位置在srcPos到srcPos+length-1之间的组件被分别复制到目标数组中的destPos到destPos+length-1位置。
42+
System.arraycopy(this.objArr, 0, newObjArr, 0, objArr.length);
43+
this.postion = this.size - 1;
44+
newObjArr[this.postion] = o;
45+
this.size = limit;
46+
objArr = null;
47+
this.objArr = newObjArr;
48+
}
49+
50+
@Override
51+
public void add(int index, Object o) {
52+
arrIndexVildate(index);
53+
objArr[index - 1] = o;
54+
size++;
55+
}
56+
57+
@Override
58+
public Object get(int index) {
59+
arrIndexVildate(index);
60+
return objArr[index - 1];
61+
}
62+
63+
@Override
64+
public Object remove(int index) {
65+
arrIndexVildate(index);
66+
Object remoteObj = objArr[index - 1];
67+
objArr[index - 1] = null;
68+
size--;
69+
//TODO need GC ccontrol
70+
return remoteObj;
71+
}
72+
73+
@Override
74+
public int size() {
75+
return this.size;
76+
}
77+
78+
@Override
79+
public Iterator iterator() {
80+
return new ArrayListIterator(this);
81+
}
82+
83+
private class ArrayListIterator implements Iterator {
84+
private MyArraryList arraryList;
85+
private int index;
86+
87+
public ArrayListIterator(MyArraryList arraryList) {
88+
this.arraryList = arraryList;
89+
this.index = arraryList.size - 1;
90+
}
91+
92+
@Override
93+
public boolean hasNext() {
94+
if (index > arraryList.size) {
95+
return true;
96+
} else {
97+
return false;
98+
}
99+
}
100+
101+
@Override
102+
public Object next() {
103+
Object obj = arraryList.get(index);
104+
index++;
105+
return obj;
106+
}
107+
}
108+
109+
private void arrIndexVildate(int index) {
110+
if (index > size - 1 || index < 0) {
111+
new Exception(String.format("cant than that array index %s,but got %", size - 1, index));
112+
}
113+
}
114+
115+
//test method
116+
public static void main(String[] args) {
117+
MyArraryList myArrary = new MyArraryList();
118+
MyArrest.arrestEq(10, myArrary.size());
119+
myArrary.add(1, 10);
120+
MyArrest.arrestEq(10, myArrary.get(1));
121+
myArrary.add(100);
122+
System.out.println(myArrary.get(11));
123+
myArrary.remove(1);
124+
MyArrest.arrestIsNull(myArrary.get(1));
125+
if (myArrary.iterator().hasNext()) {
126+
myArrary.iterator().next();
127+
}
128+
System.out.println("test myArrary2");
129+
MyArraryList myArrary2 = new MyArraryList(20);
130+
MyArrest.arrestEq(20, myArrary2.size());
131+
myArrary2.add(1, 10);
132+
MyArrest.arrestEq(10, myArrary2.get(1));
133+
myArrary2.add(100);
134+
MyArrest.arrestIsNull(myArrary2.get(20));
135+
myArrary2.remove(1);
136+
MyArrest.arrestIsNull(myArrary2.get(1));
137+
if (myArrary.iterator().hasNext()) {
138+
myArrary2.iterator().next();
139+
}
140+
}
141+
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
package Impl;
2+
3+
import Interface.Iterator;
4+
import Interface.LinkedList;
5+
import ex.MyArrest;
6+
7+
/**
8+
* Created by Administrator on 2017/2/26.
9+
*/
10+
public class MyLinkedList extends LinkedList {
11+
private MyLinkedList.Node head;
12+
private int size = 1;
13+
14+
public MyLinkedList() {
15+
16+
}
17+
18+
private static class Node {
19+
Object data;
20+
MyLinkedList.Node next;
21+
22+
public Node(Object data, Node next) {
23+
this.data = data;
24+
this.next = next;
25+
}
26+
}
27+
28+
public void add(Object o) {
29+
if (this.size == 1) {
30+
head.data = o;
31+
return;
32+
}
33+
MyLinkedList.Node newHead = new MyLinkedList.Node(o, this.head);
34+
this.size += 1;
35+
this.head = newHead;
36+
}
37+
38+
39+
public void add(int index, Object o) {
40+
IndexVildate(index);
41+
int pos = 0;
42+
if (index == 1) {
43+
this.head = new Node(o, null);
44+
return;
45+
}
46+
for (MyLinkedList.Node node = this.head; node != null; node = node.next) {
47+
pos += 1;
48+
if (pos == index - 1) {
49+
node.data = o;
50+
this.size += 1;
51+
}
52+
}
53+
}
54+
55+
56+
public Object get(int index) {
57+
int pos = 0;
58+
for (MyLinkedList.Node node = this.head; node != null; node = node.next) {
59+
if (pos == index - 1) {
60+
return node.data;
61+
}
62+
pos += 1;
63+
}
64+
return null;
65+
}
66+
67+
68+
public Object remove(int index) {
69+
IndexVildate(index);
70+
int pos = 0;
71+
MyLinkedList.Node preNode;
72+
for (MyLinkedList.Node node = this.head; node != null; node = node.next) {
73+
pos += 1;
74+
if (pos == index - 2) {
75+
//record previous node
76+
preNode = node;
77+
if (pos == index - 1) {
78+
MyLinkedList.Node willDelNode = node;
79+
preNode.next = node.next;
80+
node = null;
81+
this.size -= 1;
82+
return willDelNode;
83+
}
84+
}
85+
}
86+
return null;
87+
}
88+
89+
90+
public int size() {
91+
return this.size;
92+
}
93+
94+
95+
public void addFirst(Object o) {
96+
MyLinkedList.Node newHead = this.head;
97+
newHead.data = o;
98+
newHead.next = this.head;
99+
this.size += 1;
100+
this.head = newHead;
101+
}
102+
103+
104+
public void addLast(Object o) {
105+
for (MyLinkedList.Node node = this.head; node != null; node = node.next) {
106+
if (node.next == null) {
107+
MyLinkedList.Node lastNode = new MyLinkedList.Node(o, null);
108+
node.next = lastNode;
109+
this.size += 1;
110+
}
111+
}
112+
}
113+
114+
115+
public Object removeFirst() {
116+
MyLinkedList.Node oldHead = this.head;
117+
this.head = oldHead.next;
118+
this.size -= 1;
119+
return oldHead;
120+
}
121+
122+
123+
public Object removeLast() {
124+
for (MyLinkedList.Node node = this.head; node != null; node = node.next) {
125+
if (node.next == null) {
126+
MyLinkedList.Node willDelNode = node.next;
127+
node.next = null;
128+
this.size -= 1;
129+
return willDelNode;
130+
}
131+
}
132+
return null;
133+
}
134+
135+
public Iterator iterator() {
136+
return new LinkedListIterator(this);
137+
}
138+
139+
private class LinkedListIterator implements Iterator {
140+
private MyLinkedList linkedList;
141+
private int index;
142+
143+
public LinkedListIterator(MyLinkedList linkedList) {
144+
this.linkedList = linkedList;
145+
this.index = linkedList.size;
146+
}
147+
148+
@Override
149+
public boolean hasNext() {
150+
if (index > linkedList.size) {
151+
return true;
152+
} else {
153+
return false;
154+
}
155+
}
156+
157+
@Override
158+
public Object next() {
159+
Object obj = linkedList.get(index);
160+
index++;
161+
return obj;
162+
}
163+
}
164+
165+
private void IndexVildate(int index) {
166+
if (index > this.size || index < 0) {
167+
System.out.println("happend error");
168+
}
169+
}
170+
171+
public static void main(String[] args) {
172+
MyLinkedList linkedList = new MyLinkedList();
173+
linkedList.add(1, 23);
174+
MyArrest.arrestEqByBasicType(1, linkedList.size());
175+
176+
}
177+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package Impl;
2+
3+
import Interface.Queue;
4+
5+
/**
6+
* Created by Administrator on 2017/2/26.
7+
*/
8+
public class MyQueue extends Queue {
9+
10+
private Node first; // beginning of queue
11+
private Node last; // end of queue
12+
private int size; // number of elements on queue
13+
14+
private static class Node {
15+
private Object value;
16+
private Node next;
17+
18+
public Node(Object value, Node next) {
19+
this.value = value;
20+
this.next = next;
21+
}
22+
}
23+
24+
public MyQueue() {
25+
first = null;
26+
last = null;
27+
int n = 0;
28+
}
29+
30+
@Override
31+
public void enQueue(Object o) {
32+
Node oldlast = this.last;
33+
this.last = new Node(o, null);
34+
size += 1;
35+
//第一个进队列
36+
if (isEmpty()) {
37+
first = last;
38+
} else {
39+
oldlast.next = this.last;
40+
}
41+
42+
}
43+
44+
@Override
45+
public Object deQueue() {
46+
if (isEmpty()) {
47+
return null;
48+
} else {
49+
Node oldFirst = this.first;
50+
Node newFirst = this.first.next;
51+
this.first = null;
52+
this.first = newFirst;
53+
this.size -= 1;
54+
return oldFirst;
55+
56+
}
57+
}
58+
59+
@Override
60+
public boolean isEmpty() {
61+
return first == null;
62+
}
63+
64+
@Override
65+
public int size() {
66+
return size;
67+
}
68+
}

0 commit comments

Comments
 (0)