JAVA实现简单链表操作
生活随笔
收集整理的這篇文章主要介紹了
JAVA实现简单链表操作
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
最近拾起數(shù)據(jù)結(jié)構(gòu)和算法,特此開博,記錄一下,希望堅(jiān)持下去
Java語言中的對象引用實(shí)際上是一個(gè)指針,所以我們可以編寫這樣的類來實(shí)現(xiàn)鏈表中的結(jié)點(diǎn)。
class Node { Object tData; Node next;//指向下一個(gè)結(jié)點(diǎn) }將數(shù)據(jù)域定義成Object類是因?yàn)镺bject類是廣義超類,任何類對象都可以給其賦值,增加了代碼的通用性。為了使鏈表可以被訪問還需要定義一個(gè)表頭,表頭必須包含指向第一個(gè)結(jié)點(diǎn)的指針和指向當(dāng)前結(jié)點(diǎn)的指針。
寫了個(gè)簡單例子,實(shí)現(xiàn)了單鏈表的一些簡單功能
public class SingleLinkList {//節(jié)點(diǎn)class Node {public Object tData;public Node next;Node(Object data){this.tData = data;}public void display() { System. out.print( tData + " "); } }public Node first;public int nodeIndex;SingleLinkList() {this.first = null;}//顯示所有節(jié)點(diǎn)public void display(){ if(first == null) System.out.println("NULL"); Node cur = first; while(cur != null){ System.out.print(cur.tData.toString() + " -> "); cur = cur.next; } System.out.print("\n"); } //鏈表長度public int getListLength(){ int len=0; Node cur = first;while(cur!=null){ len++; cur=cur.next; } return len; } //將單鏈表反轉(zhuǎn),循環(huán) public Node reverseList(){ Node cur = first;if(cur==null||cur.next==null) return cur; Node pre=null; Node nex=null; while(cur!=null){ nex=cur.next; cur.next=pre; pre=cur; cur=nex; } return pre; } //將單鏈表反轉(zhuǎn),遞歸 public Node reverseListRec(){ Node cur = first;if(cur==null||cur.next==null)return cur; Node reHead=reverseListRec(); cur.next.next=cur; cur.next=null; return reHead; } //插入頭節(jié)點(diǎn)public void insertFirstList(Object obj) {Node node = new Node(obj);node.next = first;first = node;}//刪除頭節(jié)點(diǎn),返回一個(gè)頭節(jié)點(diǎn)public Node deleteFirstNode() throws Exception {if(first == null) throw new Exception("NULL"); Node tempNode = first;first = tempNode.next;return tempNode;}//指定位置增加節(jié)點(diǎn)public void add(int index, Object data) {int pos = 0;Node node = new Node(data);Node current = first;Node previous = first;while (pos != index) {previous = current;current = current.next;pos++;}node.next = current;previous.next = node;}//根據(jù)data查找節(jié)點(diǎn)信息public Object find(Object obj) throws Exception{ if(first == null) throw new Exception("LinkedList is empty!"); Node cur = first; while(cur != null){ if(cur.tData == obj){ return cur.tData; } cur = cur.next; } return null; } //根據(jù)位置查找節(jié)點(diǎn)信息public Object find(int index) throws Exception{ int pos = 0; Node cur = first; while(pos != index){ cur = cur.next; pos++;} return cur; } //刪除指定節(jié)點(diǎn)信息public Node deleteNode(int index) throws Exception {int pos =0;Node cur = first;Node pre = first;while(pos != index){pre = cur;cur = cur.next;pos++;}if(cur == first) { first = first. next; } else { pre.next = cur.next; } return cur;}public static void main(String[] args) throws Exception {// TODO Auto-generated method stubSingleLinkList sl = new SingleLinkList();sl.insertFirstList(111);sl.insertFirstList(222); sl.insertFirstList(333); sl.display(); sl.deleteFirstNode();sl.display();sl.insertFirstList(444);sl.add(2, 888);sl.display();sl.deleteNode(2);sl.display();Node d = (Node) sl.find(2);System.out.println(d.tData.toString());;}}?
和數(shù)組相比,鏈表的優(yōu)勢在于長度沒有限制,并且在進(jìn)行插入和刪除操作時(shí),不需要移動數(shù)據(jù)項(xiàng),效率上比數(shù)組要高很多
劣勢在于隨機(jī)訪問,無法像數(shù)組那樣直接通過下標(biāo)找到特定的數(shù)據(jù)項(xiàng)
轉(zhuǎn)載于:https://www.cnblogs.com/aaron8f/p/6202850.html
總結(jié)
以上是生活随笔為你收集整理的JAVA实现简单链表操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: git 记住密码
- 下一篇: sublime必备插件