Skip to content

Commit

Permalink
Merge pull request #33 from zndbl/master
Browse files Browse the repository at this point in the history
2441547139 前三次作业,部分方法未完成
  • Loading branch information
BlindingDark authored Apr 1, 2017
2 parents 4cf00ff + 7b5c997 commit 9785895
Show file tree
Hide file tree
Showing 15 changed files with 989 additions and 0 deletions.
65 changes: 65 additions & 0 deletions group26/2441547139/week1/collection/MyArrayList.java
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;
}
}
115 changes: 115 additions & 0 deletions group26/2441547139/week1/collection/MyLinkedList.java
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;

}


}
29 changes: 29 additions & 0 deletions group26/2441547139/week1/collection/MyQueue.java
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;
}

}
29 changes: 29 additions & 0 deletions group26/2441547139/week1/collection/MyStack.java
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;
}
}
Loading

0 comments on commit 9785895

Please sign in to comment.