Skip to content

Commit c399cd9

Browse files
authored
Merge pull request onlyliuxin#8 from lqt0223/master
Collections in Java
2 parents 91eeb6e + eddddc3 commit c399cd9

File tree

8 files changed

+388
-0
lines changed

8 files changed

+388
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.coding.basic;
2+
3+
public class ArrayList<E> implements List<E>, Iterable<E>{
4+
private E[] array;
5+
private int lastIndex;
6+
private int length;
7+
//Constructor
8+
@SuppressWarnings("unchecked")
9+
public ArrayList(){
10+
length = 10;
11+
array = (E[])new Object[length];
12+
lastIndex = 0;
13+
}
14+
15+
public void add(E object){
16+
if(lastIndex == length){
17+
this.grow();
18+
}
19+
array[lastIndex] = object;
20+
lastIndex++;
21+
}
22+
23+
@SuppressWarnings("unchecked")
24+
private void grow(){
25+
E[] tempArray = (E[])new Object[length + 10];
26+
System.arraycopy(array, 0, tempArray, 0, length);
27+
array = tempArray;
28+
length = length + 10;
29+
}
30+
31+
public void insert(int index, E o) {
32+
if(index > lastIndex - 1) throw new IndexOutOfBoundsException();
33+
for (int i = lastIndex; i > index; i--) {
34+
array[i] = array[i - 1];
35+
}
36+
array[index] = o;
37+
length++;
38+
}
39+
40+
public E get(int index) {
41+
if(index > lastIndex - 1) throw new IndexOutOfBoundsException();
42+
return array[index];
43+
}
44+
45+
public E remove(int index){
46+
if(index > lastIndex - 1) throw new IndexOutOfBoundsException();
47+
E removed = array[index];
48+
for (int i = index; i < lastIndex - 1; i++) {
49+
array[i] = array[i + 1];
50+
}
51+
lastIndex--;
52+
array[lastIndex] = null;
53+
return removed;
54+
}
55+
56+
public int size(){
57+
return lastIndex;
58+
}
59+
60+
public Iterator<E> iterator(){
61+
return new Itr(this);
62+
}
63+
64+
private class Itr implements Iterator<E>{
65+
private int itrCurIndex;
66+
private ArrayList arrayList;
67+
// constructor
68+
public Itr(ArrayList arrayList){
69+
this.arrayList = arrayList;
70+
itrCurIndex = -1;
71+
}
72+
73+
public boolean hasNext(){
74+
return (itrCurIndex + 1) > lastIndex - 1 ? false: true;
75+
}
76+
77+
@SuppressWarnings("unchecked")
78+
public E next(){
79+
if(this.hasNext()){
80+
return (E)this.arrayList.get(++itrCurIndex);
81+
}else{
82+
itrCurIndex = -1;
83+
return null;
84+
}
85+
}
86+
87+
@SuppressWarnings("unchecked")
88+
public E remove(){
89+
return (E)this.arrayList.remove(itrCurIndex);
90+
}
91+
}
92+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// This is a node in a customized binaryTree. The tree have 2 extra features comparing to general binary trees.
2+
// 1. The data of each node are in number class.
3+
// 2. The left child node has a smaller number data than root node, and the right child node has a larger number data that root node.
4+
5+
package com.coding.basic;
6+
7+
public class BinaryTreeNode<E> {
8+
9+
private E data;
10+
private BinaryTreeNode left;
11+
private BinaryTreeNode right;
12+
13+
// constructor
14+
public BinaryTreeNode(E data){
15+
this.data = data;
16+
}
17+
18+
public E getData() {
19+
return this.data;
20+
}
21+
22+
public void setData(E data) {
23+
this.data = data;
24+
}
25+
26+
public BinaryTreeNode getLeft() {
27+
return this.left;
28+
}
29+
30+
public boolean setLeft(BinaryTreeNode<E> left) {
31+
if(this.compareWithRoot(left.data) >= 0 || this.left != null){
32+
System.err.println("The left node data should be smaller than root node.");
33+
return false;
34+
}else{
35+
this.left = left;
36+
return true;
37+
}
38+
}
39+
40+
public BinaryTreeNode getRight() {
41+
return this.right;
42+
}
43+
44+
public boolean setRight(BinaryTreeNode<E> right) {
45+
if(this.compareWithRoot(right.data) <= 0 || this.right != null) {
46+
System.err.println("The right node data should be larger than root node.");
47+
48+
return false;
49+
}else{
50+
this.right = right;
51+
return true;
52+
}
53+
}
54+
55+
private int compareWithRoot(E o){
56+
return (Integer)o - (Integer)this.getData();
57+
}
58+
59+
@SuppressWarnings("unchecked")
60+
public void insert(E o){
61+
BinaryTreeNode newNode = new BinaryTreeNode(o);
62+
if(!this.setLeft(newNode)){
63+
if(!this.setRight(newNode)){
64+
if(this.left.getData() == o){
65+
this.right.insert(o);
66+
}else{
67+
this.left.insert(o);
68+
}
69+
}
70+
}
71+
}
72+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.coding.basic;
2+
3+
public interface Iterable<E>{
4+
public Iterator<E> iterator();
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.coding.basic;
2+
3+
public interface Iterator<E> {
4+
public boolean hasNext();
5+
public E next();
6+
public E remove();
7+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package com.coding.basic;
2+
3+
public class LinkedList<E> implements List<E>, Iterable<E> {
4+
5+
private Node head;
6+
private Node last;
7+
private int length;
8+
9+
private class Node{
10+
public E data;
11+
public Node next;
12+
13+
// constructor
14+
private Node(E o, Node n){
15+
data = o;
16+
next = n;
17+
}
18+
}
19+
20+
// constructor
21+
public LinkedList(){
22+
head = new Node(null, null);
23+
last = head;
24+
length = 0;
25+
}
26+
27+
public void add(E o){
28+
Node newNode = new Node(o, null);
29+
last.next = newNode;
30+
last = newNode;
31+
length++;
32+
}
33+
34+
public void insert(int index , E o){
35+
if(index > length - 1) throw new IndexOutOfBoundsException();
36+
Node prevNode = this.getNode(index - 1);
37+
Node nextNode = this.getNode(index);
38+
Node nodeToInsert = new Node(o,nextNode);
39+
prevNode.next = nodeToInsert;
40+
length++;
41+
}
42+
43+
private Node getNode(int index){
44+
int count = 0;
45+
Node currentNode = head;
46+
while(currentNode.next != null && count <= index){
47+
currentNode = currentNode.next;
48+
count++;
49+
}
50+
return currentNode;
51+
}
52+
53+
public E get(int index){
54+
if(index > length - 1) throw new IndexOutOfBoundsException();
55+
Node nodeAtIndex = this.getNode(index);
56+
return nodeAtIndex.data;
57+
}
58+
59+
public E remove(int index){
60+
if(index > length - 1) throw new IndexOutOfBoundsException();
61+
Node nodeToRemove = this.getNode(index);
62+
Node prevNode = this.getNode(index - 1);
63+
Node nextNode = this.getNode(index + 1);
64+
prevNode.next = nextNode;
65+
E removedData = nodeToRemove.data;
66+
nodeToRemove = null;
67+
length--;
68+
return removedData;
69+
}
70+
71+
public int size(){
72+
return length;
73+
}
74+
75+
public void addFirst(E o){
76+
this.insert(0, o);
77+
}
78+
public void addLast(E o){
79+
this.add(o);
80+
81+
}
82+
public E removeFirst(){
83+
return this.remove(0);
84+
}
85+
public E removeLast(){
86+
return this.remove(length - 1);
87+
}
88+
89+
public Iterator<E> iterator(){
90+
return new Itr(this);
91+
}
92+
93+
private class Itr implements Iterator<E>{
94+
private int itrCurIndex;
95+
private Node currentNode;
96+
private LinkedList linkedList;
97+
98+
public Itr(LinkedList linkedList){
99+
itrCurIndex = -1;
100+
currentNode = head;
101+
this.linkedList = linkedList;
102+
}
103+
104+
public boolean hasNext(){
105+
return (itrCurIndex + 1) > length - 1 ? false: true;
106+
}
107+
108+
@SuppressWarnings("unchecked")
109+
public E next(){
110+
if(this.hasNext()){
111+
return (E)this.linkedList.get(++itrCurIndex);
112+
}else{
113+
itrCurIndex = -1;
114+
return null;
115+
}
116+
}
117+
118+
@SuppressWarnings("unchecked")
119+
public E remove(){
120+
return (E)this.linkedList.remove(itrCurIndex);
121+
}
122+
}
123+
}
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+
public interface List<E> {
4+
public void add(E o);
5+
public void insert(int index, E o);
6+
public E get(int index);
7+
public E remove(int index);
8+
public int size();
9+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.coding.basic;
2+
3+
public class Queue<E> {
4+
private LinkedList<E> linkedList;
5+
6+
// constructor
7+
public Queue(){
8+
linkedList = new LinkedList<E>();
9+
}
10+
11+
public void enQueue(E o){
12+
linkedList.addLast(o);
13+
}
14+
15+
public E deQueue(){
16+
return linkedList.removeFirst();
17+
}
18+
19+
public E peek(){
20+
return linkedList.get(0);
21+
}
22+
23+
public boolean isEmpty(){
24+
return linkedList.size() == 0 ? true : false;
25+
}
26+
27+
public int size(){
28+
return linkedList.size();
29+
}
30+
}

0 commit comments

Comments
 (0)