<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)  編輯  收藏

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


    網站導航:
     
    主站蜘蛛池模板: 日韩va亚洲va欧洲va国产| 免费国产成人高清视频网站| 久久精品国产99精品国产亚洲性色| 四虎影院永久免费观看| 亚洲中文字幕久久精品无码喷水| 高h视频在线免费观看| 亚洲欧洲精品成人久久曰影片| 高潮毛片无遮挡高清免费视频| 亚洲精品无码专区2| 中国一级特黄的片子免费| 亚洲欧洲日产国码av系列天堂| 午夜在线a亚洲v天堂网2019| 24小时日本在线www免费的| 亚洲人妻av伦理| 亚洲一区二区三区写真| 我想看一级毛片免费的| 精品亚洲成a人在线观看| 亚色九九九全国免费视频| 亚洲人成无码网站| 久久久久国产精品免费网站| 亚洲精品中文字幕麻豆| 久久精品成人免费网站| 亚洲第一区在线观看| 在线观看人成视频免费无遮挡| 国产在线观看免费不卡| 亚洲欧洲日韩极速播放| 国产成人啪精品视频免费网| xxxxx做受大片视频免费| 四虎永久免费影院| 在线观看黄片免费入口不卡| 亚洲精品日韩专区silk| 午夜dj在线观看免费视频| 亚洲精品黄色视频在线观看免费资源| 亚洲成a人片在线观看中文动漫| 天天影院成人免费观看| 久久亚洲春色中文字幕久久久| 免费无码黄十八禁网站在线观看| 免费精品国自产拍在线播放| 亚洲综合一区二区精品导航| 免费特级黄毛片在线成人观看| 两个人看的www视频免费完整版|