Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions group09/790466157/src/com/coding/basic/Iterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.coding.basic;

public interface Iterator {
public boolean hasNext();
public Object next();

}
9 changes: 9 additions & 0 deletions group09/790466157/src/com/coding/basic/List.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.coding.basic;

public interface List {
public void add(Object o);
public void add(int index, Object o);
public Object get(int index);
public Object remove(int index);
public int size();
}
133 changes: 133 additions & 0 deletions group09/790466157/src/com/coding/basic/MyArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package com.coding.basic;
import java.util.Arrays;
import java.util.NoSuchElementException;

import com.coding.basic.List;
import com.coding.basic.Iterator;

public class MyArrayList implements List {

private int size = 0;

private Object[] elementData = new Object[100];

private final static int MAX_ARRAY_LENGTH = Integer.MAX_VALUE;

private static final int DEFAULT_CAPACITY = 10;


//�޳����캯��
public MyArrayList(){
this(DEFAULT_CAPACITY);
}

public MyArrayList(int size){
if (size < 0){
throw new IllegalArgumentException("Ĭ�ϵĴ�С" + size);
}
else{
elementData = new Object[size];
}
}

public void add(Object o){
isCapacityEnough(size+1);
elementData[size++] = o;
}

private void isCapacityEnough(int size){
//�ж��Ƿ񳬹���ʼ�������Ƿ���Ҫ����
if (size > DEFAULT_CAPACITY){
explicitCapacity(size);
}
if (size < 0){
throw new OutOfMemoryError();
}
}

private void explicitCapacity(int capacity){
int oldCapacity = elementData.length;
//������=������ + ��������/2�� ����1.5�������Ʋ������൱�ڳ���2��
int newLength = oldCapacity + (oldCapacity >> 1);
if (newLength - capacity < 0){
newLength = capacity;
}
//�ж�newLength�ij���
//����������涨���������󳤶����ж�Ҫ��Ҫ�����ݿռ��Ƿ����������󳤶�
//���������newLengthΪ MAX_VALUE ������Ϊ MAX_ARRAY_LENGTH��
if (newLength > (MAX_ARRAY_LENGTH)){
newLength = (capacity > MAX_ARRAY_LENGTH ? Integer.MAX_VALUE : MAX_ARRAY_LENGTH);
}
//����copyof��������
elementData = Arrays.copyOf(elementData, newLength);
}

public void add(int index, Object o){

checkRangeForAdd(index);
isCapacityEnough(size +1);
// �� elementData�д�Indexλ�ÿ�ʼ������Ϊsize-index��Ԫ�أ�
// ���������±�Ϊindex+1λ�ÿ�ʼ���µ�elementData�����С�
// ������ǰλ�ڸ�λ�õ�Ԫ���Լ����к���Ԫ������һ��λ�á�
System.arraycopy(elementData, index, elementData, index + 1, size - index);
elementData[index] = o;
size++;
}

//�ж��Ƿ�Խ��
private void checkRangeForAdd(int index){
if (index < 0 || index > size){
throw new IndexOutOfBoundsException("ָ����indexԽ��");
}
}

// ���ش��б���ָ��λ���ϵ�Ԫ�ء�
public Object get(int index){
checkRange(index);
return elementData[index];
}

//�ж��Ƿ�Խ��
private void checkRange(int index){
if (index >= size || index < 0){
throw new IndexOutOfBoundsException("ָ����indexԽ��");
}
}

public Object remove(int index){
Object value = get(index);
int moveSize = size - index -1;
if (moveSize >0){
System.arraycopy(elementData, index +1, elementData, index, size - index -1);

}
elementData[--size] = null;
return value;
}

public int size(){
return size;
}

//������
public Iterator iterator(Object o){
return new ArrayListIterator();
}


private class ArrayListIterator implements Iterator{
private int currentIndex=0;

public boolean hasNext() {
return currentIndex < size();
}

public Object next() {
if (!hasNext()){
throw new NoSuchElementException();
}
return new Object[currentIndex + 1];
}
}

}
159 changes: 159 additions & 0 deletions group09/790466157/src/com/coding/basic/MyLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package com.coding.basic;

