日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA实现简单链表操作

發布時間:2025/7/14 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA实现简单链表操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近拾起數據結構和算法,特此開博,記錄一下,希望堅持下去

Java語言中的對象引用實際上是一個指針,所以我們可以編寫這樣的類來實現鏈表中的結點。

class Node { Object tData; Node next;//指向下一個結點 }

將數據域定義成Object類是因為Object類是廣義超類,任何類對象都可以給其賦值,增加了代碼的通用性。為了使鏈表可以被訪問還需要定義一個表頭,表頭必須包含指向第一個結點的指針和指向當前結點的指針。

寫了個簡單例子,實現了單鏈表的一些簡單功能

public class SingleLinkList {//節點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;}//顯示所有節點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; } //將單鏈表反轉,循環 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; } //將單鏈表反轉,遞歸 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; } //插入頭節點public void insertFirstList(Object obj) {Node node = new Node(obj);node.next = first;first = node;}//刪除頭節點,返回一個頭節點public Node deleteFirstNode() throws Exception {if(first == null) throw new Exception("NULL"); Node tempNode = first;first = tempNode.next;return tempNode;}//指定位置增加節點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;}//根據data查找節點信息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; } //根據位置查找節點信息public Object find(int index) throws Exception{ int pos = 0; Node cur = first; while(pos != index){ cur = cur.next; pos++;} return cur; } //刪除指定節點信息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());;}}

?

和數組相比,鏈表的優勢在于長度沒有限制,并且在進行插入和刪除操作時,不需要移動數據項,效率上比數組要高很多

劣勢在于隨機訪問,無法像數組那樣直接通過下標找到特定的數據項

轉載于:https://www.cnblogs.com/aaron8f/p/6202850.html

總結

以上是生活随笔為你收集整理的JAVA实现简单链表操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。