<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    走自己的路

    路漫漫其修遠兮,吾將上下而求索

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      50 隨筆 :: 4 文章 :: 118 評論 :: 0 Trackbacks
    public class CircularLinkedList<E> {
        
    private Entry<E> head;

        
    // Last element of the list. tail.next = head
        private Entry<E> tail;
        
    private Entry<E> next;
        
    private Entry<E> lastReturned;
        
    private int size = 0;

        
    /**
         * Class to hold the individual elements.
         * 
         * 
    @param <E>
         
    */

        
    private static class Entry<E> {
            E element;

            Entry
    <E> next;

            Entry(E element, Entry
    <E> next) {
                
    this.element = element;
                
    this.next = next;
            }


            Entry(E element) 
    {
                
    this.element = element;
            }

        }


        
    public CircularLinkedList() {
            head 
    = tail = null;
        }


        @SuppressWarnings(
    "unchecked")
        
    public CircularLinkedList(Collection<? extends E> c) {
            Object[] eles 
    = c.toArray();
            
    int nums = eles.length;

            
    for (int i = 0; i < nums; i++{
                
    this.add((E) eles[i]);
            }

        }


        
    /**
         * Circular iterator
         * 
         * 
    @return
         
    */

        
    public synchronized E next() {
            
    if (head == null{
                
    throw new NoSuchElementException();
            }
     else {
                
    // Entry<E> rtned = next;
                lastReturned = next;
                next 
    = next.next;
                
    return lastReturned.element;
            }

        }


        
    public synchronized boolean hasNext() {
            
    return size > 0;
        }


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


        
    /**
         * Remove obj from the circular linked list and return the removed object
         * 
         * 
    @param obj
         * 
    @return
         
    */

        
    public synchronized E remove(E obj) {
            
    if (head == null || tail == null)
                
    throw new NoSuchElementException();
            Entry
    <E> p = head, temp = head, found = null;

            
    if (head.element.equals(obj)) {
                
    if (head.next == head) {
                    found 
    = head;
                    head 
    = null;
                    tail 
    = null;
                    size
    --;
                    
    return found.element;
                }
     else {
                    found 
    = head;
                    head 
    = found.next;
                    temp 
    = tail;
                }

            }
     else {
                p 
    = head.next;
                
    while (p != head) {
                    
    if (p.element.equals(obj)) {
                        found 
    = p;
                        
    break;
                    }

                    temp 
    = p;
                    p 
    = p.next;
                }

                
    if (found == tail) {
                    tail 
    = temp;
                }

            }


            
    if (found == null{
                
    throw new NoSuchElementException();
            }


            E result 
    = found.element;
            temp.next 
    = found.next;

            
    if (found == next) {
                next 
    = found.next;
            }


            found.next 
    = null;
            found.element 
    = null;
            size
    --;
            
    return result;
        }

        
        
    /**
         * Contains the specified object or not
         * 
    @param obj
         * 
    @return
         
    */

        
    public synchronized boolean contains(E obj) {
            
    if (head == null || tail == null)
                
    return false;
            
            Entry
    <E> found = null;
            
    if (head.element.equals(obj)) {
                found 
    = head;
            }
     else {
                Entry
    <E> p = head.next;
                
    while (p != head) {
                    
    if (p.element.equals(obj)) {
                        found 
    = p;
                        
    break;
                    }

                    p 
    = p.next;
                }

            }

            
    if (found == null{
                
    return false;
            }

            
    return true;
        }


        
    /**
         * Add obj to the circular linked list.
         * 
         * 
    @param obj
         
    */

        
    public synchronized void add(E obj) {
            Entry
    <E> e = new Entry<E>(obj);
            
    if (head == null{
                size
    ++;
                head 
    = e;
                head.next 
    = head;
                tail 
    = head;
                next 
    = head;
                
    return;
            }

            
    if (lastReturned == tail) {
                next 
    = e;
            }

            size
    ++;
            tail.next 
    = e;
            tail 
    = e;
            tail.next 
    = head;
        }


        
    /**
         * Size of the list.
         * 
         * 
    @return
         
    */

        
    public synchronized int size() {
            
    return size;
        }


        
    /**
         * 
    @return the head
         
    */

        
    public synchronized E getHead() {
            
    if (null == head) {
                
    throw new NoSuchElementException();
            }

            
    return head.element;
        }


        
    /**
         * 
    @return the tail
         
    */

        
    public synchronized E getTail() {
            
    if (null == tail) {
                
    throw new NoSuchElementException();
            }

            
    return tail.element;
        }

    }

    Test:
    @RunWith(JDaveRunner.class)
    public class CircularLinkedListSpec extends
            Specification
    <CircularLinkedList<String>> {

        
    public class EmptyCLL {
            
    private CircularLinkedList<String> cll;

            
    public CircularLinkedList<String> create() {
                
    return this.cll = new CircularLinkedList<String>();
            }


            
    public void getHead() {
                specify(
    new Block() {
                    
    public void run() throws Throwable {
                        cll.getHead();
                    }

                }
    , must.raiseExactly(NoSuchElementException.class));
            }


            
    public void getTail() {
                specify(
    new Block() {
                    
    public void run() throws Throwable {
                        cll.getTail();
                    }

                }
    , must.raiseExactly(NoSuchElementException.class));
            }


            
    public void size() {
                specify(cll.size(), must.equal(
    0));
            }


            
    public void next() {
                specify(
    new Block() {
                    
    public void run() throws Throwable {
                        cll.next();
                    }

                }
    , must.raiseExactly(NoSuchElementException.class));
            }


            
    public void remove() {
                specify(
    new Block() {
                    
    public void run() throws Throwable {
                        cll.remove(
    "gavin");
                    }

                }
    , must.raiseExactly(NoSuchElementException.class));
            }


            
    public void add() {
                
    this.cll.add("gavin");
                specify(cll.size(), 
    1);
                specify(cll.getHead(), 
    "gavin");
                specify(cll.getTail(), 
    "gavin");
            }

        }


        
    public class OnlyHeadCLL {
            
    private CircularLinkedList<String> cll;

            
    public CircularLinkedList<String> create() {
                List
    <String> list = new ArrayList<String>();
                list.add(
    "gavin");
                
    return this.cll = new CircularLinkedList<String>(list);
            }


            
    public void getHead() {
                specify(cll.getHead(), must.equal(
    "gavin"));
            }


            
    public void getTail() {
                specify(cll.getHead(), must.equal(
    "gavin"));
            }


            
    public void size() {
                specify(cll.size(), must.equal(
    1));
            }


            
    public void next() {
                String current 
    = cll.next();
                specify(current, must.equal(
    "gavin"));
            }


            
    public void remove() {
                String removed 
    = cll.remove("gavin");
                specify(removed, 
    "gavin");
                specify(cll.size(), must.equal(
    0));
                specify(
    new Block() {
                    
    public void run() throws Throwable {
                        cll.getHead();
                    }

                }
    , must.raiseExactly(NoSuchElementException.class));
                specify(
    new Block() {
                    
    public void run() throws Throwable {
                        cll.getTail();
                    }

                }
    , must.raiseExactly(NoSuchElementException.class));
                specify(
    new Block() {
                    
    public void run() throws Throwable {
                        cll.next();
                    }

                }
    , must.raiseExactly(NoSuchElementException.class));
            }


            
    public void removeTwice() {
                String removed 
    = cll.remove("gavin");
                specify(removed, 
    "gavin");
                specify(cll.size(), must.equal(
    0));
                specify(
    new Block() {
                    
    public void run() throws Throwable {
                        cll.remove(
    "gavin");
                    }

                }
    , must.raiseExactly(NoSuchElementException.class));
            }


            
    public void add() {
                
    this.cll.add("afka");
                specify(cll.size(), must.equal(
    2));
                specify(cll.getHead(), 
    "gavin");
                specify(cll.getTail(), 
    "afka");
            }


        }


        
    public class NormalCLL {
            
    private CircularLinkedList<String> cll;

            
    public CircularLinkedList<String> create() {
                List
    <String> list = new ArrayList<String>();
                list.add(
    "gavin");
                list.add(
    "afka");
                list.add(
    "eddie");
                
    return this.cll = new CircularLinkedList<String>(list);
            }

            
    public void add() {
                specify(cll.size(), 
    3);
                specify(
    this.cll.getHead(), "gavin");
                specify(
    this.cll.getTail(), "eddie");
                
    this.cll.add("rex");
                specify(cll.size(), 
    4);
                specify(
    this.cll.getHead(), "gavin");
                specify(
    this.cll.getTail(), "rex");
            }


            
    public void remove() {
                specify(cll.size(), 
    3);
                
    this.cll.remove("afka");
                specify(cll.size(), 
    2);
                specify(
    this.cll.getHead(), "gavin");
                specify(
    this.cll.getTail(), "eddie");
            }


            
    public void removeHead() {
                specify(cll.size(), 
    3);
                
    this.cll.remove("gavin");
                specify(cll.size(), 
    2);
                specify(
    this.cll.getHead(), "afka");
            }


            
    public void removeTail() {
                specify(cll.size(), 
    3);
                
    this.cll.remove("eddie");
                specify(cll.size(), 
    2);
                specify(
    this.cll.getTail(), "afka");
            }


            
    public void removeNotExist() {
                specify(
    new Block() {
                    
    public void run() throws Throwable {
                        cll.remove(
    "rex");
                    }

                }
    , must.raiseExactly(NoSuchElementException.class));
            }

        }


        
    public class Iteration {
            
    private CircularLinkedList<String> cll;

            
    public CircularLinkedList<String> create() {
                List
    <String> list = new ArrayList<String>();
                list.add(
    "gavin");
                list.add(
    "afka");
                list.add(
    "eddie");
                
    return this.cll = new CircularLinkedList<String>(list);
            }

            
            
    public void next() {
                String step1 
    = this.cll.next();
                specify(
    3, cll.size());
                specify(step1, 
    "gavin");
                specify(cll.getHead(), 
    "gavin");
                specify(cll.getTail(), 
    "eddie");
            }

            
            
    public void addWhenNext() {
                String step1 
    = this.cll.next();
                specify(step1, 
    "gavin");
                String step2 
    = this.cll.next();
                specify(step2, 
    "afka");
                
    this.cll.add("rex");
                String step3 
    = this.cll.next();
                specify(step3, 
    "eddie");
                specify(cll.getTail(), 
    "rex");
                String step4 
    = this.cll.next();
                specify(step4, 
    "rex");
                specify(cll.getTail(), 
    "rex");
                specify(cll.getHead(), 
    "gavin");
            }

            
            
    public void removeSubWhenNext() {
                String step1 
    = this.cll.next();
                specify(step1, 
    "gavin");
                
    this.cll.remove("eddie");
                specify(
    this.cll.next(), "afka");
                specify(
    this.cll.next(), "gavin");
            }

            
            
    public void removePreWhenNext() {
                specify(
    this.cll.next(), "gavin");
                specify(
    this.cll.next(), "afka");
                
    this.cll.remove("gavin");
                specify(
    this.cll.next(), "eddie");
                specify(
    this.cll.next(), "afka");
            }

            
            
    public void nextIsJustRemoved() {
                String step1 
    = this.cll.next();
                specify(step1, 
    "gavin");
                
    this.cll.remove("afka");
                String step2 
    = this.cll.next();
                specify(step2, 
    "eddie");
            }

            
            
    public void nextIsJustAdded() {
                String step1 
    = this.cll.next();
                specify(step1, 
    "gavin");
                String step2 
    = this.cll.next();
                specify(step2, 
    "afka");
                String step3 
    = this.cll.next();
                specify(step3, 
    "eddie");
                specify(cll.getTail(), 
    "eddie");
                
    this.cll.add("rex");
                String step4 
    = this.cll.next();
                specify(step4, 
    "rex");
                specify(cll.getTail(), 
    "rex");
                specify(cll.getHead(), 
    "gavin");
            }

            
            
    public void iterationCycle() {
                specify(
    3, cll.size());
                specify(
    this.cll.next(), "gavin");
                specify(
    this.cll.next(), "afka");
                specify(
    this.cll.next(), "eddie");
                specify(
    this.cll.next(), "gavin");
                specify(cll.getHead(), 
    "gavin");
                specify(cll.getTail(), 
    "eddie");
            }

        }

    }


    posted on 2009-04-01 12:42 叱咤紅人 閱讀(536) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲人成网站观看在线播放| 四虎成人精品永久免费AV| 国产亚洲欧美在线观看| 中文字幕在线日亚洲9| 精品日韩99亚洲的在线发布| 亚洲国产精品久久网午夜| 亚洲黄色在线观看网站| 亚洲综合激情九月婷婷| 亚洲成人网在线观看| 亚洲午夜在线一区| 亚洲sss综合天堂久久久| 午夜在线a亚洲v天堂网2019| 亚洲一卡2卡3卡4卡5卡6卡 | 久久久久国产精品免费免费搜索 | 永久免费不卡在线观看黄网站| 一级特黄录像免费播放肥| 中文永久免费观看网站| 毛片在线全部免费观看| 日日麻批免费40分钟无码| 57pao国产成视频免费播放| 青草草色A免费观看在线| 在线观看免费宅男视频| 免费人成网站7777视频| 亚洲色自偷自拍另类小说| 亚洲视频在线观看免费| 国产91在线|亚洲| 国产亚洲成在线播放va| 中文字幕手机在线免费看电影| 久久国产乱子伦精品免费不卡| 日韩免费一区二区三区在线播放| 免费观看的av毛片的网站| 国产啪亚洲国产精品无码| 亚洲欧洲免费视频| 亚洲中文字幕无码久久2020| 一级做a爰片久久毛片免费陪 | 亚洲一日韩欧美中文字幕在线| 无遮挡免费一区二区三区| 久久大香伊焦在人线免费| 国产免费av片在线看| 亚洲中文字幕无码爆乳av中文| 亚洲网站在线观看|