循環單鏈表-數據結構-java實現


循環單鏈表-數據結構-java實現

目錄

抽象表:1

循環單鏈表實現:1

循環單鏈表輸出測試:4

輸出結果:5

 

抽象表:

package edu.cquptzx.List;

publicinterface List

{

    publicvoid insert(int i ,Object obj) throws Exception;        //插入

    public Object delete(int i ) throws Exception;                 //刪除

    public Object getData(int i ) throws Exception;                //獲取i元素

    publicint size();                                          //表數據總數

    publicboolean isEmpty();                                   //是否為空

   

}

循環單鏈表實現:

package edu.cquptzx.List;

 

publicclass LoopLinkList implements List {

    Node head;

    Node current;

    intsize;

   

    LoopLinkList()

    {

       head = current = new Node(null);

       head.next = head;

       size =0;

    }

    /**

     * 定位成員函數index(int i)的實現

     * 循環從頭開始查找,循環的條件是:1.定位完成j==i;2.鏈表查找結束了.

     * @param i

     * @throws Exception 當參數i錯誤時,拋出異常.

     */

    publicvoid index(int i )throws Exception

    {

       if(i<-1 || i >size-1)

       {

           thrownew Exception("i error in INDEX.");

       }

       if(i == -1) return;

       current = head.next;

       int j = 0;

       while(current!=head && j<i)

       {

           current = current.next;

           j++;

       }

    }

    /**

     * 插入節點算法:

     * 1.調用index(i-1),讓成員變量current指向第i-1個節點.

     * 2.obj,current.next為參數創建新的節點.

     * 3.更改current指向,改為下一個節點.

     * 4.表元素總數加1.

     */

    publicvoid insert(int i, Object obj) throws Exception {

       if(i<0 || i>size)

       {

           thrownew Exception ("i error in INSERT.");

       }

       index(i-1);

       current.setNext(new Node(obj,current.next));

       size++;

    }

 

    /**

     * 刪除節點算法:

     * 1.調用index(i-1),讓成員變量current指向第i-1個節點.

     * 2.把第i個節點脫鏈:讓第i-1個節點的next域等於第i個節點的next.

     * 3.數據元素總數size1.

     */

    public Object delete(int i) throws Exception {

       if(size == 0)

       {

           thrownew Exception ("Link Blank in DELETE.");

       }

       if(i<0 || i>size-1)

       {

           thrownew Exception ("i error in DELETE.");

       }

       index(i-1);

       Object obj = current.next.getElement();

       current.setNext(current.next.next);

       size--;

       return obj;

    }

    /**

     * 獲取指定的元素

     * 1.調用index(i),讓成員變量current指向第i個節點.

     * 2.返回該節點的數據域的值.

     */

    @Override

    public Object getData(int i) throws Exception {

       // TODO Auto-generated method stub

       if(i<-1 || i>size-1)

       {

           thrownew Exception ("i error in getData.");

       }

       index(i);

       returncurrent.getElement();

    }

 

    @Override

    publicint size() {

       // TODO Auto-generated method stub

       returnsize;

    }

 

    @Override

    publicboolean isEmpty() {

       // TODO Auto-generated method stub

       returnsize ==0;

    }

   

}

循環單鏈表輸出測試:

package edu.cquptzx.List;

 

publicclass LoopLinkListTest

{

    publicstaticvoid main(String agrs[])

    {

       LoopLinkList lplklt = new LoopLinkList();

       int n = 10;

       try

       {

           for(int i = 0;i<n;i++)

           {

              lplklt.insert(i, new Integer(i+1));

           }

           lplklt.delete(4);

           for(int i = 0;i<lplklt.size;i++)

           {

              System.out.print(lplklt.getData(i)+"...end ");

           }

       }

       catch(Exception e)

       {

           System.out.println(e.getMessage());

       }

    }

}

 

輸出結果:

image


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM