1.用数组实现栈
栈:先进后出
//用数组实现栈
//1.压栈
//2.出栈
//3.判断栈是否为空
public class Stack {
//首先需要一个数组以及栈的空间大小
private int[] arr;
private int size;
private int cur;
public Stack(int size) {
arr = new int[size];
this.size = size;
cur = 0;
}
//压栈
public void push(int num) {
if(cur == size) {
System.out.println("栈已满,无法入栈");
}else {
arr[cur++] = num;
}
}
//出栈
public int pop() throws Exception {
if(isEmpty()) {
throw new Exception("栈为空,无法出栈元素");
}else {
return arr[--cur];
}
}
//获取当前栈顶的元素,但不弹出
public int peek() {
return arr[--cur];
}
//判断栈是否为空,需要确定一个表示当前栈的指针
public boolean isEmpty() {
return cur == 0;
}
//判断栈是否已满
public boolean inFull() {
return cur == size;
}
//获取当前栈的大小
public int getSize() {
return size;
}
2.用数组实现队列(该队列为循环队列)
队列:先进先出
public class Queue {
private int[] arr;
private int size;
private int front;
private int tail;
public Queue(int size) {
arr = new int[size];
front = 0;
tail = 0;
size = 0;
}
//入队
public void push(int obj) {
if (size == arr.length) {
throw new ArrayIndexOutOfBoundsException("队列已满");
}else {
size++;
arr[tail] = obj;
tail = tail == arr.length-1? 0: tail+1;
}
}
//出队
public int poll() {
if (size == 0) {
throw new ArrayIndexOutOfBoundsException("队列为空");
}
size--;
int tmp = front;
front = front == arr.length? 0: front+1;
return arr[tmp];
}
//获取第一个元素
public int peek() {
if (size == 0) {
throw new ArrayIndexOutOfBoundsException("队列为空");
}
return arr[front];
}
//判断队列是否为空:
public boolean isEmpy() {
return size == 0;
}
}
3.用两个栈实现一个队列
import java.util.Stack;
public class TwoStackQueue {
private Stack<Integer> stackPush;
private Stack<Integer> stackPop;
public TwoStackQueue() {
stackPush = new Stack<Integer>();
stackPop = new Stack<Integer>();
}
//压队:直接将元素压入到push栈中
public void add(int obj) {
stackPush.push(obj);
}
//出队:首先判断push栈的元素是否为空或者元素的个数是否为1,若是抛异常或者直接弹出
//否则,就将push栈的元素弹出压入到pop栈中,并留取一个元素,直接弹出,然后将pop栈中的数据返回到push中
public int poll() {
if (stackPush.isEmpty()) {
throw new RuntimeException("栈为空");
}
if (stackPush.size() != 1) {
while (stackPush.size() != 1) {
stackPop.push(stackPush.pop());
}
int tmp = stackPush.pop();
dao();
return tmp;
}
return stackPush.pop();
}
//将StackPop的数据全都倒入StackPush中
public void dao() {
if(stackPop.isEmpty()) {
throw new RuntimeException("栈的元素为空,无法进行该操作");
}else {
while (!stackPop.isEmpty()) {
stackPush.push(stackPop.pop());
}
}
}
4.用两个队列实现栈
import java.util.LinkedList;
import java.util.Queue;
public class TwoQueueStack {
private Queue<Integer> data;
private Queue<Integer> help;
public TwoQueueStack() {
data = new LinkedList<Integer>();
help = new LinkedList<Integer>();
}
//入栈
public void push(int obj) {
data.add(obj);
}
//出栈
public int pop() {
if (data.isEmpty()) {
throw new RuntimeException("栈为空");
}
while (data.size() > 1) {
help.add(data.poll());
}
int res = data.poll();
swap();
return res;
}
//获取第一个栈顶的元素
public int peek() {
if (data.isEmpty()) {
throw new RuntimeException("栈为空");
}
while (data.size() > 1) {
help.add(data.poll());
}
int res = data.poll();
help.add(res);
swap();
return res;
}
//交换引用
public void swap() {
Queue<Integer> tmp = help;
help = data;
data = tmp;
}
}