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

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

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

    posts - 495,  comments - 11,  trackbacks - 0

    interface List
    {
    public void insert(int i,Object obj) throws Exception;?? //插入
    public Object delete(int i) throws Exception;??????????? //刪除
    public Object getData(int i) throws Exception;?????????? //取數據元素
    public int size();?????????????????????????????????????? //求元素個數
    public boolean isEmpty();??????????????????????????????? //是否空
    }

    //單鏈表結點類

    class Node
    {
    Object element;?? //數據元素
    Node next;?????? //表示下一個結點的對象引用

    Node(Node nextval)?? //用于頭結點的構造函數1
    {
    ?? next = nextval;
    }

    Node(Object obj,Node nextval)?? //用于其他結點的構造函數2
    {
    ?? element = obj;
    ?? next = nextval;
    }

    public Node getNext()?? //取next
    {
    ?? return next;
    }

    public void setNext(Node nextval)?? //置next
    {
    ?? next = nextval;
    }

    public Object getElement()??? //取element
    {
    ?? return element;
    }

    public void setElement(Object obj)?? //置element
    {
    ?? element = obj;
    }

    public String toString()?? //轉換element為String類型
    {
    ?? return element.toString();
    }
    }

    //單鏈表類

    class LinList implements List
    {
    Node head;????? //頭指針
    Node current;?? //當前結點位置
    int size;?????? //數據元素個數

    LinList()?????? //構造函數
    {
    ?? head = current = new Node(null);
    ?? size =0;
    }

    public void index(int i) throws Exception
    { //定位到第i個結點(以0開始記起)
    ????? if(i < -1 || i > size-1)
    ????? {
    ??? throw new Exception("參數錯誤!");
    ?? }
    ?? if(i == -1) return;
    ?? current = head.next;
    ?? int j = 0;
    ?? while((current != null) && j < i)
    ?? {
    ??? current = current.next;
    ??? j++;
    ?? }
    ???? }

    ???? public void insert(int i,Object obj) throws Exception
    ???? {
    ?? if(i < 0 || i > size)
    ?? {
    ??? throw new Exception("參數錯誤!");
    ?? }
    ?? index(i-1);
    ?? current.setNext(new Node(obj,current.next));
    ?? size++;
    }

    public Object delete(int i) throws Exception
    {
    ?? if(size == 0)
    ?? {
    ??? throw new Exception("鏈表已空無元素可刪除!");
    ?? }
    ?? if(i < 0 || i > size-1)
    ?? {
    ??? throw new Exception("參數錯誤!");
    ?? }
    ?? index(i-1);
    ?? Object obj = current.next.getElement();
    ?? current.setNext(current.next.next);
    ?? size--;
    ?? return obj;
    }

    public Object getData(int i) throws Exception
    {
    ????? if(i < -1 || i > size-1)
    ????? {
    ??? throw new Exception("參數錯誤!");
    ?? }
    ?? index(i);
    ?? return current.getElement();
    }

    public int size()
    {
    ?? return size;
    }

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

    /* 主函數
    * 刪除數列{1,2,3,4,5,6,7,8,9,10}里的元素5
    */

    public class LinListTest
    {
    public static void main(String args[])
    {
    ?? LinList linList = new LinList();
    ?? int n = 10;
    ?? try
    ?? {
    ??? for(int i = 0;i < n;i++)
    ??? {
    ???? linList.insert(i,new Integer(i+1));
    ??? }

    ??? linList.delete(4);

    ??? for(int i =0;i<linList.size;i++)
    ??? {
    ???? System.out.print(linList.getData(i)+"?? ");
    ??? }
    ?? }
    ?? catch(Exception e)
    ?? {
    ??? System.out.println(e.getMessage());
    ?? }
    }
    }

    posted on 2007-05-04 10:50 jadmin 閱讀(138) 評論(0)  編輯  收藏

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


    網站導航:
     
    主站蜘蛛池模板: 久久免费美女视频| 成人A毛片免费观看网站| 99精品免费观看| 国产AV无码专区亚洲AV男同| 岛国精品一区免费视频在线观看| 亚洲精品动漫人成3d在线| 亚洲AV无码国产剧情| 国产成人免费永久播放视频平台| 亚洲色大成网站www| 黄网址在线永久免费观看| 国产精品亚洲专区一区| 一级毛片直播亚洲| 中文在线观看国语高清免费| 亚洲欧洲日产国码av系列天堂| a在线观看免费网址大全| 亚洲国产一区二区a毛片| www视频在线观看免费| ASS亚洲熟妇毛茸茸PICS| 免费黄色app网站| 精品女同一区二区三区免费播放 | 日本不卡视频免费| 午夜亚洲WWW湿好爽| 国产精品亚洲αv天堂无码| 成全在线观看免费观看大全| 亚洲白色白色永久观看| 在线精品免费视频| 亚洲免费在线观看| 亚洲精品不卡视频| 成人免费视频国产| 本免费AV无码专区一区| 亚洲毛片一级带毛片基地| 永久免费看bbb| 中文字幕在线免费视频| 亚洲成综合人影院在院播放| 午夜视频在线观看免费完整版| 亚洲黄片手机免费观看| 亚洲自偷自偷精品| 免费观看亚洲人成网站| 免费A级毛片无码视频| 久久精品国产亚洲av天美18| 久久精品夜色国产亚洲av|