表,单链表,双链表

public class MyArrayList<AnyType> implements Iterable<AnyType>{

    private static final int DEFAULT_CAPACITY=10;
    private int theSize;
    private AnyType [] theItems;
    public MyArrayList(){
        clear();
    }
    public  void clear() {
        // TODO Auto-generated method stub
        theSize=0;
        ensureCapacity(DEFAULT_CAPACITY);
    }
    public int size(){
        return theSize;
    }

    public boolean isEmpty(){
        return size()==0;
    }

    public void trimToSize(){
        ensureCapacity(size());
    }

    public AnyType get(int idx){
        if(idx<0||idx>=size()){
            throw new ArrayIndexOutOfBoundsException();
        }
        return theItems[idx];
    }

    public AnyType set(int idx,AnyType newVal){
        if(idx<0||idx>=size()){
            throw new ArrayIndexOutOfBoundsException();
        }
        AnyType old=theItems[idx];
        theItems[idx]=newVal;
        return old;
    }

    public void ensureCapacity(int newCapacity) {
        // TODO Auto-generated method stub
        if(newCapacity<theSize){
            return;
        }
        AnyType[] old=theItems;
        theItems=(AnyType[])new Object[newCapacity];
        for(int i=0;i<size();i++){
            theItems[i]=old[i];
        }
    }

    public boolean add(AnyType x){
        add(size(),x);
        return true;
    }
    public void add(int idx,AnyType x){
        if(theItems.length==size()){
            ensureCapacity(size()*2+1);
        }
        for(int i=theSize;i>idx;i--){
            theItems[i]=theItems[i-1];
        }
        theItems[idx]=x;
        theSize++;
    }
    public void addAll(Iterable<?extends AnyType> items){
        Iterator<? extends AnyType> iter=items.iterator();
        while(iter.hasNext()){
            add(iter.next());
        }
    }
    public void removeAll(Iterable<?extends AnyType> items){
        AnyType item,element;
        Iterator<? extends AnyType> iter=items.iterator();
        while(iter.hasNext()){
            item=iter.next();
            Iterator<? extends AnyType> iterList=iterator();
            while(iterList.hasNext()){
                element=iterList.next();
                if(element.equals(item))
                    iterList.remove();
            }
        }
    }
    public AnyType remove(int idx){
        AnyType removedItem=theItems[idx];
        for(int i=idx;i<size()-1;i++){
            theItems[i]=theItems[i+1];
        }
        theSize--;
        return removedItem;
    }
    @Override
    public java.util.Iterator<AnyType> iterator(){
        return new ArrayListIterator();
    }

    public class ArrayListIterator implements java.util.Iterator<AnyType>{
        private int current=0;
        public boolean hasNext(){
            return current<size();
        }

        public AnyType next(){
            if(!hasNext()){
                throw new java.util.NoSuchElementException();
            }
            return theItems[current++];
        }
        public void remove(){
            MyArrayList.this.remove(--current);
        }
    }
}

单链表

public class SingleList {
    SingleList(){
        init();
    }
    boolean add(Object x){
        if(contains(x))
            return false;
        else{
            Node<Object> p=new Node<Object>(x);
            p.next=head.next;
            head.next=p;
            theSize++;
        }
        return true;
    }
    boolean remove(Object x){
        if(!contains(x)) return false;
        else{
            Node<Object> p=head.next;
            Node<Object> trailer=head;
            while(!p.data.equals(x)){
                trailer=p;
                p=p.next;
            }
            trailer.next=p.next;
            theSize--;
        }
        return true;
    }
    int size(){
        return theSize;
    }
    void print(){
        Node<Object> p=head.next;
        while(p!=null){
            System.out.println(p.data+" ");
            p=p.next;
        }
        System.out.println();
    }
    boolean contains(Object x){
        Node<Object> p=head.next;
        while(p!=null){
            if(x.equals(p.data))
                return true;
            else
                p=p.next;
        }
        return false;
    }
    void init(){
        theSize=0;
        head=new Node<Object>();
        head.next=null;
    }
    private Node<Object> head;
    private int theSize;
    private class Node<Object>{
        Object data;
        Node next;
        Node(){
            this(null,null);
        }
        Node(Object d){
            this(d,null);
        }
        Node(Object d, Node n) {
            // TODO Auto-generated constructor stub
            data=d;
            next=n;
        }
    }
}