public class MyLinkedList<E> {

private int size;

private Node<E> first;

private Node<E> last;


public boolean add(E element) {
addAtLast(element);
return true;
}

private void addAtLast(E element) {
Node<E> l = last;
Node<E> node = new Node<E>(element, null, l);
last = node;
if (l == null) {
first = node;
} else {
l.next = node;
}
size++;
}

public void add(int index, E element) {
checkRangeForAdd(index);
if (index == size) {
addAtLast(element);
} else {
Node<E> l = node(index);
addBeforeNode(element, l);
}
}

private void addBeforeNode(E element, Node<E> specifiedNode) {
Node<E> preNode = specifiedNode.prev;
Node<E> newNode = new Node<E>(element, specifiedNode, preNode);
if (preNode == null) {
first = newNode;
} else {
preNode.next = newNode;
}
specifiedNode.prev = newNode;
size++;
}


private Node<E> node(int index) {
if (index < (size << 1)) {
Node<E> cursor = first;
for (int i = 0; i < index; i++) {
cursor = cursor.next;
}
return cursor;
} else {
Node<E> cursor = last;
for (int i = size - 1; i > index; i--) {
cursor = cursor.prev;
}
return cursor;
}
}

private void checkRangeForAdd(int index) {
if (index > size || index < 0) {
throw new IndexOutOfBoundsException("ָ����index��������");
}
}

public E get(int index) {
checkRange(index);
return node(index).item;
}

private void checkRange(int index) {
if (index >= size || index < 0) {
throw new IndexOutOfBoundsException("ָ��index��������");
}
}

public int indexOf(Object element) {
Node<E> cursor = first;
int count = 0;
while (cursor != null) {
if (element != null) {
if (element.equals(cursor.item)) {
return count;
}
}else{
if (cursor.item == null) {
return count;
}
}
count ++;
cursor = cursor.next;
}
return -1;
}

public E remove(int index) {
checkRange(index);
return deleteLink(index);
}

public boolean remove(Object o) {
int index = indexOf(o);
if (index < 0){
return false;
}
deleteLink(index);
return true;
}

private E deleteLink(int index) {
Node<E> l = node(index);
E item = l.item;
Node<E> prevNode = l.prev;
Node<E> 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;
}



public int size(){
return size;
}
private static class Node<E> {
E item;
Node<E> next;
Node<E> prev;

public Node(E item, Node<E> next, Node<E> prev) {
this.item = item;
this.next = next;
this.prev = prev;

}
}
}
68 changes: 68 additions & 0 deletions group09/790466157/src/com/coding/basic/Queue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.coding.basic;
import java.util.Arrays;

public class Queue {
private static final int CAPACITY = 10;
private static int capacity;
private static int front;
private static int tail;
private static Object[] array;

public Queue(){
this.capacity = CAPACITY;
array = new Object[capacity];
front = tail = 0;
}

public void enQueue(Object o) throws ExceptionQueueFull {
if (size() == capacity -1)
throw new ExceptionQueueFull("Queue is full");
array[tail] = o;
tail = (tail +1) % capacity;
}

public Object deQueue() throws ExceptionQueueEmpty{
Object o;
if (isEmpty())
throw new ExceptionQueueEmpty("Queue is empty");
o = array[front];
front = (front + 1) % capacity;
return o;
}

public boolean isEmpty(){
return (front == tail);
}

public int size(){
if (isEmpty())
return 0;
else
return (capacity + tail - front) % capacity;
}

public class ExceptionQueueEmpty extends Exception {
// Constructor
public ExceptionQueueEmpty() {

}

// Constructor with parameters
public ExceptionQueueEmpty(String mag) {
System.out.println(mag);
}
}

public class ExceptionQueueFull extends Exception {

// Constructor
public ExceptionQueueFull() {

}

// Constructor with parameters
public ExceptionQueueFull(String mag) {
System.out.println(mag);
}
}
}
Loading