背包

作者: null12 | 来源:发表于2018-03-21 10:14 被阅读0次

    一、定义

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

    1-1 背包示意图

    二、API

    2-1 背包的API定义

    三、实现

    3.1 数组方式实现

    可动态调整大小的背包实现(数组方式):

    • 当包容量满时,采用遍历数组的方式进行扩容,默认调整为原容量的2倍。
    • 当包容量变成1/4时,默认调整为原容量的1/2。
    public class ResizingArrayBag<Item> implements Iterable<Item> {
        private Item[] a;         // array of items
        private int n;            // number of elements on bag
    
        /**
         * Initializes an empty bag.
         */
        public ResizingArrayBag() {
            a = (Item[]) new Object[2];
            n = 0;
        }
    
        /**
         * Is this bag empty?
         * @return true if this bag is empty; false otherwise
         */
        public boolean isEmpty() {
            return n == 0;
        }
    
        /**
         * Returns the number of items in this bag.
         * @return the number of items in this bag
         */
        public int size() {
            return n;
        }
    
        // resize the underlying array holding the elements
        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;
        }
    
        /**
         * Adds the item to this bag.
         * @param item the item to add to this bag
         */
        public void add(Item item) {
            if (n == a.length) resize(2*a.length);    // double size of array if necessary
            a[n++] = item;                            // add 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++];
            }
        }
    }
    

    3.2 链表方式实现

    背包的链表实现方式:

    • 添加元素相当于“头插法”插入元素。
    public class Bag<Item> implements Iterable<Item> {
        private Node<Item> first;    // beginning of bag
        private int n;               // number of elements in bag
    
        // helper linked list class
        private static class Node<Item> {
            private Item item;
            private Node<Item> next;
        }
    
        /**
         * Initializes an empty bag.
         */
        public Bag() {
            first = null;
            n = 0;
        }
    
        /**
         * Returns true if this bag is empty.
         *
         * @return {@code true} if this bag is empty;
         *         {@code false} otherwise
         */
        public boolean isEmpty() {
            return first == null;
        }
    
        /**
         * Returns the number of items in this bag.
         *
         * @return the number of items in this bag
         */
        public int size() {
            return n;
        }
    
        /**
         * Adds the item to this bag.
         *
         * @param  item the item to add to this bag
         */
        public void add(Item item) {
            Node<Item> oldfirst = first;
            first = new Node<Item>();
            first.item = item;
            first.next = oldfirst;
            n++;
        }
    
    
        /**
         * Returns an iterator that iterates over the items in this bag in arbitrary order.
         *
         * @return an iterator that iterates over the items in this bag in arbitrary order
         */
        public Iterator<Item> iterator()  {
            return new ListIterator<Item>(first);  
        }
    
        // an iterator, doesn't implement remove() since it's optional
        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;
            }
        }
    }
    

    相关文章

      网友评论

        本文标题:背包

        本文链接:https://www.haomeiwen.com/subject/hqswqftx.html