-
Notifications
You must be signed in to change notification settings - Fork 641
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 #33 from zndbl/master
2441547139 前三次作业,部分方法未完成
- Loading branch information
Showing
15 changed files
with
989 additions
and
0 deletions.
There are no files selected for viewing
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,65 @@ | ||
package week1.collection; | ||
|
||
import java.util.Arrays; | ||
/** | ||
* Created by zndbl on 2017/3/11. | ||
*/ | ||
public class MyArrayList { | ||
|
||
private int size; | ||
private Object[] arr; | ||
|
||
public MyArrayList() { | ||
this(10); | ||
} | ||
|
||
public MyArrayList(int oldLength) { | ||
if(oldLength < 0) { | ||
throw new RuntimeException("创建集合失败"); | ||
} | ||
arr = new Object[oldLength]; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
/** | ||
* 数组的长度扩充策略 | ||
*/ | ||
public void ensureCapacity(int minCapatity ) { | ||
int oldCapacity = arr.length; | ||
if(minCapatity > oldCapacity) { | ||
int newCapatity = 3 * oldCapacity / 2 + 1; | ||
if(minCapatity > newCapatity) { | ||
newCapatity = minCapatity; | ||
} | ||
arr = Arrays.copyOf(arr,newCapatity); | ||
} | ||
} | ||
|
||
public void add(Object element) { | ||
ensureCapacity(size+1); | ||
arr[size++] = element; | ||
} | ||
|
||
public void add(int index, Object element) { | ||
if(index < 0 || index > size) { | ||
throw new RuntimeException("数组越界"); | ||
} | ||
ensureCapacity(size+1); | ||
System.arraycopy(arr,index,arr,index+1,size-index); | ||
arr[index] = element; | ||
size++; | ||
} | ||
|
||
public boolean remove(Object o) { | ||
for(int i=0; i<size; i++) { | ||
if(arr[i].equals(o)) { | ||
int num = size - i - 1; | ||
System.arraycopy(arr, i+1, arr, i, num); | ||
} | ||
} | ||
return true; | ||
} | ||
} |
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,115 @@ | ||
package week1.collection; | ||
|
||
/** | ||
* Created by zndbl on 2017/3/11. | ||
*/ | ||
public class MyLinkedList { | ||
|
||
private int size; | ||
private Node first; | ||
private Node last; | ||
|
||
public static class Node{ | ||
Object item; | ||
Node next; | ||
Node prev; | ||
|
||
public Node(Object item, Node next, Node prev) { | ||
this.item = item; | ||
this.next = next; | ||
this.prev = prev; | ||
} | ||
} | ||
|
||
public boolean add(Object element) { | ||
addAtLast(element); | ||
return true; | ||
} | ||
|
||
public void addAtLast(Object element) { | ||
Node l = last; | ||
Node node = new Node(element,null,l); | ||
last = node; | ||
if(l == null) { | ||
first = node; | ||
} else { | ||
l.next = node; | ||
} | ||
size++; | ||
} | ||
|
||
public Node node(int index) { | ||
if(index < size/2 ) { | ||
Node cussor = first; | ||
for (int i = 0; i < index ; i++) { | ||
cussor = cussor.next; | ||
} | ||
return cussor; | ||
} else { | ||
Node cussor = last; | ||
for (int i = size -1 ; i > index ; i--) { | ||
cussor = cussor.prev; | ||
} | ||
return cussor; | ||
} | ||
} | ||
|
||
public Object get(int index) { | ||
checkRange(index); | ||
return node(index).item; | ||
} | ||
|
||
public void checkRange(int index) { | ||
if(index >= size || index < 0) { | ||
throw new RuntimeException("index超过界限"); | ||
} | ||
} | ||
|
||
public int indexOf(Object element) { | ||
Node cussor = first; | ||
int count = 0; | ||
while (cussor != null) { | ||
if(element.equals(cussor.item)) { | ||
return count; | ||
} | ||
count++; | ||
cussor = cussor.next; | ||
} | ||
return -1; | ||
} | ||
|
||
public boolean remove(Object o) { | ||
int index = indexOf(o); | ||
if(index < 0) { | ||
return false; | ||
} | ||
deleteLink(index); | ||
return true; | ||
} | ||
|
||
public Object deleteLink(int index) { | ||
Node l = node(index); | ||
Object item = l.item; | ||
Node prevNode = l.prev; | ||
Node nextNode = l.next; | ||
|
||
if(prevNode == null) { | ||
first = nextNode; | ||
} else { | ||
prevNode.next = nextNode; | ||
l.next = null; | ||
} | ||
if(nextNode == null) { | ||
last = prevNode; | ||
} else { | ||
nextNode.prev = prevNode; | ||
l.prev = null; | ||
} | ||
size--; | ||
l.item = null; | ||
return item; | ||
|
||
} | ||
|
||
|
||
} |
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,29 @@ | ||
package week1.collection; | ||
|
||
/** | ||
* Created by zndbl on 2017/3/12. | ||
*/ | ||
public class MyQueue { | ||
|
||
private Object[] data; | ||
private int head; | ||
private int tail; | ||
|
||
public MyQueue() { | ||
data = new Object[10]; | ||
head = 1; | ||
tail = 1; | ||
} | ||
|
||
public void put(Object obj) { | ||
data[tail] = obj; | ||
tail++; | ||
} | ||
|
||
public Object get() { | ||
Object obj = data[head]; | ||
head++; | ||
return obj; | ||
} | ||
|
||
} |
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,29 @@ | ||
package week1.collection; | ||
|
||
/** | ||
* Created by zndbl on 2017/3/12. | ||
*/ | ||
public class MyStack { | ||
|
||
private Object[] data; | ||
private int top; | ||
|
||
public MyStack() { | ||
data = new Object[100]; | ||
top = -1; | ||
} | ||
|
||
public void put(Object t) { | ||
data[data.length] = t; | ||
top++; | ||
} | ||
|
||
public Object pop() { | ||
if(top < 0) { | ||
return null; | ||
} | ||
Object object = data[top]; | ||
top--; | ||
return object; | ||
} | ||
} |
Oops, something went wrong.