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 @@ -14,7 +14,7 @@ public CircleQueue(int capacity){
elementData = new Object[capacity];
}
public boolean isEmpty() {
return front == rear;
return (front == rear) && !isFull();

}

Expand All @@ -29,6 +29,7 @@ public void enQueue(E data) {
if(isFull()){
throw new RuntimeException("The queue is full");
}
rear = (rear+1) % elementData.length;
elementData[rear++] = data;
size++;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.coding.basic.stack;

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

private Stack<Integer> normalStack = new Stack<Integer>();
private Stack<Integer> minNumStack = new Stack<Integer>();

public void push(int data){

normalStack.push(data);

if(minNumStack.isEmpty()){
minNumStack.push(data);
} else{
if(minNumStack.peek() >= data) {
minNumStack.push(data);
}
}

}
public int pop(){
if(normalStack.isEmpty()){
throw new RuntimeException("the stack is empty");
}
int value = normalStack.pop();
if(value == minNumStack.peek()){
minNumStack.pop();
}
return value;
}
public int findMin(){
return minNumStack.peek();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.coding.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());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.coding.basic.stack;

import java.util.ArrayDeque;
import java.util.Queue;

public class StackWithTwoQueues {
Queue<Integer> queue1 = new ArrayDeque<>();
Queue<Integer> queue2 = new ArrayDeque<>();

public void push(int data) {
//两个栈都为空时,优先考虑queue1
if (queue1.isEmpty()&&queue2.isEmpty()) {
queue1.add(data);
return;
}

if (queue1.isEmpty()) {
queue2.add(data);
return;
}

if (queue2.isEmpty()) {
queue1.add(data);
return;
}

}

public int pop() {

if (queue1.isEmpty()&&queue2.isEmpty()) {
throw new RuntimeException("stack is empty");
}

if (queue1.isEmpty()) {
while (queue2.size()>1) {
queue1.add(queue2.poll());
}
return queue2.poll();
}

if (queue2.isEmpty()) {
while (queue1.size()>1) {
queue2.add(queue1.poll());
}
return queue1.poll();
}

throw new RuntimeException("no queue is empty, this is not allowed");


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.coding.basic.stack;

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



public class StackWithTwoQueuesTest {

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void test() {
StackWithTwoQueues stack = new StackWithTwoQueues();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
Assert.assertEquals(4, stack.pop());
Assert.assertEquals(3, stack.pop());

stack.push(5);
Assert.assertEquals(5, stack.pop());
Assert.assertEquals(2, stack.pop());
Assert.assertEquals(1, stack.pop());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.coding.basic.stack;

import java.util.Arrays;

/**
* 用一个数组实现两个栈
* 将数组的起始位置看作是第一个栈的栈底,将数组的尾部看作第二个栈的栈底,压栈时,栈顶指针分别向中间移动,直到两栈顶指针相遇,则扩容。
* @author liuxin
*
*/
public class TwoStackInOneArray {
private Object[] data = new Object[10];
private int size;
private int top1, top2;

public TwoStackInOneArray(int n){
data = new Object[n];
size = n;
top1 = -1;
top2 = data.length;
}
/**
* 向第一个栈中压入元素
* @param o
*/
public void push1(Object o){
ensureCapacity();
data[++top1] = o;
}
/*
* 向第二个栈压入元素
*/
public void push2(Object o){
ensureCapacity();
data[--top2] = o;
}
public void ensureCapacity(){
if(top2-top1>1){
return;
} else{

Object[] newArray = new Object[data.length*2];
System.arraycopy(data, 0, newArray, 0, top1+1);

int stack2Size = data.length-top2;
int newTop2 = newArray.length-stack2Size;
System.arraycopy(data, top2, newArray, newTop2, stack2Size);

top2 = newTop2;
data = newArray;
}
}
/**
* 从第一个栈中弹出元素
* @return
*/
public Object pop1(){
if(top1 == -1){
throw new RuntimeException("Stack1 is empty");
}
Object o = data[top1];
data[top1] = null;
top1--;
return o;

}
/**
* 从第二个栈弹出元素
* @return
*/
public Object pop2(){
if(top2 == data.length){
throw new RuntimeException("Stack2 is empty");
}
Object o = data[top2];
data[top2] = null;
top2++;
return o;
}
/**
* 获取第一个栈的栈顶元素
* @return
*/

public Object peek1(){
if(top1 == -1){
throw new RuntimeException("Stack1 is empty");
}
return data[top1];
}


/**
* 获取第二个栈的栈顶元素
* @return
*/

public Object peek2(){
if(top2 == data.length){
throw new RuntimeException("Stack2 is empty");
}
return data[top2];
}

public Object[] stack1ToArray(){
return Arrays.copyOf(data, top1+1);
}
public Object[] stack2ToArray(){
int size = data.length-top2;
Object [] stack2Data = new Object[size];
int j=0;
for(int i=data.length-1; i>=top2 ;i--){
stack2Data[j++] = data[i];
}
return stack2Data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.coding.basic.stack;

import java.util.Arrays;

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



public class TwoStackInOneArrayTest {

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void test1() {
TwoStackInOneArray stack = new TwoStackInOneArray(10);
stack.push1(1);
stack.push1(2);
stack.push1(3);
stack.push1(4);
stack.push1(5);

stack.push2(1);
stack.push2(2);
stack.push2(3);
stack.push2(4);
stack.push2(5);

for(int i=1;i<=5;i++){
Assert.assertEquals(stack.peek1(), stack.peek2());
Assert.assertEquals(stack.pop1(), stack.pop2());
}


}
@Test
public void test2() {
TwoStackInOneArray stack = new TwoStackInOneArray(5);
stack.push1(1);
stack.push1(2);
stack.push1(3);
stack.push1(4);
stack.push1(5);
stack.push1(6);
stack.push1(7);

stack.push2(1);
stack.push2(2);
stack.push2(3);
stack.push2(4);


Assert.assertEquals("[1, 2, 3, 4, 5, 6, 7]",Arrays.toString(stack.stack1ToArray()));
Assert.assertEquals("[1, 2, 3, 4]",Arrays.toString(stack.stack2ToArray()));
}

}
Loading