forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from silencehe09/master
基础集合类实现
- Loading branch information
Showing
6 changed files
with
372 additions
and
0 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
group19/709960951/CodeLearning/src/com/coding/basic/ArrayList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.Arrays; | ||
|
||
public class ArrayList implements List { | ||
private static final int INI_SIZE = 100; | ||
private static final int EXTENDED_SIZE = 100; // 每次扩容的大小 | ||
private int size = 0; | ||
private Object[] elements = new Object[INI_SIZE]; | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
if (size < 1) { | ||
index = 0; | ||
} else { | ||
if (index < 0) { | ||
index = 0; | ||
} | ||
if (index > size - 1) { | ||
index = size - 1; | ||
} | ||
} | ||
|
||
ensureCapacity(); | ||
for (int i = size - 1; i >= index; i--) { | ||
elements[i + 1] = elements[i]; | ||
} | ||
elements[index] = o; | ||
size++; | ||
} | ||
|
||
@Override | ||
public void add(Object o) { | ||
ensureCapacity(); | ||
elements[size] = o; | ||
size++; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
if (size < 1 || index < 0 || index > size - 1) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
return elements[index]; | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
if (size < 1 || index < 0 || index > size - 1) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
Object object = elements[index]; | ||
for (int i = index; i < size - 1; i++) { | ||
elements[i] = elements[i + 1]; | ||
} | ||
elements[size - 1] = null; | ||
size--; | ||
adjustCapacity(); // 调整数组至合适大小 | ||
return object; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
// 底层数组最多有2*EXTENDED_SIZE个多余空间 | ||
private void adjustCapacity() { | ||
if ((size + 2 * EXTENDED_SIZE) < elements.length) { | ||
elements = Arrays.copyOf(elements, size + 2 * EXTENDED_SIZE); | ||
} | ||
} | ||
|
||
// 每次添加元素时,检查底层数组的长度,保证存储空间 | ||
private void ensureCapacity() { | ||
if (size == elements.length) { | ||
elements = Arrays.copyOf(elements, elements.length + EXTENDED_SIZE); | ||
} | ||
} | ||
|
||
public Iterator iterator() { | ||
return new ArrayListIterator(); | ||
} | ||
|
||
private class ArrayListIterator implements Iterator { | ||
|
||
private int curIndex = 0; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if (size > 0 && curIndex < size) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
if (!hasNext()) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
Object object = elements[curIndex]; | ||
curIndex++; | ||
return object; | ||
} | ||
|
||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
group19/709960951/CodeLearning/src/com/coding/basic/Iterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.coding.basic; | ||
|
||
public interface Iterator { | ||
boolean hasNext(); | ||
Object next(); | ||
} |
183 changes: 183 additions & 0 deletions
183
group19/709960951/CodeLearning/src/com/coding/basic/LinkedList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
package com.coding.basic; | ||
|
||
public class LinkedList implements List { | ||
private static class Node { | ||
Object data; | ||
Node next; | ||
} | ||
|
||
private int size = 0; // 初始化size=0 | ||
private Node head = null; | ||
|
||
@Override | ||
public void add(Object o) { | ||
Node node = new Node(); | ||
node.data = o; | ||
node.next = null; | ||
if (head == null) { | ||
head = node; | ||
} else { | ||
Node tail = head; | ||
while (tail.next != null) { | ||
tail = tail.next; | ||
} | ||
tail.next = node; | ||
} | ||
size++; | ||
} | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
if (size < 1) { | ||
index = 0; | ||
} else { | ||
if (index < 0) { | ||
index = 0; | ||
} | ||
if (index > size - 1) { | ||
index = size - 1; | ||
} | ||
} | ||
Node p = null;// 插入位置的前一节点 | ||
Node q = head; | ||
while (index > 0) { | ||
p = q; | ||
q = q.next; | ||
index--; | ||
} | ||
Node node = new Node(); | ||
node.data = o; | ||
node.next = null; | ||
if (p == null) { | ||
node.next = head; | ||
head = node; | ||
} else { | ||
node.next = p.next; | ||
p.next = node; | ||
} | ||
size++; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
if (index < 0 || index > size - 1) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
Node p = head; | ||
while (index > 0) { | ||
p = p.next; | ||
index--; | ||
} | ||
return p.data; | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
if (index < 0 || index > size - 1) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
Node removeObj; | ||
Node p = null; | ||
Node q = head; | ||
while (index > 0) { | ||
p = q; | ||
q = q.next; | ||
index--; | ||
} | ||
if (p == null) { | ||
removeObj = head; | ||
head = head.next; | ||
} else { | ||
removeObj = p.next; | ||
p.next = removeObj.next; | ||
} | ||
size--; | ||
return removeObj.data; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
public void addFirst(Object o) { | ||
Node node = new Node(); | ||
node.data = o; | ||
|
||
node.next = head; | ||
|
||
head = node; | ||
size++; | ||
} | ||
|
||
public void addLast(Object o) { | ||
Node node = new Node(); | ||
node.data = o; | ||
|
||
if (head == null) { | ||
head = node; | ||
} else { | ||
Node p = head; | ||
while (p.next != null) { | ||
p = p.next; | ||
} | ||
p.next = node; | ||
} | ||
size++; | ||
} | ||
|
||
public Object removeFirst() { | ||
if (size < 1) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
Node removeObj = head; | ||
head = head.next; | ||
size--; | ||
return removeObj.data; | ||
} | ||
|
||
public Object removeLast() { | ||
if (size < 1) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
Node removeObj; | ||
if (head.next == null) { | ||
removeObj = head; | ||
head = null; | ||
} else { | ||
Node p = head; | ||
while (p.next.next != null) { | ||
p = p.next; | ||
} | ||
removeObj = p.next; | ||
p.next = null; | ||
} | ||
size--; | ||
return removeObj.data; | ||
} | ||
|
||
public Iterator iterator() { | ||
return new LinkedListIterator(); | ||
} | ||
|
||
private class LinkedListIterator implements Iterator { | ||
|
||
Node curNode = head; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return curNode != null; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
if (!hasNext()) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
Object object = curNode.data; | ||
curNode = curNode.next; | ||
return object; | ||
} | ||
|
||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
group19/709960951/CodeLearning/src/com/coding/basic/List.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.coding.basic; | ||
|
||
public interface List { | ||
void add(Object o); | ||
|
||
void add(int index, Object o); | ||
|
||
Object get(int index); | ||
|
||
Object remove(int index); | ||
|
||
int size(); | ||
} |
26 changes: 26 additions & 0 deletions
26
group19/709960951/CodeLearning/src/com/coding/basic/Queue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.coding.basic; | ||
|
||
public class Queue { | ||
|
||
private LinkedList list = new LinkedList(); | ||
|
||
public void enQueue(Object o) { | ||
list.addFirst(o); | ||
} | ||
|
||
public Object deQueue() { | ||
if (isEmpty()) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
Object object = list.removeLast(); | ||
return object; | ||
} | ||
|
||
public boolean isEmpty() { | ||
return list.size() == 0; | ||
} | ||
|
||
public int size() { | ||
return list.size(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
group19/709960951/CodeLearning/src/com/coding/basic/Stack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.coding.basic; | ||
|
||
|
||
public class Stack { | ||
|
||
private ArrayList list=new ArrayList(); | ||
public void push(Object o) | ||
{ | ||
list.add(o); | ||
} | ||
public Object pop() | ||
{ | ||
if(isEmpty()) | ||
{ | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
return list.remove(list.size()-1); | ||
} | ||
public Object peak() | ||
{ | ||
if(isEmpty()) | ||
{ | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
return list.get(list.size()-1); | ||
} | ||
|
||
public boolean isEmpty() { | ||
return list.size()==0; | ||
} | ||
|
||
public int size() { | ||
return list.size(); | ||
} | ||
} |