数组模拟栈
public static class ArrayStack {
private Integer[] arr;
private Integer size;
public ArrayStack(int initSize) {
if (initSize < 0) {
throw new IllegalArgumentException("The init size is less than 0");
}
arr = new Integer[initSize];
size = 0;
}
public Integer peek() {
if (size == 0) {
return null;
}
return arr[size - 1];
}
public void push(int num) {
if (size == arr.length) {
throw new ArrayIndexOutOfBoundsException("The queue is full");
}
arr[size++] = num;
}
public Integer pop() {
if (size == 0) {
throw new ArrayIndexOutOfBoundsException("The queue is empty");
}
return arr[--size];
}
}
数组模拟栈(自动扩容)
public class MyStack {
public static void main(String[] args) {
MyStack stack = new MyStack(3);
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
System.out.println(stack.size());
}
private int[] storage;
private int capacity;
private int count;
private static final int GROW_FACTOR = 2;
public MyStack() {
this.capacity = 8;
this.storage=new int[8];
this.count = 0;
}
public MyStack(int initialCapacity) {
if (initialCapacity < 1)
throw new IllegalArgumentException("Capacity too small.");
this.capacity = initialCapacity;
this.storage = new int[initialCapacity];
this.count = 0;
}
public void push(int value) {
if (count == capacity) {
ensureCapacity();
}
storage[count++] = value;
}
private void ensureCapacity() {
int newCapacity = capacity * GROW_FACTOR;
storage = Arrays.copyOf(storage, newCapacity);
capacity = newCapacity;
}
private int pop() {
count--;
if (count == -1)
throw new IllegalArgumentException("Stack is empty.");
return storage[count];
}
private int peek() {
if (count == 0){
throw new IllegalArgumentException("Stack is empty.");
}else {
return storage[count-1];
}
}
private boolean isEmpty() {
return count == 0;
}
private int size() {
return count;
}
}
返回栈中的最小元素
public static class MyStack1 {
private Stack<Integer> stackData;
private Stack<Integer> stackMin;
public MyStack1() {
this.stackData = new Stack<>();
this.stackMin = new Stack<>();
}
public void push(int newNum) {
if (this.stackMin.isEmpty()) {
this.stackMin.push(newNum);
} else if (newNum < this.getMin()) {
this.stackMin.push(newNum);
} else {
int newMin = this.stackMin.peek();
this.stackMin.push(newMin);
}
this.stackData.push(newNum);
}
public int pop() {
if (this.stackData.isEmpty()) {
throw new RuntimeException("Your stack is empty.");
}
this.stackMin.pop();
return this.stackData.pop();
}
public int getMin() {
if (this.stackMin.isEmpty()) {
throw new RuntimeException("Your stack is empty.");
}
return this.stackMin.peek();
}
}
栈实现一个队列
public static class TwoStacksQueue {
private Stack<Integer> stackPush;
private Stack<Integer> stackPop;
public TwoStacksQueue() {
stackPush = new Stack<>();
stackPop = new Stack<>();
}
public void push(int pushInt) {
stackPush.push(pushInt);
}
public int poll() {
if (stackPop.empty() && stackPush.empty()) {
throw new RuntimeException("Queue is empty!");
} else if (stackPop.empty()) {
while (!stackPush.empty()) {
stackPop.push(stackPush.pop());
}
}
return stackPop.pop();
}
public int peek() {
if (stackPop.empty() && stackPush.empty()) {
throw new RuntimeException("Queue is empty!");
} else if (stackPop.empty()) {
while (!stackPush.empty()) {
stackPop.push(stackPush.pop());
}
}
return stackPop.peek();
}
}
队列实现栈
public static class TwoQueuesStack {
private Queue<Integer> dataQueue;
private Queue<Integer> helpQueue;
public TwoQueuesStack() {
dataQueue = new LinkedList<>();
helpQueue = new LinkedList<>();
}
public void push(int pushInt) {
dataQueue.add(pushInt);
}
public int peek() {
if (dataQueue.isEmpty()) {
throw new RuntimeException("Stack is empty!");
}
while (dataQueue.size() != 1) {
helpQueue.add(dataQueue.poll());
}
int res = dataQueue.poll();
helpQueue.add(res);
swap();
return res;
}
public int pop() {
if (dataQueue.isEmpty()) {
throw new RuntimeException("Stack is empty!");
}
while (dataQueue.size() > 1) {
helpQueue.add(dataQueue.poll());
}
int res = dataQueue.poll();
swap();
return res;
}
private void swap() {
Queue<Integer> tmp = helpQueue;
helpQueue = dataQueue;
dataQueue = tmp;
}
}