java實(shí)現(xiàn)單鏈表增刪改查的實(shí)例代碼詳解
更新時間:2019年08月30日 08:13:06 作者:曾聰聰
在本篇文章里小編給大家整理了關(guān)于java實(shí)現(xiàn)單鏈表增刪改查的實(shí)例內(nèi)容,需要的朋友們可以參考下。
package 數(shù)據(jù)結(jié)構(gòu)算法.鏈表; /* *定義節(jié)點(diǎn) * 鏈表由節(jié)點(diǎn)構(gòu)成 */ public class Node<E> { private E e; //數(shù)據(jù)data private Node<E> next; //指向下一個節(jié)點(diǎn) public Node() { } public Node(E e) { this.e = e; } public Node<E> getNext() { return next; } public void setNext(Node<E> next) { this.next = next; } public E getE() { return e; } public void setE(E e) { this.e = e; } }
package 數(shù)據(jù)結(jié)構(gòu)算法.鏈表; /* * 定義實(shí)現(xiàn)類MyLinkedList * 實(shí)現(xiàn)鏈表的基本功能:增刪改查 */ public class MyLinkedList<E> { //聲明頭節(jié)點(diǎn)尾節(jié)點(diǎn) private Node<E> head; private Node<E> last; //鏈表的大小 private int size; private int modcount; //計算被修改的次數(shù) public MyLinkedList() { head = new Node<E>();//實(shí)例化頭結(jié)點(diǎn) last = head; } /* *返回單鏈表中存儲的元素總數(shù) */ public int size() { return size; } /* *獲取指定索引位置的節(jié)點(diǎn)對象 */ public Node<E> get(int index) { if (index < 0 || index > size - 1) return null; Node<E> node = head.getNext();//將頭結(jié)點(diǎn)的下一個節(jié)點(diǎn)賦給Node for (int i = 0; i < index; i++) { node = node.getNext();//獲取node的下一個節(jié)點(diǎn) } return node; } /* *獲取指定索引位置的數(shù)據(jù) */ public E getValue(int index) { if (index < 0 || index > size - 1) return null; Node<E> node = get(index); return node.getE(); } /* *增加元素 */ public void add(E e) { Node<E> node = new Node<E>(e); //以e實(shí)例化一個節(jié)點(diǎn) last.setNext(node);//往尾節(jié)點(diǎn)后追加節(jié)點(diǎn) last = node;//該節(jié)點(diǎn)設(shè)為最后一個節(jié)點(diǎn) size++; modcount++; } /* *指定位置插入元素,返回插入的節(jié)點(diǎn)數(shù)據(jù) */ public E add(int index, E e) { if (index < 0 || index > size - 1) return null; Node<E> node = new Node<E>(e); //實(shí)例化一個節(jié)點(diǎn) //找到插入的原節(jié)點(diǎn) Node<E> oldNode = get(index); if (index == 0) {//當(dāng)索引為0時 head.setNext(node); } else { //找到插入節(jié)點(diǎn)的上一個 Node<E> bNode = get(index - 1); bNode.setNext(node); } node.setNext(oldNode); size++; modcount++; return oldNode.getE(); } /* *刪除指定的節(jié)點(diǎn)e,并返回刪除節(jié)點(diǎn)的數(shù)據(jù) */ public E delete(int index) { if (index < 0 || index > size - 1) return null; if (index == 0) {//當(dāng)索引為1,令頭結(jié)點(diǎn)的下一個節(jié)點(diǎn)為頭結(jié)點(diǎn) Node<E> node = head.getNext(); head.setNext(node.getNext()); } //獲取要刪除節(jié)點(diǎn)的前一個節(jié)點(diǎn) Node<E> bNode = get(index - 1); //獲取要刪除的節(jié)點(diǎn) Node<E> Node = bNode.getNext(); //獲取要刪除節(jié)點(diǎn)的下一個節(jié)點(diǎn) Node<E> nNode = Node.getNext(); //刪除該節(jié)點(diǎn) bNode.setNext(nNode); //清除Node的下一個節(jié)點(diǎn) Node.setNext(null); size--; modcount++; return Node.getE();//返回節(jié)點(diǎn)中的數(shù)據(jù)域 } /* *修改指定位置的數(shù)據(jù)域并返回修改后的數(shù)據(jù) */ public E set(int index, E e) { if (index < 0 || index > size - 1) return null; //獲取指定位置的原節(jié)點(diǎn) Node<E> node = get(index); node.setE(e); modcount++; return node.getE(); } }
package 數(shù)據(jù)結(jié)構(gòu)算法.鏈表; /* *定義測試類 */ public class MyLinkedListTest { public static void main(String[] args) { MyLinkedList<String> list = new MyLinkedList<>(); //測試add list.add("one"); list.add("two"); list.add("three"); list.add("four"); list.add(0,"newone"); list.add(1,"newtwo"); for (int i = 0; i < list.size(); i++) { System.out.print(list.getValue(i)+" "); } //測試set System.out.println(); list.set(0, "111"); list.set(1, "222"); System.out.println(list.getValue(0) + " " + list.getValue(1)); //測試delete System.out.println(); list.delete(1); for (int i = 0; i < list.size(); i++) { System.out.print(list.getValue(i)+" "); } } }
運(yùn)行結(jié)果如下:
以上就是全部知識點(diǎn)內(nèi)容,感謝大家對腳本之家的支持。
相關(guān)文章
javascript檢測對象中是否存在某個屬性判斷方法小結(jié)
檢測對象中屬性的存在與否可以通過以下幾種方法來判斷:使用in關(guān)鍵字、使用對象的hasOwnProperty()方法、用undefined判斷、在條件語句中直接判斷,感興趣的朋友可以了解下哈2013-05-05javascript實(shí)現(xiàn)可拖動變色并關(guān)閉層窗口實(shí)例
這篇文章主要介紹了javascript實(shí)現(xiàn)可拖動變色并關(guān)閉層窗口的方法,涉及javascript操作層的樣式與屬性的相關(guān)技巧,需要的朋友可以參考下2015-05-05完美解決手機(jī)瀏覽器頂部下拉出現(xiàn)網(wǎng)頁源或刷新的問題
下面小編就為大家分享一篇完美解決手機(jī)瀏覽器頂部下拉出現(xiàn)網(wǎng)頁源或刷新的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-11-11js判斷手機(jī)端(Android手機(jī)還是iPhone手機(jī))
現(xiàn)在使用手機(jī)上網(wǎng)的人越來越多,一些下載網(wǎng)站會通過判斷不同系統(tǒng)手機(jī)來訪問不同網(wǎng)頁,比如iPhone和Android。下面我們就來介紹一下如何用javascript判斷iPhone或Android手機(jī)訪問2015-07-07JS實(shí)現(xiàn)復(fù)制內(nèi)容到剪貼板功能
本文主要介紹了JS實(shí)現(xiàn)復(fù)制內(nèi)容到剪貼板功能的步驟方法,可兼容所有PC瀏覽器,不兼容手機(jī)端。具有一定的參考價值,下面跟著小編一起來看下吧2017-02-02