双链表

public class MyLinkedList<AnyType> implements Iterable<AnyType> {

    private int theSize;
    private int modCount=0;
    private Node<AnyType> beginMarker;
    private Node<AnyType> endMarker;

    private static class Node<AnyType>{
        public Node(AnyType d,Node<AnyType> p,Node<AnyType> n){
            data=d;
            prev=p;
            next=n;
        }
        public AnyType data;
        public Node<AnyType>prev;
        public Node<AnyType> next;

    }

    public MyLinkedList(){
        clear();
    }
    /*
     * Change the size of this collection to zero
     */
    public void clear(){
        beginMarker =new Node<AnyType>(null,null, null);
        endMarker=new Node<AnyType>(null,beginMarker,null);
        beginMarker.next=endMarker;
        theSize=0;
        modCount++;
    }
    public int size(){
        return theSize;
    }
    public boolean isEmpty(){
        return size()==0;
    }
    public boolean add(AnyType x){
        add(size(),x);
        return true;
    }
    public void add(int idx,AnyType x){
        addBefore(getNode(idx),x);

    }

    public AnyType get(int idx){
        return getNode(idx).data;
    }

    public AnyType set(int idx,AnyType newVal){
        Node<AnyType> p=getNode(idx);
        AnyType oldVal=p.data;
        p.data=newVal;
        return oldVal;
    }

    public AnyType remove(int idx){
        return remove(getNode(idx));
    }



    /*
     * add an item to this collection ,at specified position p
     */
    private void addBefore(Node<AnyType> p,AnyType x){
        Node<AnyType> newNode=new Node<AnyType>(x,p.prev,p);
        newNode.prev.next=newNode;
        p.prev=newNode;
        theSize++;
        modCount++;
    }
    /*
     * Removes the object containted in Node p
     */
    private AnyType remove(Node<AnyType> p){
        p.next.prev=p.prev;
        p.prev.next=p.next;
        theSize--;
        modCount++;

        return p.data;
    }
    /*
     * Gets the Node at position idx,which must range from 0 to size()
     */
    private Node<AnyType> getNode(int idx){
        Node<AnyType> p;
        if(idx<0||idx>size()){
            throw new IndexOutOfBoundsException();
        }
        if(idx<size()/2){
            p=beginMarker.next;
            for(int i=0;i<idx;i++){
                p=p.next;
            }
        }else{
            p=endMarker;
            for(int i=size();i>idx;i--){
                p=p.prev;
            }
        }
        return p;
    }
    public boolean contains(AnyType x){
        Node<AnyType> p=beginMarker.next;
        while(p!=endMarker&&!(p.data.equals(x))){
            p=p.next;
        }
        return (p!=endMarker);
    }
    @Override
    public Iterator<AnyType> iterator() {
        // TODO Auto-generated method stub
        return new LinkedListIterator();
    }

    private class LinkedListIterator implements java.util.Iterator<AnyType>{
        private Node<AnyType> current=beginMarker.next;
        private int expectedModCount=modCount;
        private boolean okToRemove=false;
        public boolean hasNext(){
            return current!=endMarker;
        }

        public AnyType next(){
            if(modCount!=expectedModCount)
                throw new java.util.ConcurrentModificationException();
            if(!hasNext())
                throw new java.util.NoSuchElementException();

            AnyType nextItem=current.data;
            current =current.next;
            okToRemove=true;
            return nextItem;
        }

        public void remove(){
            if(modCount!=expectedModCount)
                throw new java.util.ConcurrentModificationException();

            if(!okToRemove)
                throw new IllegalStateException();
            MyLinkedList.this.remove(current.prev);
            okToRemove=false;
            expectedModCount++;
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值