栈
定义
栈是一种基于后进先出(LIFO)策略的集合类型。
API

链表实现

pop

push

代码
1 | public class LinkedStackOfStrings { |
数组实现
Array implementation of a stack.
- Use array
s[]to store N items on stack. push(): add new item ats[N].pop(): remove item froms[N-1].

Defect. Stack overflows when N exceeds capacity.
调整数组大小
push(): double size of arrays[]when array is full.pop(): halve size of arrays[]when array is one-quarter full.
代码
1 | public class ResizingArrayStackOfStrings { |
队列
定义
队列是一种基于先进先出(FIFO)策略的集合类型。
链表实现
enqueue

dequeue

代码
1 | public class LinkedQueueOfStrings { |
数组实现
API

背包
定义
背包是一种不支持从中删除元素的集合数据类型——它的目的就是帮助用例收集元素并迭代遍历(顺序无关)所有收集到的元素。
API

实现
Stack (without pop) or queue (without dequeue).