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
Expand Up @@ -318,7 +318,7 @@ public LinkedList intersection(LinkedList list){

LinkedList cList = new LinkedList();

// TODO:为了方便起见,先各自复制并去重,虽然效率不高,以后有时间再优化
// 为了方便起见,先各自复制并去重,虽然效率不高,以后有时间再优化
LinkedList aList = LinkedList.copy(this);
LinkedList bList = LinkedList.copy(list);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.github.miniyk2012.coding2017.basic.queue;

import java.util.ArrayDeque;

/**
* 用数组实现循环队列,这也是一个队列,和普通队列有相同的外部特性,只是内部使用循环数组实现的罢了
* @author liuxin
*
* @param <E>
*/
public class CircleQueue <E> {

private final static int DEFAULT_SIZE = 10;

//用数组来保存循环队列的元素
private Object[] elementData = new Object[DEFAULT_SIZE] ;

//队头
protected int head = 0;
//队尾
protected int rear = 0; // rear指向的位置不放值,也就是说即使满了,也有一个空位

public CircleQueue(int capacity) {
elementData = new Object[capacity+1]; // 实际的数组长度要比容量大1
}

public CircleQueue() {
}

public boolean isEmpty() {
return head == rear;
}

public int size() {

return Math.floorMod(rear-head, elementData.length);
}

/**
* 如果满了还往里面放,则抛IndexOutOfBoundsException异常
*/
public void enQueue(E data) {
if (isFull()) throw new IndexOutOfBoundsException("循环队列已满");
elementData[rear] = data;
rear = Math.floorMod(rear+1, elementData.length);
}

/**
* 如果为空,则抛IndexOutOfBoundsException异常
* @return
*/
public E deQueue() {
if (isEmpty()) throw new IndexOutOfBoundsException("循环队列为空");
E result = (E) elementData[head];
head = Math.floorMod(head+1, elementData.length);
return result;
}

public boolean isFull() {
return size() == elementData.length-1;
}

public static void main(String[] args) {
/**
Math.floorMod( 2, 3) = 2
Math.floorMod(-2, 3) = 1
Math.floorMod( 2, -3) = -1
Math.floorMod(-2, -3) = -2
*/
System.out.println(Math.floorMod(1-2, 5));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.github.miniyk2012.coding2017.basic.queue;

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



public class CircleQueueTest {

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void test() {
CircleQueue<String> queue = new CircleQueue<String>(5);
Assert.assertTrue(queue.isEmpty());
Assert.assertFalse(queue.isFull());

queue.enQueue("a");
queue.enQueue("b");
queue.enQueue("c");
// System.out.println(queue.head);
// System.out.println(queue.rear);
queue.enQueue("d");
queue.enQueue("e");

Assert.assertTrue(queue.isFull());
Assert.assertFalse(queue.isEmpty());
Assert.assertEquals(5, queue.size());

Assert.assertEquals("a", queue.deQueue());
Assert.assertEquals("b", queue.deQueue());
Assert.assertEquals("c", queue.deQueue());

Assert.assertFalse(queue.isFull());
Assert.assertEquals(2, queue.size());

queue.enQueue("f");
queue.enQueue("g");
// System.out.println(queue.head);
// System.out.println(queue.rear);
Assert.assertEquals("d", queue.deQueue());
Assert.assertEquals("e", queue.deQueue());
// System.out.println(queue.head);
// System.out.println(queue.rear);
Assert.assertEquals("f", queue.deQueue());
Assert.assertEquals("g", queue.deQueue());

Assert.assertEquals(0, queue.size());
Assert.assertTrue(queue.isEmpty());
Assert.assertFalse(queue.isFull());

// System.out.println(queue.head);
// System.out.println(queue.rear);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.github.miniyk2012.coding2017.basic.queue;

import java.util.LinkedList;
import java.util.List;

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

public static List<Integer> execute(int n, int m){
CircleQueue<Integer> circleQueue = new CircleQueue<>(n);
List<Integer> result = new LinkedList<>();
for (int i=0; i<n; i++) {
circleQueue.enQueue(i);
}
int num = 1; // 从1开始报数
while (!circleQueue.isEmpty()) {
Integer x = circleQueue.deQueue();
if (num == m) {
num = 1; // 杀死一个人,下一个人从1开始报数
result.add(x);
} else {
num++;
circleQueue.enQueue(x);
}
}
return result;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.github.miniyk2012.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, 6]", Josephus.execute(7, 2).toString());

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.github.miniyk2012.coding2017.basic.queue;

import java.util.NoSuchElementException;

public class Queue<E> {
private Node<E> first;
private Node<E> last;
private int size;


private static class Node<E> {
private E item;
private Node<E> next;
}


public Queue() {
first = null;
last = null;
size = 0;
}


public boolean isEmpty() {
return first == null;
}

public int size() {
return size;
}



public void enQueue(E data) {
Node<E> oldlast = last;
last = new Node<E>();
last.item = data;
last.next = null;
if (isEmpty()) {
first = last;
}
else{
oldlast.next = last;
}
size++;
}

public E deQueue() {
if (isEmpty()) {
throw new NoSuchElementException("Queue underflow");
}
E item = first.item;
first = first.next;
size--;
if (isEmpty()) {
last = null;
}
return item;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.github.miniyk2012.coding2017.basic.queue;

import java.util.Stack;

/**
* 用两个栈来实现一个队列
* @author liuxin
*
* @param <E>
*/
public class QueueWithTwoStacks<E> {
private Stack<E> stack1;
private Stack<E> stack2;


public QueueWithTwoStacks() {
stack1 = new Stack<E>();
stack2 = new Stack<E>();
}

public boolean isEmpty() {
return stack1.isEmpty();
}

private void move(Stack s1, Stack s2) {
while (!s1.empty()) {
s2.push(s1.pop());
}
}

public int size() {
return stack1.size();
}

public void enQueue(E item) {
stack1.push(item);
}

public E deQueue() {
move(stack1, stack2);
E result = stack2.pop();
move(stack2, stack1);
return result;
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.github.miniyk2012.coding2017.basic.queue;

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

/**
* QueueWithTwoStacks Tester.
*
* @author <Authors name>
* @since <pre>May 3, 2017</pre>
* @version 1.0
*/
public class QueueWithTwoStacksTest {
QueueWithTwoStacks<String> queue;

@Before
public void before() throws Exception {
queue = new QueueWithTwoStacks<>();
}

@After
public void after() throws Exception {
}

@Test
public void testFunction() {
queue.enQueue("a");
queue.enQueue("b");
queue.enQueue("c");
queue.enQueue("d");
Assert.assertEquals(4, queue.size());
Assert.assertFalse(queue.isEmpty());

Assert.assertEquals("a", queue.deQueue());
Assert.assertEquals("b", queue.deQueue());
Assert.assertEquals("c", queue.deQueue());
Assert.assertEquals(1, queue.size());
Assert.assertFalse(queue.isEmpty());

queue.enQueue("e");
Assert.assertEquals("d", queue.deQueue());
Assert.assertEquals("e", queue.deQueue());
Assert.assertTrue(queue.isEmpty());
Assert.assertEquals(0, queue.size());

}



}
Loading