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

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

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

    中文JAVA技術平等自由協作創造

    Java專題文章博客和開源

    常用鏈接

    統計

    最新評論

    數據結構之雙向鏈表的Java實現

      單鏈表只能從前往后遍歷,如果鏈表的長度較大,遍歷到鏈表后半部分的時候想要往前查找,就只能回到開頭,重新遍歷了。

      雙向鏈表提供了這個能力,即允許前向遍歷,也允許后向遍歷整個鏈表。原因是雙向鏈表的每個節點都有兩個指向其他節點的引用。但這也是其缺點,因為在插入、刪除的時候需要處理四個鏈接點的引用, 占用的空間也大了一些。如將頭節點和尾節點鏈接起來,即成為雙向循環鏈表托福答案 www.jamo123.com

      下面是java代碼:

      package test;

      public class DoubleLink {

      public Link first;

      public Link last;

      public DoubleLink() {// 構造器,初始化

      this.first = null;

      this.last = null;

      }

      public boolean isEmpty() {// 判斷是否為空

      return first == null;

      }

      public void insertFirst(int idata) {// 將元素插入鏈表開頭

      Link link = new Link(idata);

      if (isEmpty())

      last = link;// 如果為空,last需要改變

      else

      first.previous = link;// 非空,則要在first前插入

      link.next = first;

      first = link;

      }

      public void insertLast(int idata) {// 插入鏈表結尾

      Link link = new Link(idata);

      if (isEmpty())

      first = link;

      else

      last.next = link;

      link.previous = last;

      last = link;

      }

      public boolean insertAfter(int key, int idata) {// 在某項元素后插入

      Link current = first;

      while (current.idata != key) {//從頭開始查找

      current = current.next;

      if (current == null)//到表尾也沒有找到

      return false;

      }

      Link link = new Link(idata);

      if (current == last) {

      link.next = null;

      last = link;

      } else {

      link.next = current.next;

      current.next.previous = link;

      }

      link.previous = current;

      current.next = link;

      return true;

      }

      public Link delectKey(int key) {// 刪除某項元素

      Link current = first;

      while (current.idata != key) {

      current = current.next;

      if (current == null)

      return null;

      }

      if (current == first)

      first = current.next;

      else

      current.previous.next = current.next;

      if (current == last)

      last = current.previous;

      else

      current.next.previous = current.previous;

      return current;

      }

      public Link delectFirst() {// 刪除鏈表開頭元素

      Link temp = first;

      if (first.next == null)// 只有一個元素

      last = null;

      else

      first.next.previous = null;//first節點的next字段引用的鏈節點的previous字段

      first = first.next;

      return temp;

      }

      public Link delectLast() {// 刪除鏈表最后的元素

      Link temp = last;

      if (first.next == null)

      first = null;

      else

      last.previous.next = null;

      last = last.previous;

      return temp;

      }

      public void showFirst() {// 前向展示

      Link current = last;

      while (current != null) {

      current.showLink();

      current = current.previous;

      }

      }

      public void showLast() {// 后向展示

      Link current = first;

      while (current != null) {

      current.showLink();

      current = current.next;

      }

      }

      public static void main(String[] args) {

      DoubleLink dlink = new DoubleLink();

      dlink.insertFirst(1);

      dlink.insertFirst(2);

      dlink.insertFirst(3);

      dlink.showFirst();

      dlink.insertLast(4);

      dlink.insertLast(5);

      dlink.showFirst();

      }

      }

      class Link {

      public int idata;// 存放的數據

      public Link previous;// 對前一項的引用

      public Link next;// 對后一項的引用

      public Link(int idata) {

      this.idata = idata;

      }

      public void showLink() {

      System.out.print(idata + " ");

      }

      }
     

    posted on 2014-03-11 16:35 好不容易 閱讀(161) 評論(0)  編輯  收藏


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


    網站導航:
     
    PK10開獎 PK10開獎
    主站蜘蛛池模板: 亚洲成网777777国产精品| 白白国产永久免费视频| 亚洲精品国产品国语在线| 一级特黄aaa大片免费看| 免费国产a国产片高清网站| 亚洲中文字幕久久久一区| 一二三四在线观看免费高清中文在线观看| 亚洲视频在线观看网址| 亚洲免费在线视频播放| 亚洲天堂2016| 国产成人免费全部网站| 无码天堂va亚洲va在线va| 免费人成网站7777视频| 一级成人毛片免费观看| 中文字幕亚洲天堂| 先锋影音资源片午夜在线观看视频免费播放 | 久艹视频在线免费观看| 亚洲av福利无码无一区二区 | 亚洲爆乳大丰满无码专区| 大陆一级毛片免费视频观看| 亚洲JIZZJIZZ妇女| 亚洲人成电影网站国产精品| 久久www免费人成精品香蕉| 亚洲高清视频在线观看| 免费看黄视频网站| 国产精品亚洲一区二区无码| 自拍偷自拍亚洲精品情侣| 四虎影视成人永久免费观看视频 | 亚洲乱码在线观看| 免费无码精品黄AV电影| 国产精品亚洲专区无码牛牛 | 亚洲天堂一区二区三区| 成人激情免费视频| 最好2018中文免费视频| 久久精品国产精品亚洲蜜月| 岛国av无码免费无禁网站| 黄桃AV无码免费一区二区三区| 亚洲网址在线观看| 免费中文字幕在线| 最好看的中文字幕2019免费| 国产成人人综合亚洲欧美丁香花 |