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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.johnChnia.coderising2017.array;

import com.johnChnia.coding2017.basic.Queue;
import com.johnChnia.coding2017.basic.queue.Queue;
import com.johnChnia.coding2017.basic.ArrayList;


Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.johnChnia.coding2017.basic;

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

/**
* Created by john on 2017/3/8.
*
* @// TODO: 2017/4/1 实现Iterator 接口
* @// TODO: 学会多线程后,实现Iterator 的 remove 方法
*/

public class ArrayList<E> implements List<E> {
Expand Down Expand Up @@ -138,8 +140,32 @@ public int size() {
return size;
}

@Override
public Iterator<E> iterator() {
return new Itr();
}

/**

private class Itr implements Iterator<E> {
int cursor = 0;
@Override
public boolean hasNext() {
return cursor != size;
}

@Override
public E next() {
int i = cursor;
if (i >= size) {
throw new NoSuchElementException();
}
Object[] elementData = ArrayList.this.elementData;
cursor = i + 1;
return (E) elementData[i];
}
}

/**
* Increases the capacity to ensure that it can hold at least the
* number of elements specified by the double length of list.
*/
Expand All @@ -148,6 +174,25 @@ private void grow() {
2 * elementData.length);
}

public boolean contains(Object o) {
Itr itr = new Itr();
if (o == null) {
while (itr.hasNext()) {
if (itr.next() == null) {
return true;
}
}

} else {
while (itr.hasNext()) {
if (itr.next().equals(o)) {
return true;
}
}
}
return false;
}

public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.johnChnia.coding2017.basic;

import com.johnChnia.coding2017.basic.queue.Queue;

/**
* Created by john on 2017/3/13.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package com.johnChnia.coding2017.basic;

import java.util.Iterator;

/**
* Created by john on 2017/3/12.
*/
public interface List<E> {
public void add(E o);
public interface List<E> extends Iterable<E>{
void add(E o);

void add(int index, E o);

E get(int index);

E remove(int index);

public void add(int index, E o);
int size();

public E get(int index);
Iterator<E> iterator();

public E remove(int index);
boolean contains(Object o);

public int size();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import com.johnChnia.coding2017.basic.List;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
* Created by john on 2017/3/9.
*
* @// TODO: 2017/4/1 支持Iterator
* @// TODO: 学会多线程后,实现Iterator 的 remove 方法
*/

public class LinkedList<E> implements List<E> {
Expand Down Expand Up @@ -223,6 +224,40 @@ public int size() {
return size;
}

@Override
public Iterator<E> iterator() {
return new Itr();
}

@Override
public boolean contains(Object o) {
return false;
}

private class Itr implements Iterator<E> {
/**
* Index of element to be returned by subsequent call to next.
*/
int cursor = 0;


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

@Override
public E next() {
int i = cursor;
if (i >= size) {
throw new NoSuchElementException();
}
E next = get(i);
cursor = i + 1;
return next;
}
}

private void checkElementIndex(int index) {
if (!isElementIndex(index)) {
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.johnChnia.coding2017.basic.queue;

/**
* Created by john on 2017/5/6.
*
* NOTE:循环队列下一个为(i+1)%N,上一个为(i+N-1)%N
*/
public class CircleQueue {
private int rear = -1;
private int front = -1;
private Object[] array;
private static final int DEFAULTCAPACITY = 10;


/**
* 构造一个空的循环队列
*/
public CircleQueue() {
array = new Object[DEFAULTCAPACITY];
}

/**
* 根据传进来的容量构造循环队列
*
* @param capacity 队列容量
*/
public CircleQueue(int capacity) {
array = new Object[capacity];
}

/**
* 循环队列长度
*
* @return 队列长度
*/
public int size() {
return array.length;
}

/**
* 判断队列是否已经满了
*
* @return 如果队列满的话返回true,否则返回false
*/
public boolean isFull() {
if ((rear + 1) % size() == front) {
return true;
}
return false;
}

/**
* 判断队列是否为空
*
* @return 如果队列为空返回true,否则返回false
*/
public boolean isEmpty() {
if (rear == -1 && front == -1) {
return true;
}
return false;
}

/**
* 入队操作
*/
public void enQueue(int item) throws Exception {
if (isFull()) {
throw new Exception("队列已满");
} else if (isEmpty()) {
rear++;
front = rear;
array[rear] = item;
} else {
rear = (rear + 1) % size();
array[rear] = item;
}
}

/**
* 出队操作
*/
public void deQueue() throws Exception {
if (isEmpty()) {
throw new Exception("队列为空");
} else if (front == rear) {
front = -1;
rear = -1;
} else {
front = (front + 1) % size();
}
}

public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("(");
for (int i = 0; i <= size() - 1; i++) {
sb.append(array[i]);
sb.append(", ");
}
sb.append(")");
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.johnChnia.coding2017.basic.queue;


import com.johnChnia.coding2017.basic.ArrayList;
import com.johnChnia.coding2017.basic.List;

/**
* 用Queue来实现Josephus问题
* 在这个古老的问题当中, N个深陷绝境的人一致同意用这种方式减少生存人数: N个人围成一圈(位置记为0到N-1), 并且从第一个人报数, 报到M的人会被杀死, 直到最后一个人留下来
* 该方法返回一个List, 包含了被杀死人的次序
* 参考 http://interactivepython.org/courselib/static/pythonds/BasicDS/SimulationHotPotato.html
*
* @author john
*/
public class Josephus {

public static List<Integer> execute(int n, int m) {
ArrayList<Integer> arrayList = new ArrayList<>();
Queue<Integer> queue = new Queue<>();
for (int i = 0; i < n; i++) {
queue.add(i);
}
while (queue.size() > 1) {
for (int i = 0; i < m - 1; i++) {
queue.add(queue.remove());
}
arrayList.add(queue.remove());
}
return arrayList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.johnChnia.coding2017.basic.queue;

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



public class JosephusTest {

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void testExecute() {

Assert.assertEquals("[1, 3, 5, 0, 4, 2, ]", Josephus.execute(7, 2).toString());

}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.johnChnia.coding2017.basic;
package com.johnChnia.coding2017.basic.queue;

import com.johnChnia.coding2017.basic.ArrayList;

import java.util.NoSuchElementException;

Expand Down
Loading