<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无码无线在线观看| 亚洲国产精品一区二区久久hs| 久久久久亚洲AV无码观看| 久久大香香蕉国产免费网站| 久久亚洲精品成人| 99视频在线精品免费| 亚洲欧洲日产国码www| 久久WWW免费人成一看片| 亚洲午夜电影一区二区三区| 日本视频一区在线观看免费| 亚洲一区在线免费观看| 好男人看视频免费2019中文| 青青青亚洲精品国产| 亚洲精品A在线观看| 中国一级特黄的片子免费| 亚洲av永久无码精品古装片| 91香蕉在线观看免费高清| 亚洲国产成人精品无码一区二区| 青青在线久青草免费观看| 亚洲精品乱码久久久久蜜桃 | 亚洲色偷偷综合亚洲AV伊人蜜桃| 好大好深好猛好爽视频免费| 羞羞的视频在线免费观看| 国产亚洲精品va在线| 99久久久国产精品免费无卡顿| 国产精品亚洲专区无码唯爱网| 亚洲视频在线一区二区| 无码少妇精品一区二区免费动态| 亚洲宅男天堂a在线| 国产免费看插插插视频| 日本免费高清视频| 亚洲宅男精品一区在线观看| 无码欧精品亚洲日韩一区夜夜嗨| 91成人免费观看在线观看| 亚洲国产精品久久人人爱| 免费人妻av无码专区| 日韩精品极品视频在线观看免费| 亚洲国产精品自在自线观看| 中国亚洲女人69内射少妇| 毛片a级毛片免费播放100| 国产一级a毛一级a看免费视频 |