Stack
Stack 后进先出(LIFO)
是在表的一端进行插入或删除运算的线性表,我们把插入、删除的这一端称为栈顶(Top),另一端称为栈底(Bottom)。
方法说明
E push(E item) 把元素压入栈顶并返回此元素
E pop() 移除栈顶元素并返回此元素
E peek() 查看栈顶元素而不移除它
boolean empty() 测试栈是否为空
int search(Object o) 返回元素在栈中的位置,-1代表未找到该元素
两个栈实现一个队列思路
stack1负责入栈,stack2负责出栈
入队列:直接把stack1压入
出队列:如果stack2不为空,则把stack2栈顶元素直接弹出,否则, 把stack1中所有元素全部压入到stack2中,再弹出stack2栈顶元素
注意:对stack1、stack1栈中元素做空处理
示例代码
public class TwoStackImpQueue {
private Stack<Integer> stack1;
private Stack<Integer> stack2;
public TwoStackImpQueue() {
stack1 = new Stack();
stack2 = new Stack();
}
public boolean offer(int value) {
//不需要考虑栈是否存满
stack1.push(value);
return true;
}
public Integer poll() {
Integer value = null;
if (!stack2.empty()) {
value = stack2.pop();
} else {
while (!stack1.empty()) {
value = stack1.pop();
stack2.push(value);
}
if (!stack2.empty()) {
value = stack2.pop();
}
}
return value;
}
public static void main(String[] args) {
TwoStackImpQueue stacks = new TwoStackImpQueue();
stacks.offer(1);
stacks.offer(2);
stacks.offer(3);
Integer poll = stacks.poll();
System.out.println(poll);
stacks.offer(4);
Integer poll1 = stacks.poll();
System.out.println(poll1);
stacks.offer(5);
Integer poll2 = stacks.poll();
System.out.println(poll2);
Integer poll3 = stacks.poll();
Integer poll4 = stacks.poll();
Integer poll5 = stacks.poll();
Integer poll6 = stacks.poll();
System.out.println(poll3);
System.out.println(poll4);
System.out.println(poll5);
System.out.println(poll6);
}
}
打印结果
1
2
3
4
5
null
null
Queue
Queue 先进先出(FIFO)
队列是一种特殊的线性表,它只允许在队列头部进行删除操作,而在队列尾部进行插入操作。LinkedList类实现了Queue接口,因此可以把LinkedList当成Queue来用。
方法说明
boolean add(E var1) 增加一个元索 如果队列已满,则抛出一个IllegalStateException异常
E remove() 移除并返回队列头部的元素 如果队列为空,则抛出一个NoSuchElementException异常
E element() 返回队列头部的元素 如果队列为空,则抛出一个NoSuchElementException异常
boolean offer(E var1) 添加一个元素并返回true 如果队列已满,则返回false
E poll() 移除并返问队列头部的元素 如果队列为空,则返回null
E peek() 返回队列头部的元素 如果队列为空,则返回null
两个队列实现一个栈思路
元素集中存放在一个队列中,但不指定(queue1或queue2,此处默认是queue1)
入栈:哪个队列不为空就压入到那个队列
出栈:把不为空的队列,除最后一个元素,全部转移到另一个队列,并把最后这个元素弹出
注意:对queue1、queue2队列中元素做空处理
示例代码
public class TwoQueueImpStack {
private Queue<Integer> queue1;
private Queue<Integer> queue2;
public TwoQueueImpStack() {
queue1 = new LinkedList<>();
queue2 = new LinkedList<>();
}
public Integer push(Integer value) {
if (!queue1.isEmpty()) {
queue1.offer(value);
} else if (!queue2.isEmpty()) {
queue2.offer(value);
}
if (queue1.isEmpty() && queue2.isEmpty()) {
queue1.offer(value);
}
return value;
}
public Integer pop() {
Integer value = null;
if (!queue1.isEmpty()) {
while (!queue1.isEmpty()) {
value = queue1.poll();
if (!queue1.isEmpty()) {
queue2.offer(value);
}
}
} else if (!queue2.isEmpty()) {
while (!queue2.isEmpty()) {
value = queue2.poll();
if (!queue2.isEmpty()) {
queue1.offer(value);
}
}
}
return value;
}
public static void main(String[] args) {
TwoQueueImpStack stack = new TwoQueueImpStack();
stack.push(1);
stack.push(2);
stack.push(3);
Integer pop = stack.pop();
System.out.println(pop);
stack.push(4);
Integer pop1 = stack.pop();
System.out.println(pop1);
stack.push(5);
Integer pop2 = stack.pop();
System.out.println(pop2);
Integer pop3 = stack.pop();
Integer pop4 = stack.pop();
Integer pop5 = stack.pop();
Integer pop6 = stack.pop();
System.out.println(pop3);
System.out.println(pop4);
System.out.println(pop5);
System.out.println(pop6);
}
}
打印结果
3
4
5
2
1
null
null