java資料結構 單鏈表實現

2021-08-05 21:56:56 字數 1343 閱讀 4320

節點類:

package linkedlist;

public

class node

}

單鏈錶類:

package linkedlist;

/** * 鍊錶

* * @author administrator

* @param

* */

public class

linkedlist

else

curr.next = node;

length++;

return node;}}

/*** 插入節點,在索引index之前

* @param data

* 節點資料

* @param index

* 索引

* @return

* 插入的節點

*/public nodeaddnode(t data, int index)

if (index >= length)

while (pos != index - 1)

node.next = curr.next;

curr.next = node;

length++;

return node;

}/**

* 刪除索引處的節點

* @param index

* 索引

* @return

* 被刪除的節點

*/public nodedeletenode(int index)

if(index == 0)

int pos = 0;

nodepioneer = head;

while(pos != index - 1)

nodecurr = pioneer.next;

pioneer.next = curr.next;

length--;

return curr;

}/**

* 獲取索引處的節點

* * @param index

* @return

*/public nodegetnode(int index)

nodecurr = head;

int pos = 0;

while (pos != index)

return curr;

}/**

* 獲取所有節點

* @return

*/public node<?> getallnode()

return node;

}}

資料結構 單鏈表的java實現

單鏈表實現鍊錶的列印及元素刪除操作,鍊錶的實現主要是next屬性的定義,將一堆節點關聯起來的。實現簡單的鍊錶如下 public class linknode public linknode getnext public void setnext linknode next public int ge...

資料結構 單鏈表的java實現

單向鍊錶是一種線性表,實際上是由節點 node 組成的,乙個鍊錶擁有不定數量的節點。其資料在記憶體中儲存是不連續的,它儲存的資料分散在記憶體中,每個結點只能也只有它能知道下乙個結點的儲存位置。由n各節點 node 組成單向鍊錶,每乙個node記錄本node的資料及下乙個node。向外暴露的只有乙個頭...

Java資料結構 單鏈表

鍊錶是一種資料結構,和陣列同級。鍊錶在進行迴圈遍歷時效率不高,但是插入和刪除時優勢明顯。單鏈表結構 單鏈表就相當於從頭結點開始,每乙個節點只要記錄下一節點,就把所有資料串了起來,形成了乙個單向鍊錶。各個節點的儲存可以是分散的。頭插法 尾插法 entry entry new entry val 建立要...