java背包的数组实现,链表实现

数组实现

package base.structure;

import java.lang.reflect.Array;
import java.util.Iterator;

/**
 * @program: Algorithm4J
 * @description: 背包数组的实现
 * @author: Mr.Dai
 * @create: 2018-12-05 22:56
 **/

class Bag implements Iterable{

    int n;
    T [] elements;

    Bag(){
        elements= (T[]) new Object[20];
        n=0;
    }

    void add(T item){
        if(n==elements.length){
            T [] new_elements= (T[]) new Object[elements.length*2];
            System.arraycopy(elements,0,new_elements,0,elements.length);
            elements=new_elements;
        }
        elements[n++]=item;
    }

    boolean isEmpty(){
        return n==0;
    }
    int Size(){
        return n;
    }

    @Override
    public Iterator iterator() {
        return new BagIterator();
    }

    class BagIterator implements Iterator{

        private int i=0;
        @Override
        public boolean hasNext() {
            return i<n;
        }

        @Override
        public T next() {
            return elements[i++];
        }
    }
}

public class BagForArray {

    public static void main(String[] args) {
        Bag bag = new Bag<>();
        bag.add(1);
        bag.add(2);
        for (int i = 0; i < 3; i++) {
            bag.add(4);
        }
        Iterator iterator = bag.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }

    }

}

链表的实现

 

package base.structure;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * @program: Algorithm4J
 * @description: 背包逆序链表实现
 * @author: Mr.Dai
 * @create: 2018-12-06 10:25
 **/

class Bags implements Iterable{

    int n;
    Node fisrt;

    /**
     * 构建一个链表类
     * @param 
     */
    private static class Node{
        T val;
        Node next;
    }

    /**
     * 有头结点才能正序取到值
     */
    Bags(){
        fisrt=null;
        n=0;
    }

    void add(T item){
        Node old_Node=fisrt;
        fisrt=new Node<>();
        fisrt.val=item;
        fisrt.next=old_Node;
        n++;
    }

    boolean isEmpty(){
        return fisrt==null;
    }

    int Size(){
        return n;
    }


    @Override
    public Iterator iterator() {
        return new BagsIterator(fisrt);
    }

    class BagsIterator implements Iterator{

        private Node current;

        BagsIterator(Node item){
            current=item;
        }
        public void remove(){
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean hasNext() {
            return current!=null;
        }

        @Override
        public T next() {
            if(!hasNext()) throw new NoSuchElementException();
            T item=current.val;
            current=current.next;
            return item;
        }
    }


}

public class BagForLinked {

    public static void main(String[] args) {
        Bags bag=new Bags<>();

        bag.add("a");
        bag.add("b");
        bag.add("c");
        bag.add("d");

        for (String s : bag) {
            System.out.println(s);
        }

    }
}

 

你可能感兴趣的:(java背包的数组实现,链表实现)