普林斯顿Algorithms-1.3.1-包、队列、栈的数组实现

Bags, Queues, and Stacks (数组实现)


Several fundamental data types involve collections of objects. Specifically, the set of values is a collection of objects, and the operations revolve around adding, removing, or examining objects in the collection. In this section, we consider three such data types, known as the bag, the queue, and the stack. 

API

They differ in the specification of which object is to be removed or examined next.

泛型:集合类的抽象数据类型的一个关键特性是我们可以用他们存储任意类型的数据。


自动装箱拆箱


可迭代的集合类型


包,先进先出队列,下压栈

包不支持删除,为了收集和遍历而生,适合用于统计

队列来得早走的早,跟排队一样,适合任务调度

栈先来的最后走,浏览器的浏览顺序就是一个栈,点击后退就返回之前的页面


算术表达式求值
Dijkstra双栈算法实现的简单解释器

使用定容数组和扩容数组  实现集合类(非常重要)

Fixed-capacity stack of strings. FixedCapacityStackOfString.java implements a fixed-capacity stack of strings using an array.

Fixed-capacity generic stack. FixedCapacityStack.java implements a generic fixed-capacity stack.

Array resizing stack. ResizingArrayStack.java implements a generic stack using a resizing array. With a resizing array, we dynamically adjust the size of the array so that it is both sufficiently large to hold all of the items and not so large as to waste an excessive amount of space. We double the size of the array in push() if it is full; we halve the size of the array in pop() if it is less than one-quarter full.

Array resizing queue. ResizingArrayQueue.java implements the queue API with a resizing array.

动态数组实现下压栈/队列是一个很重要的算法,它几乎达到了任意集合类数据类型实现的最佳性能:

操作用时和集合大小无关

空间需求不超过集合大小*常数

但动态调整的有一个复制原数组的造作,耗时和size正相关,于是为了更加优化,链表诞生了


你可能感兴趣的:(普林斯顿Algorithms-1.3.1-包、队列、栈的数组实现)