JAVA单向/双向链表的实现
生活随笔
收集整理的這篇文章主要介紹了
JAVA单向/双向链表的实现
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、JAVA單向鏈表的操作(增加節(jié)點(diǎn)、查找節(jié)點(diǎn)、刪除節(jié)點(diǎn))
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | class?Link {?// 鏈表類 ????class?Node {?// 保存每一個(gè)節(jié)點(diǎn),此處為了方便直接定義成內(nèi)部類 ????????private?String data;?// 節(jié)點(diǎn)的內(nèi)容 ????????private?Node next;?// 保存下一個(gè)節(jié)點(diǎn) ????????public?Node(String data) {?// 通過(guò)構(gòu)造方法設(shè)置節(jié)點(diǎn)內(nèi)容 ????????????this.data = data; ????????} ????????public?void?add(Node node) {?// 增加節(jié)點(diǎn) ????????????if?(this.next ==?null) {?// 如果下一個(gè)節(jié)點(diǎn)為空,則把新節(jié)點(diǎn)加入到next的位置上 ????????????????this.next = node; ????????????}?else?{?// 如果下一個(gè)節(jié)點(diǎn)不為空,則繼續(xù)找next ????????????????this.next.add(node); ????????????} ????????} ????????public?void?print() {?// 打印節(jié)點(diǎn) ????????????if?(this.next !=?null) { ????????????????System.out.print(this.data +?"-->"); ????????????????this.next.print(); ????????????}?else?{ ????????????????System.out.print(this.data +?"\n"); ????????????} ????????} ????????public?boolean?search(String data) {?// 內(nèi)部搜索節(jié)點(diǎn)的方法 ????????????if?(this.data.equals(data)) { ????????????????return?true; ????????????} ????????????if?(this.next !=?null) { ????????????????return?this.next.search(data); ????????????}?else?{ ????????????????return?false; ????????????} ????????} ????????public?void?delete(Node previous, String data) {?// 內(nèi)部刪除節(jié)點(diǎn)的方法 ????????????if?(this.data.equals(data)) { ????????????????previous.next =?this.next; ????????????}?else?{ ????????????????if?(this.next !=?null) { ????????????????????this.next.delete(this, data); ????????????????} ????????????} ????????} ????} ????private?Node root;?// 定義頭節(jié)點(diǎn) ????public?void?addNode(String data) {?// 根據(jù)內(nèi)容添加節(jié)點(diǎn) ????????Node newNode =?new?Node(data);?// 要插入的節(jié)點(diǎn) ????????if?(this.root ==?null) {?// 沒(méi)有頭節(jié)點(diǎn),則要插入的節(jié)點(diǎn)為頭節(jié)點(diǎn) ????????????this.root = newNode; ????????}?else?{?// 如果有頭節(jié)點(diǎn),則調(diào)用節(jié)點(diǎn)類的方法自動(dòng)增加 ????????????this.root.add(newNode); ????????} ????} ????public?void?print() {?// 展示列表的方法 ????????if?(root !=?null) {?// 當(dāng)鏈表存在節(jié)點(diǎn)的時(shí)候進(jìn)行展示 ????????????this.root.print(); ????????} ????} ????public?boolean?searchNode(String data) {?// 在鏈表中尋找指定內(nèi)容的節(jié)點(diǎn) ????????return?root.search(data);?// 調(diào)用內(nèi)部搜索節(jié)點(diǎn)的方法 ????} ????public?void?deleteNode(String data) {?// 在鏈表中刪除指定內(nèi)容的節(jié)點(diǎn) ????????if?(root.data.equals(data)) {?// 如果是頭節(jié)點(diǎn) ????????????if?(root.next !=?null) { ????????????????root = root.next; ????????????}?else?{ ????????????????root =?null; ????????????} ????????}?else?{ ????????????root.next.delete(this.root, data); ????????} ????} } |
測(cè)試:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public?class?TestMain { ????public?static?void?main(String[] args) { ????????Link l =?new?Link(); ????????l.addNode("A"); ????????? ????????l.addNode("B"); ????????l.addNode("C"); ????????l.addNode("D"); ????????System.out.println("原鏈表:"); ????????l.print(); ????????String searchNode =?"B"; ????????System.out.println("查找節(jié)點(diǎn):"?+ searchNode); ????????String result = l.searchNode(searchNode)?"找到!":"沒(méi)找到!"; ????????System.out.println("查找結(jié)果:"?+ result); ????????System.out.println("刪除節(jié)點(diǎn):"?+ searchNode); ????????l.deleteNode(searchNode); ????????System.out.println("刪除節(jié)點(diǎn)后的鏈表:"); ????????l.print(); ????} } |
測(cè)試結(jié)果如下:
| 1 2 3 4 5 6 7 | 原鏈表: A-->B-->C-->D 查找節(jié)點(diǎn):B 查找結(jié)果:找到! 刪除節(jié)點(diǎn):B 刪除節(jié)點(diǎn)后的鏈表: A-->C-->D |
原地址
?二、雙向鏈表的簡(jiǎn)單實(shí)現(xiàn)
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | public?class?DoubleLink<T> { ????/** ?????* Node<AnyType>類定義了雙向鏈表中節(jié)點(diǎn)的結(jié)構(gòu),它是一個(gè)私有類, 而其屬性和構(gòu)造函數(shù)都是公有的,這樣,其父類可以直接訪問(wèn)其屬性 ?????* 而外部類根本不知道Node類的存在。 ?????* ?????* @author ZHB ?????* ?????* @param <T> ?????*??????????? 類型 ?????* @param Data ?????*??????????? 是節(jié)點(diǎn)中的數(shù)據(jù) ?????* @param pre ?????*??????????? 指向前一個(gè)Node節(jié)點(diǎn) ?????* @param next ?????*??????????? 指向后一個(gè)Node節(jié)點(diǎn) ?????*/ ????private?class?Node<T> { ????????public?Node<T> pre; ????????public?Node<T> next; ????????public?T data; ????????public?Node(T data, Node<T> pre, Node<T> next) { ????????????this.data = data; ????????????this.pre = pre; ????????????this.next = next; ????????} ????????public?Node() { ????????????this.data =?null; ????????????this.pre =?null; ????????????this.next =?null; ????????} ????} ????// 下面是DoubleLinkedList類的數(shù)據(jù)成員和方法 ????private?int?theSize; ????private?Node<T> Header; ????private?Node<T> Tail; ????/* ?????* 構(gòu)造函數(shù) 我們構(gòu)造了一個(gè)帶有頭、尾節(jié)點(diǎn)的雙向鏈表 頭節(jié)點(diǎn)的Next指向尾節(jié)點(diǎn) 為節(jié)點(diǎn)的pre指向頭節(jié)點(diǎn) 鏈表長(zhǎng)度起始為0。 ?????*/ ????public?DoubleLink() { ????????theSize =?0; ????????Header =?new?Node<T>(null,?null,?null); ????????Tail =?new?Node<T>(null, Header,?null); ????????Header.next = Tail; ????} ????public?void?add(T item) { ????????Node<T> aNode =?new?Node<T>(item,?null,?null); ????????Tail.pre.next = aNode; ????????aNode.pre = Tail.pre; ????????aNode.next = Tail; ????????Tail.pre = aNode; ????????theSize++; ????} ????public?boolean?isEmpty() { ????????return?(this.theSize ==?0); ????} ????public?int?size() { ????????return?this.theSize; ????} ????public?T getInt(int?index) { ????????if?(index >?this.theSize -?1?|| index <?0) ????????????throw?new?IndexOutOfBoundsException(); ????????Node<T> current = Header.next; ????????for?(int?i =?0; i < index; i++) { ????????????current = current.next; ????????} ????????return?current.data; ????} ????public?void?print() { ????????Node<T> current = Header.next; ????????while?(current.next !=?null) { ????????????System.out.println(current.data.toString()); ????????????current = current.next; ????????} ????} ????public?static?void?main(String[] args) { ????????DoubleLink<String> dLink =?new?DoubleLink<String>(); ????????dLink.add("zhb"); ????????dLink.add("zzb"); ????????dLink.add("zmy"); ????????dLink.add("zzj"); ????????System.out.println("size : "?+ dLink.size()); ????????System.out.println("isEmpty? : "?+ dLink.isEmpty()); ????????System.out.println("3 : "?+ dLink.getInt(2)); ????????dLink.print(); ????} } |
原文地址
本文轉(zhuǎn)自Work Hard Work Smart博客園博客,原文鏈接:http://www.cnblogs.com/linlf03/p/5278629.html,如需轉(zhuǎn)載請(qǐng)自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的JAVA单向/双向链表的实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: H5 中的 new FileReader
- 下一篇: 第四届国际软件自由日在西安邮电学院的发言