Skip to content

Commit

Permalink
Merge pull request #2 from Greastate/master
Browse files Browse the repository at this point in the history
123
  • Loading branch information
BigBangGe authored Feb 26, 2017
2 parents bc46fec + b394c4e commit be3ef60
Show file tree
Hide file tree
Showing 12 changed files with 495 additions and 2 deletions.
111 changes: 111 additions & 0 deletions group08/769638826/2-27/ArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.coding.basic;

import java.util.Arrays;
import java.util.NoSuchElementException;

/**
* Created by huitailang on 17/2/25.
*
* @author zhangkun
* @date 2017年02月25日13:23:30
*/
public class ArrayList implements List {
private int size = 0;
private static final int DEFAULT_SIZE = 16;
private Object[] elementData = null;
private int index;

public ArrayList() {
elementData = new Object[DEFAULT_SIZE];
}

public ArrayList(final int size) {
elementData = new Object[size];
}

public void add(Object o) {
//如果当前元素个数大于数组长度的2/3
if (size() > elementData.length * 2 / 3) {
raiseArray();
}

elementData[index++] = o;
size++;
}

public void add(int index, Object o) {
checkParam(index);

//如果当前元素个数大于数组长度的2/3
if (size() > elementData.length * 2 / 3) {
raiseArray();
}

elementData[index] = o;
size++;
}

public Object get(int index) {
checkParam(index);

return elementData[index];
}

public Object remove(int index) {
checkParam(index);

Object o = elementData[index];
elementData[index] = null;
size--;
return o;
}

private void raiseArray() {
Object[] newElementData = Arrays.copyOf(elementData, size() * 2);
elementData = newElementData;
}

private void checkParam(int index) {
if (index < 0 || index > elementData.length - 1) {
throw new IndexOutOfBoundsException();
}
}

public int size() {
return size;
}

public Iterator iterator() {
return new ListIterator();
}

private class ListIterator implements Iterator{
int cursor;

@Override
public boolean hasNext() {
return cursor != size;
}

public void remove(){
throw new UnsupportedOperationException();
}

@Override
public Object next() {
if(!hasNext()) {
throw new NoSuchElementException();
}

int i = cursor;
if (i >= size)
throw new NoSuchElementException();

Object[] elementData = ArrayList.this.elementData;

cursor = i + 1;

return elementData[i];
}
}
}
74 changes: 74 additions & 0 deletions group08/769638826/2-27/ArrayListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.coding.basic;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
* Created by huitailang on 17/2/25.
* test arraylist
*/
public class ArrayListTest {
ArrayList arrayList = null;

@Before
public void setUp() {
arrayList = new ArrayList();
}

@Test
public void testArrayLength() {
int[] array = new int[10];
Assert.assertEquals(10, array.length);
}

@Test
public void testAddElement() {
arrayList.add(11);
Assert.assertEquals(11, arrayList.get(0));
printElementSize(arrayList);

for (int i = 0; i < 18; i++) {

}
}

@Test
public void testAriseArray() {
for (int i = 0; i < 18; i++) {
arrayList.add(i + 1);
}

Assert.assertEquals(18, arrayList.size());

for (int i = 0; i < 18; i++) {
System.out.println(arrayList.get(i));
}
}

@Test
public void testRemoveElement() {
for (int i = 0; i < 18; i++) {
arrayList.add(i + 1);
}

Assert.assertEquals(18, arrayList.size());

arrayList.remove(17);

Assert.assertEquals(17, arrayList.size());

for (int i = 0; i < 18; i++) {
System.out.println(arrayList.get(i));
}
}

@Test(expected = IndexOutOfBoundsException.class)
public void testInValidGet() {
arrayList.get(19);
}

private void printElementSize(ArrayList arrayList) {
System.out.println("array size => " + arrayList.size());
}
}
13 changes: 13 additions & 0 deletions group08/769638826/2-27/Iterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.coding.basic;

/**
* Created by huitailang on 17/2/25.
*
* @author zhangkun
* @date 2017年02月25日16:25:42
*/
public interface Iterator {
public boolean hasNext();

public Object next();
}
190 changes: 190 additions & 0 deletions group08/769638826/2-27/LinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package com.coding.basic;

import java.util.NoSuchElementException;

/**
* Created by huitailang on 17/2/25.
*
* @author zhangkun
* @date 2017年02月25日13:57:58
*/
public class LinkedList implements List {
private Node head;
private int size;

public LinkedList() {
head = null;
size = 0;
}

@Override
public void add(Object o) {
if (head == null) {
Node newNode = new Node();
newNode.data = o;
head = newNode;
}

Node oldhead = head;
head = new Node();
head.data = o;
head.next = oldhead;
size++;
}

@Override
public void add(int index, Object o) {
Node newNode = new Node();
newNode.data = o;

if (head == null) {
head = newNode;
}

if (index < 1 || index > size + 1) {
throw new IllegalArgumentException("invalid index, it's should be 1 and" + size + 1);
}

if (index == 1) {
newNode.next = head;
} else {
Node currentNode = head;
int count = 1;
while (count < index - 1) {
count++;
currentNode = currentNode.next;
}
newNode.next = currentNode.next;
currentNode.next = newNode;
}
}

@Override
public Object get(int index) {
if (head == null) {
return null;
}

if (index == 1) {
return head.next.data;
} else {
Node currentNode = head;
int count = 1;
while (count < index - 1) {
count++;
currentNode = currentNode.next;
}

return currentNode.next.data;
}
}

@Override
public Object remove(int index) {
Object result = null;

if (index < 1 || index > size) {
throw new IllegalArgumentException("invalid index, it's should be 1 and " + size);
}

if (index == 1) {
Node currentNode = head.next;
head = null;
return currentNode;
} else {
Node prevNode = head;

int count = 1;
while (count < index - 1) {
prevNode = prevNode.next;
count++;
}
Node currentNode = prevNode.next;
prevNode.next = currentNode.next;
result = currentNode.data;
currentNode = null;
}

return result;
}

@Override
public int size() {
return size;
}

public void addFirst(Object o) {
add(1, o);
}

public void addLast(Object o) {
add(size + 1, o);
}

public Object removeFirst() {
return remove(1);
}

public Object removeLast() {
return remove(size);
}

public Iterator iterator() {
return new ListIterator();
}

public void print() {
if (head == null) {
System.out.println("No elements in the list!");
}

Node currentNode = head;
while (currentNode != null) {
System.out.println(currentNode.data + "->");
currentNode = currentNode.next;
}

System.out.println();
}

public int length() {
int count = 0;

Node currentNode = head;
while (currentNode != null) {
count++;
currentNode = currentNode.next;
}

return count;
}

private static class Node {
Object data;
Node next;
}

private class ListIterator implements Iterator {
private Node current = head;

@Override
public boolean hasNext() {
return current != null;
}

public void remove() {
throw new UnsupportedOperationException();
}

@Override
public Object next() {
if (!hasNext()) {
throw new NoSuchElementException();
}

Object o = current.data;
current = current.next;
return o;
}
}
}
Loading

0 comments on commit be3ef60

Please sign in to comment.