Skip to content

Commit f1ca044

Browse files
author
leijing
committed
同步fork的代码
2 parents ccf6e6d + d9c0269 commit f1ca044

File tree

559 files changed

+28821
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

559 files changed

+28821
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package com.coding.basic;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Created by zhangjiatao on 2017/2/25.
7+
*/
8+
public class ArrayList implements List {
9+
private int size = 0;
10+
private Object[] elementData = new Object[100];
11+
12+
@Override
13+
public void add(Object o) {
14+
if(isFull()) {
15+
elementData = expandArray(elementData);
16+
}
17+
elementData[size++] = o;
18+
}
19+
20+
@Override
21+
public void add(int index, Object o) {
22+
ifOutOfBounds(index);
23+
if(isFull()) {
24+
elementData = expandArray(elementData);
25+
}
26+
System.arraycopy(elementData,index,elementData,index+1,size++);
27+
elementData[index] = o;
28+
}
29+
30+
@Override
31+
public Object get(int index) {
32+
ifOutOfBounds(index);
33+
return elementData[index];
34+
}
35+
36+
@Override
37+
public Object remove(int index) {
38+
ifOutOfBounds(index);
39+
Object delData = elementData[index];
40+
System.arraycopy(elementData,index+1,elementData,index,size-index);
41+
size--;
42+
return index;
43+
}
44+
45+
@Override
46+
public int size() {
47+
return size;
48+
}
49+
50+
@Override
51+
public String toString() {
52+
String str = "";
53+
for(int i=0; i<=size-1; i++){
54+
str += elementData[i] + " ";
55+
}
56+
return str;
57+
}
58+
59+
private boolean isFull() {
60+
if(size >= elementData.length-1){
61+
return true;
62+
}
63+
return false;
64+
}
65+
66+
public boolean isEmpty() {
67+
if(size == 0) {
68+
return true;
69+
}
70+
return false;
71+
}
72+
73+
private void ifOutOfBounds(int index) {
74+
if(index >= size || index < 0) {
75+
throw new IndexOutOfBoundsException();
76+
}
77+
}
78+
79+
private Object[] expandArray(Object[] arr) {
80+
return Arrays.copyOf(arr, arr.length+10);
81+
}
82+
83+
public Iterator iterator = new ArrayListIterator(this);
84+
85+
private class ArrayListIterator implements Iterator{
86+
87+
private ArrayList arrList;
88+
int position = 0,length;
89+
90+
private ArrayListIterator(ArrayList arrList){
91+
this.arrList = arrList;
92+
this.length = arrList.size();
93+
}
94+
95+
@Override
96+
public boolean hasNext() {
97+
return position < length;
98+
}
99+
100+
@Override
101+
public Object next() {
102+
return arrList.get(position++);
103+
}
104+
105+
}
106+
107+
public static void main(String[] args) {
108+
ArrayList arr = new ArrayList();
109+
arr.add(1);
110+
arr.add(2);
111+
arr.add(3);
112+
arr.add(1,4);
113+
arr.remove(2);
114+
System.out.println(arr.toString());
115+
System.out.println(arr.size());
116+
System.out.println(arr.iterator.next());
117+
System.out.println(arr.iterator.next());
118+
}
119+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.coding.basic;
2+
3+
/**
4+
* Created by zhangjiatao on 2017/2/25.
5+
*/
6+
public class BinaryTreeNode {
7+
private Object data;
8+
private BinaryTreeNode left;
9+
private BinaryTreeNode right;
10+
11+
public BinaryTreeNode(Object o) {
12+
this.data = o;
13+
this.left = null;
14+
this.right = null;
15+
}
16+
17+
public int getData() {
18+
return (int) data;
19+
}
20+
21+
public void setData(Object data) {
22+
this.data = data;
23+
}
24+
25+
public BinaryTreeNode getLeft() {
26+
return left;
27+
}
28+
29+
public void setLeft(BinaryTreeNode left) {
30+
this.left = left;
31+
}
32+
33+
public BinaryTreeNode getRight() {
34+
return right;
35+
}
36+
37+
public void setRight(BinaryTreeNode right) {
38+
this.right = right;
39+
}
40+
41+
public BinaryTreeNode insert(Object o) {
42+
BinaryTreeNode newNode = new BinaryTreeNode(o);
43+
insertInto(this, newNode);
44+
return this;
45+
}
46+
47+
private void insertInto(BinaryTreeNode tree, BinaryTreeNode o) {
48+
if (o.getData() <= tree.getData()) {
49+
if (tree.getLeft() != null) insertInto(tree.getLeft(), o);
50+
else tree.setLeft(o);
51+
} else {
52+
if (tree.getRight() != null) insertInto(tree.getRight(), o);
53+
else tree.setRight(o);
54+
}
55+
}
56+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.coding.basic;
2+
3+
/**
4+
* Created by zhangjiatao on 2017/2/25.
5+
*/
6+
public interface Iterator {
7+
public boolean hasNext();
8+
public Object next();
9+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
package com.coding.basic;
2+
3+
import java.util.Objects;
4+
5+
/**
6+
* Created by zhangjiatao on 2017/2/25.
7+
*/
8+
public class LinkedList implements List {
9+
private Node head = null,tail = null;
10+
private int size=0;
11+
private LinkedListIterator iterator = null;
12+
13+
@Override
14+
public void add(Object o) {
15+
Node newNode = new Node(o);
16+
if(tail == null) {
17+
head = tail = newNode;
18+
}else {
19+
tail.next = newNode;
20+
tail = newNode;
21+
}
22+
size++;
23+
}
24+
25+
@Override
26+
public void add(int index, Object o) {
27+
ifOutOfBounds(index);
28+
Node temp = head,previousNode = head;
29+
Node newNode = new Node(o);
30+
if(index == 0) {
31+
newNode.next = head;
32+
head = newNode;
33+
return ;
34+
}
35+
while(index-- > 0) {
36+
previousNode = temp;
37+
temp = temp.next;
38+
}
39+
previousNode.next = newNode;
40+
newNode.next = temp;
41+
size++;
42+
}
43+
44+
@Override
45+
public Object get(int index) {
46+
ifOutOfBounds(index);
47+
Node temp = head;
48+
while(index-- > 0) {
49+
temp = temp.next;
50+
}
51+
return temp;
52+
}
53+
54+
@Override
55+
public Object remove(int index) {
56+
ifOutOfBounds(index);
57+
Node temp = head,previousNode = head;
58+
if(head == tail) {
59+
head = tail = null;
60+
return temp;
61+
}
62+
while(index-- > 0) {
63+
previousNode = temp;
64+
temp = temp.next;
65+
}
66+
if(tail == temp) {
67+
tail = previousNode;
68+
}
69+
previousNode.next = temp.next;
70+
size--;
71+
return temp;
72+
}
73+
74+
@Override
75+
public int size() {
76+
return size;
77+
}
78+
79+
public void addFirst(Object o) {
80+
add(0,o);
81+
}
82+
83+
public void addLast(Object o) {
84+
add(o);
85+
}
86+
87+
public Object removeFirst() {
88+
return remove(0);
89+
}
90+
91+
public Object removeLast() {
92+
return remove(size()-1);
93+
}
94+
95+
public LinkedListIterator Iterator() {
96+
if(iterator ==null) {
97+
iterator = new LinkedListIterator(this);
98+
}
99+
return iterator;
100+
}
101+
102+
private void ifOutOfBounds(int index) {
103+
if(index >= size || index < 0) {
104+
throw new IndexOutOfBoundsException();
105+
}
106+
}
107+
108+
private boolean isEmpty() {
109+
return size==0;
110+
}
111+
112+
public String toString() {
113+
String str = "";
114+
Node temp = head;
115+
while(temp!=tail) {
116+
str = str + temp.data + " ";
117+
temp = temp.next;
118+
}
119+
return str + tail.data;
120+
}
121+
122+
private static class Node {
123+
Object data;
124+
Node next;
125+
public Node(Object o) {
126+
this.data = o;
127+
this.next = null;
128+
}
129+
}
130+
131+
private class LinkedListIterator implements Iterator {
132+
private LinkedList linkedList = null;
133+
private Node currentNode = null;
134+
135+
public LinkedListIterator(LinkedList linkedList) {
136+
this.linkedList = linkedList;
137+
this.currentNode = this.linkedList.head;
138+
}
139+
140+
@Override
141+
public boolean hasNext() {
142+
if(linkedList.isEmpty()) return false;
143+
return currentNode.next != null;
144+
}
145+
146+
@Override
147+
public Object next() {
148+
Object data = currentNode.data;
149+
if(hasNext()) {
150+
currentNode = currentNode.next;
151+
}
152+
return data;
153+
}
154+
}
155+
156+
public static void main(String[] args) {
157+
LinkedList arr = new LinkedList();
158+
arr.add(1);
159+
arr.add(2);
160+
arr.add(3);
161+
arr.add(5);
162+
arr.add(1,4);
163+
arr.remove(2);
164+
arr.removeLast();
165+
arr.addFirst(5);
166+
System.out.println(arr.toString());
167+
System.out.println(arr.size());
168+
System.out.println(arr.Iterator().hasNext());
169+
System.out.println(arr.Iterator().next());
170+
System.out.println(arr.Iterator().next());
171+
System.out.println(arr.Iterator().next());
172+
System.out.println(arr.Iterator().next());
173+
}
174+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.coding.basic;
2+
3+
/**
4+
* Created by zhangjiatao on 2017/2/25.
5+
*/
6+
public interface List {
7+
public void add(Object o);
8+
public void add(int index, Object o);
9+
public Object get(int index);
10+
public Object remove(int index);
11+
public int size();
12+
}

0 commit comments

Comments
 (0)