栈结构的基本操作
pop()
top()或peek()
push()
size()
队列的基本操作
push()头部
pop()尾部
O(1)
双端队列 首尾都可以添加弹出元素
深度优先队列:栈
宽度优先队列:队列
1、编写一个栈,只能用两个栈结构实现队列,支持队列的基本操作
stackpush和stackpop
如果stackpush要往stackpop中倒入数据,那么必须要把stackpush中所有数据一次性倒完
如果stackpop中有数据,则不能发生倒入数据的行为
2、实现一个栈的逆序
移除栈顶元素
public int get(Stack<Integer> stack)
{
int result=stack.pop();
if(stack.isEmpty()
{
return result;
}
else
{
int last=get(stack);
stack.push(result);
return last;
}
}
把栈中元素逆序的主方法
public void reverse(Stack<Integer> stack)
{
int i=get(stack);
reverse(stack);
stack.push(i);
}
4、栈排序
排stack
辅助栈help
stack pop()若小于等于help中元素,则直接压入栈
stack pop()若大于help中元素,则将help中元素弹出依次压入stack中,直到current元素小于等于help栈顶元素,将current压入到help中
重复
直到stack中元素都压入help中
5、实现一个特殊的栈,在实现栈的基本功能基础上,再实现返回栈中的最小元素getMin
stackData
stackMin
只有当前数小于等于stackMin栈顶时,该数放入stackMin。