背包:
它是一种不支持从中删除元素的集合数据类型,目标就是帮助收集全部的元素,并且迭代遍历所有收集到的元素。迭代的顺序不确定,并且与用例无关。
主要的API:
Bag() 创建一个空的背包
void add(Item item) 添加一个元素
boolean isEmpty() 判断是否为空
int size() 背包中的数量
java实现:
链表:
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- public class Bag<Item> implements Iterable<Item> {
- // 背包的两个属性,一个是背包里面的数量,一个是背包里面的节点
- private int N;
- private Node<Item> first;
- private static class Node<Item> {
- private Item item;
- private Node<Item> next;
- }
- // 构造一个空的背包
- public Bag() {
- first = null;
- N = 0;
- }
- // 判断背包是否为空
- public boolean isEmpty() {
- return first == null;
- }
- // 背包的数量
- public int size() {
- return N;
- }
- // 背包的添加
- public void add(Item item) {
- Node<Item> oldfirst = first;
- first = new Node<Item>();
- first.item = item;
- first.next = oldfirst;
- N++;
- }
- // 实现迭代器接口的重写
- @Override
- public Iterator<Item> iterator() {
- return new ListIterator<Item>(first);
- }
- // 构建迭代器对象
- private class ListIterator<Item> implements Iterator<Item> {
- // 迭代器里面有一个属性节点
- private Node<Item> current;
- // 构造器
- public ListIterator(Node<Item> first) {
- current = first;
- }
- // 判断是否迭代完成
- public boolean hasNext() {
- return current != null;
- }
- // 移除
- public void remove() {
- throw new UnsupportedOperationException();
- }
- // 迭代下一个
- public Item next() {
- if (!hasNext())
- throw new NoSuchElementException();
- Item item = current.item;
- current = current.next;
- return item;
- }
- }
- public static void main(String[] args) {
- Bag<String> bag = new Bag<String>();
- bag.add("aaa");
- bag.add("iii");
- for (String s : bag) {
- System.out.println(s);
- }
- }
- }
可变数组实现:
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- public class ResizingArrayBag<Item> implements Iterable<Item> {
- private Item[] a; // 空数组
- private int N = 0; // 背包元素数量
- /**
- * 创建一个空的背包
- */
- public ResizingArrayBag() {
- a = (Item[]) new Object[2];
- }
- /**
- * 背包是否为空
- * @return true 背包为空; false 相反
- */
- public boolean isEmpty() {
- return N == 0;
- }
- /**
- * 返回背包元素数量
- * @return the number of items in this bag
- */
- public int size() {
- return N;
- }
- // 创建一个数组,大小为capacity
- private void resize(int capacity) {
- assert capacity >= N;
- Item[] temp = (Item[]) new Object[capacity];
- for (int i = 0; i < N; i++)
- temp[i] = a[i];
- a = temp;
- }
- /**
- * 给背包中添加元素
- * @param item the item to add to this bag
- */
- public void add(Item item) {
- if (N == a.length) resize(2*a.length); // 如果N的数量等于给定数组大小的话就重新创建一个数组并且数组大小扩大一倍
- a[N++] = item; // 增加元素
- }
- /**
- * Returns an iterator that iterates over the items in the bag in arbitrary order.
- * @return an iterator that iterates over the items in the bag in arbitrary order
- */
- public Iterator<Item> iterator() {
- return new ArrayIterator();
- }
- // an iterator, doesn‘t implement remove() since it‘s optional
- private class ArrayIterator implements Iterator<Item> {
- private int i = 0;
- public boolean hasNext() { return i < N; }
- public void remove() { throw new UnsupportedOperationException(); }
- public Item next() {
- if (!hasNext()) throw new NoSuchElementException();
- return a[i++];
- }
- }
- /**
- * Unit tests the <tt>ResizingArrayBag</tt> data type.
- */
- public static void main(String[] args) {
- ResizingArrayBag<String> bag = new ResizingArrayBag<String>();
- bag.add("Hello");
- bag.add("World");
- bag.add("how");
- bag.add("are");
- bag.add("you");
- for (String s : bag)
- StdOut.println(s);
- }
- }
可以用来统计数字来进行计算。
=============================================================================
队列:
全称先进先出队列,按照任务产生的顺序完成他们的策略,基本上每天都会用到:典型就是排队。服务优先等待最久的人。
主要的API:
Queue() 创建一个空的队列
void enqueue(Item item) 添加一个元素
Item dequeue() 删除最早添加的元素
boolean isEmpty() 判断是否为空
int size() 队列中的数量
java代码实现:
链表:
栈:
下压栈 ,后进先出,好比浏览网页,的后退键等。
java实现:
链表:
数组实现:
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- public class ResizingArrayStack<Item> implements Iterable<Item> {
- private Item[] a;
- private int N;
- public ResizingArrayStack() {
- a = (Item[]) new Object[2];
- }
- public boolean isEmpty() {
- return N == 0;
- }
- public int size() {
- return N;
- }
- private void resize(int cap) {
- assert cap >= N;
- Item[] temp = (Item[]) new Object[cap];
- for (int i = 0; i < N; i++) {
- temp[i] = a[i];
- }
- a = temp;
- }
- public void push(Item item) {
- if (N == a.length) {
- resize(2 * a.length);
- }
- a[N++] = item;
- }
- public Item pop() {
- if (isEmpty()) {
- throw new NoSuchElementException("Stack underflow");
- }
- Item item = a[N - 1];
- a[N - 1] = null;
- N--;
- if (N > 0 && N == a.length / 4) {
- resize(a.length / 2);
- }
- return item;
- }
- public Item peek() {
- if (isEmpty())
- throw new NoSuchElementException("Stack underflow");
- return a[N - 1];
- }
- @Override
- public Iterator<Item> iterator() {
- return new ReverseArrayIterator();
- }
- private class ReverseArrayIterator implements Iterator<Item> {
- private int i;
- public ReverseArrayIterator() {
- i = N - 1;
- }
- public boolean hasNext() {
- return i >= 0;
- }
- public void remove() {
- throw new UnsupportedOperationException();
- }
- public Item next() {
- if (!hasNext())
- throw new NoSuchElementException();
- return a[i--];
- }
- }
- public static void main(String[] args) {
- ResizingArrayStack<String> s = new ResizingArrayStack<String>();
- s.push("kk");
- s.push("pp");
- s.push("pp");
- s.push("pp");
- System.out.println(s.pop());
- System.out.println(s.pop());
- System.out.println(s.pop());
- System.out.println(s.pop());
- System.out.println(s.N);
- }
- }
algorithms第四版学习进程(一)背包,栈,队列
原文:http://www.cnblogs.com/wuwulalala/p/4593317.html