首页 > 代码库 > LinkList

LinkList

LinkList:java中jdk1.6之后java.util.LinkList 类中对分装链表的理解:

参考:http://www.cnblogs.com/lintong/p/4374292.html

第一部分:熟悉LinkList中哪些方法和其构造;第二部分熟悉使用JDK中LinkList的API接口

 

第1部分:LinkList 的介绍:

linklist:是一个双向链表,当做堆栈,队列或者双端队列进行操作;当做stack时候只能用push,pop,peek方法;当做队列时候用 add,remove,element方法

实现List接口能对队列操作;实现Deque接口当做双端队列使用;

自己编写的Linklist 与JDK中LinkedList相比较,推荐使用自带的LinkedList

 

package MyArrayList;



public class MyLinkList <AnyType> implements Iterable<AnyType> {
    
    //内嵌类node,指向LinkList的头结点(header_node)与尾部结点(tail_node);
    //父亲查看私有类的属性,但是外部不知道有这样私有元素
    private int theSize;
    private int modCount=0;
    private Node<AnyType> beginMaker;
    private Node<AnyType> endMaker;
    private static class Node<AnyType>
    {
        @SuppressWarnings("unused")
        public AnyType data;
        @SuppressWarnings("unused")//你从没有使用这个变量
        public Node<AnyType> prev;
        @SuppressWarnings("unused")
        public Node<AnyType> next;
        //构造函数初始化
        @SuppressWarnings("unused")
        public Node(AnyType data,Node<AnyType>p,Node<AnyType>q)
        {
            this.data=http://www.mamicode.com/data;
            this.prev=p;
            this.next=q;
        }
    }
    public void cLear()
    {
        //申请Node结点同时初始化,后面建立初始链表
        doClear();
    }
    private void doClear()
    {
         beginMaker=new Node<AnyType>(null,null,null);
         endMaker=new Node<AnyType>(null,beginMaker,null);
        beginMaker.next=endMaker;
        
        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,0,size()),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));
    }
    private AnyType remove(Node<AnyType> p)
    {
        p.next.prev=p.prev;
        p.prev.next=p.next;
        theSize--;
        modCount++;
        return p.data;
    }
    private void addBefore ( Node<AnyType> p,AnyType x)
    {
        //P的前面插入
        Node<AnyType>newNode=new Node<>(x,p.prev,p);
        
        p.prev.next=newNode;
        p.prev=newNode;
        theSize++;
        modCount++;
    }
    private Node<AnyType> getNode(int idx)
    {
        return getNode(idx,0,size()-1);
    }
    private Node<AnyType> getNode(int idx,int lower,int upper)
    {
        Node<AnyType>p;
        if(idx<lower || idx> upper)
            throw new IndexOutOfBoundsException();
        if(idx<size()/2)
        {
            p=beginMaker.next;
            for(int i=0;i<idx;i++)
                p=p.next;
        }
        else
        {
            p=endMaker;
            for(int i=size();i>idx;i--)
                p=p.prev;
        }
        return p;
    }
    //采用Iterator接口
    public java.util.Iterator<AnyType> iterator()
    {
        return new LinkedListIterator();
    }
    public class LinkedListIterator implements java.util.Iterator<AnyType>
    {
        private Node<AnyType> current =beginMaker.next;
        private int expectedModCount=modCount;//记录迭代器在迭代的期间被修改的次数
        private boolean okToremove=false;//判断是否已经执行移除操作

        @Override
        public boolean hasNext() {
            // TODO Auto-generated method stub
            return current!=endMaker ;
        }

        @Override
        public AnyType next() {
            // TODO Auto-generated method stub
            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()
        {
            // TODO Auto-generated method stub
                        if(modCount!=expectedModCount)
                            throw new java.util.ConcurrentModificationException();
                        if(!okToremove)
                            throw new java.util.NoSuchElementException();
            MyLinkList.this.remove(current.prev);
            expectedModCount++;
            okToremove=false;
        }
        
    }
    
    
}

 

 

 

 使用小程序对上面编写东西进行测试:

package MyArrayList;
import 自己包下的MyLinkList
import java.util.Iterator;

//使用泛型类,实现Iterator的接口,必须实现Iterator中iterator方法
public class MyArrayList <AnyType> implements Iterable<AnyType>{
    private static final int DEFAULT_CAPACITY=10;//ArryList中默认的容量10
    
    private int theSize;
    private AnyType [] theItems;//Number of elements
    public MyArrayList()
    {//构造函数初始化
        doClear();
    }
    
    private void doClear()
    {
        theSize=0;
        //开辟一个默认为10的数组
        enSureCapacity(DEFAULT_CAPACITY);
    }
    
    /*****为了外部调用内部方法****/
    public int size()
    {
        return theSize;
    }
    public boolean isEmpty()
    {
        return size()==0;
    }
    public void trimToSize()
    {
        enSureCapacity(size());
    }
    @SuppressWarnings("unchecked")
    public void enSureCapacity(int newCapacity)
    {
        if(newCapacity<theSize)
            return;
        //如果发现要的容量大于theSize,必须开辟更多空间
        AnyType [] old=theItems;
        theItems=(AnyType[]) new Object[newCapacity];
        for(int i=0;i<theSize;i++)
            theItems[i]=old[i];
        
    }
    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 add(int idx,AnyType x)
    {//防止是否在数组满情况下相加
        if(size()==theItems.length)
        {
            enSureCapacity(size()*2+1);
        }
        //在idx位置上插入一个数,将idx右侧数据向右边移动,腾出插入位置
        for(int i=theSize;i>idx;i--)
            theItems[i]=theItems[i-1];
        theItems[idx]=x;
        theSize++;
        
        
    }
    public AnyType remove(int idx)
    {
        //与插入相反,将右侧位置覆盖就行
        AnyType removeItem=theItems[idx];
        for(int i=idx;i<size();i++)
            theItems[i]=theItems[i++];
        theSize--;
        return removeItem;
    }
    //对ArrayList数组数进行遍历:
    public void access()
    {  AnyType val=null;
    
        for(int i=0;i<size();i++)
        {
            val=theItems[i];
            System.out.print(val+" ");
        }
    }
    //实现Iterator下iterator方法
    @Override
    public    java.util.Iterator<AnyType>  iterator() {
        // TODO Auto-generated method stub
        return new ArrayListIterator();
        //这个实列实现Iterator接口
    }
    private class ArrayListIterator implements java.util.Iterator<AnyType>
    {
        private int current=0;
        @Override
        public boolean hasNext() {
            // TODO Auto-generated method stub
            
            return current<size();
        }

        @Override
        public AnyType next() {
            // TODO Auto-generated method stub
            if(!hasNext())
                throw new java.util.NoSuchElementException();
            return theItems[current++];
        }
        public void remove()
        {
            MyArrayList.this.remove(--current);
        }
        
    }
    public static  void main(String[] args)
    {
        MyArrayList<Integer> test=new MyArrayList<Integer>();
        test.enSureCapacity(10);
        test.add(0, 1);
        test.access();
        
    }

    
}
*****测试自己编写的LinkList*********
current val3
push stack top is c
After pop 2
pop stack top b
*****测试jdk的LinkList*********
2
1
*****测试自己编写的LinkList*********
显示队列中顶元素a
删除队列中元素a
显示队列中顶元素b
*****测试jdk的LinkList*********
queue.element():20
queue.remove():20

 

LinkList