中文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 好不容易 閱讀(167) 評論(0)  編輯  收藏


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


          網站導航:
           
          PK10開獎 PK10開獎
          主站蜘蛛池模板: 遂溪县| 图们市| 类乌齐县| 望城县| 三门峡市| 新宾| 江山市| 会理县| 沧源| 家居| 偏关县| 金华市| 辛集市| 定襄县| 贡觉县| 勐海县| 徐闻县| 固阳县| 贵南县| 云南省| 灌云县| 库车县| 沅江市| 阜新市| 龙陵县| 乡宁县| 江川县| 石首市| 石家庄市| 五华县| 苗栗县| 凤阳县| 通辽市| 怀远县| 青神县| 普格县| 隆林| 赞皇县| 商都县| 泰顺县| 宿州市|