返回栈中最小元素
package abc;
import java.util.Stack;
public class P01GetMinStack {
public static class MinStack1 {
private Stack<Integer> stackData;
private Stack<Integer> stackMin;
public MinStack1() {
this.stackData = new Stack<Integer>();
this.stackMin = new Stack<Integer>();
}
public int pop(){
if (this.stackData.isEmpty()) {
throw new RuntimeException("Your stack is empty.");
}
this.stackMin.pop();
return this.stackData.pop();
}
public void push(int i){
if (stackMin.empty()){
this.stackMin.push(i);
} else {
int tmp = stackMin.peek();
if (tmp < i){
this.stackMin.push(tmp);
} else {
this.stackMin.push(i);
}
}
this.stackData.push(i);
}
public int getMin(){
if (this.stackMin.isEmpty()) {
throw new RuntimeException("Your stack is empty.");
}
return this.stackMin.peek();
}
}
public static void main(String[] args){
MinStack1 ms = new MinStack1();
ms.push(3);
ms.push(2);
ms.push(4);
ms.push(1);
ms.push(5);
System.out.println(ms.getMin());
ms.pop();
System.out.println(ms.getMin());
ms.pop();
System.out.println(ms.getMin());
ms.pop();
System.out.println(ms.getMin());
ms.pop();
System.out.println(ms.getMin());
}
}
栈实现队列
package abc;
import java.util.Stack;
public class P02StackImplQueue {
public static class TwoStackQueue {
public Stack<Integer> stackPush;
public Stack<Integer> stackPop;
public TwoStackQueue() {
stackPush = new Stack<Integer>();
stackPop = new Stack<Integer>();
}
public void add(int i) {
stackPush.push(i);
}
public int poll() {
if (stackPush.empty() && stackPop.empty()){
throw new RuntimeException("Queue is empty!");
} else if (stackPop.empty()){
while (! stackPush.empty()){
int tmp = stackPush.pop();
stackPop.push(tmp);
}
}
return stackPop.pop();
}
public int peek() {
if (stackPush.empty() && stackPop.empty()){
throw new RuntimeException("Queue is empty!");
} else if (stackPop.empty()){
while (! stackPush.empty()){
int tmp = stackPush.pop();
stackPop.push(tmp);
}
}
return stackPop.peek();
}
}
public static void main(String[] args) {
TwoStackQueue sq = new TwoStackQueue();
sq.add(1);
sq.add(2);
sq.add(3);
System.out.println(sq.peek());
System.out.println(sq.poll());
System.out.println(sq.peek());
System.out.println(sq.poll());
System.out.println(sq.peek());
System.out.println(sq.poll());
}
}
实现栈的逆序,仅用递归
package abc;
import java.util.Stack;
public class P03ReverseStack {
public static void main(String[] args){
Stack<Integer> test = new Stack<Integer>();
test.push(1);
test.push(2);
test.push(3);
test.push(4);
test.push(5);
reverse(test);
while (!test.isEmpty()) {
System.out.println(test.pop());
}
}
private static void reverse(Stack<Integer> stack) {
if (stack.empty()){
return;
}
int tmp = getLast(stack);
reverse(stack);
stack.push(tmp);
}
private static int getLast(Stack<Integer> stack) {
int result = stack.pop();
if (stack.empty()){
return result;
} else {
int last = getLast(stack);
stack.push(result);
return last;
}
}
}

栈排序
package abc;
import java.util.Stack;
public class P05SortStack {
public static void main(String[] args){
Stack<Integer> stack = new Stack<Integer>();
stack.push(3);
stack.push(1);
stack.push(6);
stack.push(2);
stack.push(5);
stack.push(4);
sortStack(stack);
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
}
private static void sortStack(Stack<Integer> stack) {
if (stack.empty()){
return;
}
Stack<Integer> help = new Stack<Integer>();
while (!stack.isEmpty()) {
int cur = stack.pop();
while (!help.isEmpty() && help.peek() < cur) {
stack.push(help.pop());
}
help.push(cur);
}
while (!help.isEmpty()) {
stack.push(help.pop());
}
}
}