//定義鍊錶的結點類
class node
//顯示結點
public void show()
}//定義鍊錶類及其操作方法
class linkedlist
//頭刪,並返回頭結點,返回之前的頭結點
public node deleteheadnode()
//在任意位置插入結點,在index後面插入
public void insert(int index,string data)
node.next = curnode;//!!!!!
previousnode.next = node;//!!!!!
location = 0; }
//查詢任意位置的結點
public node findbylocation(int index)
return curnode; }
//刪除任意位置的結點
public node deletebylocation(int index)
if(curnode == head)else
return curnode; }
//根據資料查詢結點資訊
public node findbydata(string data)
return curnode; }
//根據結點的data刪除結點(僅僅刪除第乙個)
public node deletebydata(string data)
previousnode = curnode;
curnode = curnode.next;
} if(curnode == head)else
return curnode; }
//顯示所有結點資訊
public void displayallnodes()
system.out.println();
} }public class link
}
執行結果:
c d e f b a
node:a
c d e f b
node:c
node:e
單鏈表反轉 java版
head a b c 變成 head c b a 我們可以用迴圈的方式去實現,遍歷一次鍊錶即可。1.用乙個臨時變數tmp儲存 a的下乙個元素b,a的next指向null,即 由頭變尾 head指向null。head null a b c tmp b 2.因為此時tmp就是b,所以將tmp指向tmp的...
單鏈表的逆序 Java版
示例 輸入 1 2 3 4 5 null 輸出 5 4 3 2 1 null 方法一 三個指標直接反轉法 思路 兩個指標用來反轉兩個節點,第三個指標用來儲存後續節點。definition for singly linked list.public class listnode class solut...
java單鏈表實現
class node 非空節點 public node object obj description 注 在這裡鍊錶預設都是帶有頭節點 資料域為空 version 1.0 author meify 2013 7 29 下午3 36 19 public class linklist 往單鏈表頭部插入節...