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
61 changes: 61 additions & 0 deletions group24/798277403/src/basic/queue/CircleQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package basic.queue;

/**
* 用数组实现循环队列
* @author liuxin
* @param <E>
*/
public class CircleQueue <E> {

private final static int DEFAULT_SIZE = 10;

private int size = 0;

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

//队头
private int front = 0;
//队尾
private int rear = 0;

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

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

public boolean isEmpty() {
return size==0;
}

public int size() {
return size;
}

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

public void enQueue(E data) {
if(isFull()){
throw new RuntimeException("The queue is full");
}
elementData[rear] = data;
rear = (rear+1)%elementData.length;
size++;
}

public E deQueue() {
if(isEmpty()){
throw new RuntimeException("The queue is empty");
}
E data = (E)elementData[front];
elementData[front] = null;
front = (front+1) % elementData.length;
size--;
return data;
}
}
45 changes: 45 additions & 0 deletions group24/798277403/src/basic/queue/CircleQueueTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package 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");
queue.enQueue("d");
queue.enQueue("e");

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

Assert.assertEquals("a", queue.deQueue());
queue.enQueue("f");
Assert.assertEquals("b", queue.deQueue());
Assert.assertEquals("c", queue.deQueue());
Assert.assertEquals("d", queue.deQueue());
Assert.assertEquals("e", queue.deQueue());

}

}
35 changes: 35 additions & 0 deletions group24/798277403/src/basic/queue/Josephus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package basic.queue;

import java.util.ArrayList;
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){
Queue<Integer> queue = new Queue<Integer>();
List<Integer> result = new ArrayList<Integer>();

for(int i=0; i<n; i++){
queue.enQueue(i);
}
int temp = 0;
while(!queue.isEmpty()){
temp++;
int current = queue.deQueue();
if(temp%m==0){
result.add(current);
}else {
queue.enQueue(current);
}
}
return result;
}

}
27 changes: 27 additions & 0 deletions group24/798277403/src/basic/queue/JosephusTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package 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());

}

}
61 changes: 61 additions & 0 deletions group24/798277403/src/basic/queue/Queue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package 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;
}

}
49 changes: 49 additions & 0 deletions group24/798277403/src/basic/queue/QueueWithTwoStacks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package 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() && stack2.isEmpty();
}

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

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

public E deQueue() {
if(isEmpty()){
throw new RuntimeException("The queue is empty");
}
E date = null;
if(!stack2.isEmpty()){
date = stack2.pop();
}else{
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
date = stack2.pop();
}
return date;
}

}

36 changes: 36 additions & 0 deletions group24/798277403/src/basic/stack/QuickMinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package basic.stack;

import java.util.Stack;

/**
* 设计一个栈,支持栈的push和pop操作,以及第三种操作findMin, 它返回改数据结构中的最小元素
* finMin操作最坏的情形下时间复杂度应该是O(1) , 简单来讲,操作一次就可以得到最小值
* @author liuxin
*
*/
public class QuickMinStack {
Stack<Integer> stackNum = new Stack<>();
Stack<Integer> stackMin = new Stack<>();
public void push(int data){
stackNum.push(data);
if(stackMin.isEmpty() || stackMin.peek()>=data){
stackMin.push(data);
}
}

public int pop(){
if(stackNum.size()<=0){
throw new RuntimeException("The stack is empty!");
}
int result = stackNum.pop();
if(result==stackMin.peek()){
stackMin.pop();
}
return result;
}

public int findMin(){
return stackMin.peek();
}

}
39 changes: 39 additions & 0 deletions group24/798277403/src/basic/stack/QuickMinStackTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package basic.stack;

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


public class QuickMinStackTest {

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void test() {
QuickMinStack stack = new QuickMinStack();
stack.push(5);
Assert.assertEquals(5, stack.findMin());
stack.push(6);
Assert.assertEquals(5, stack.findMin());
stack.push(4);
Assert.assertEquals(4, stack.findMin());
stack.push(4);
Assert.assertEquals(4, stack.findMin());

stack.pop();
Assert.assertEquals(4, stack.findMin());
stack.pop();
Assert.assertEquals(5, stack.findMin());
stack.pop();
Assert.assertEquals(5, stack.findMin());
}

}
Loading