java實現鍊錶操作

2021-07-07 01:14:14 字數 2530 閱讀 7083

package com.fsc.mylinkedlist;

/** * 線性表操作介面

* @author fsc

* * @param */

public inte***ce listinte***ce

為了插入和刪除**的一致性,引入了哨兵節點(也就是下面**中出現的firstnode)。

下標的選擇和陣列的規則相同,都是從0開始。為了滿足下標的規則,在**中相應作出了調整,下面是**

package com.fsc.mylinkedlist;

public class llistimplements listinte***ce

/*** 將鍊錶置為初始狀態

*/public final void clear()

/*** 向鍊錶尾部插入資料域

*/@override

public boolean add(t newentry)

/*** 向鍊錶指定位置插入資料 position為0時代表向煉表頭插入元素(第乙個元素)

* * @param newentry

* @param position

* @return

*/public boolean add(t newentry, int position)

if (position < 0 || position > length)

node nextnode = new node(newentry);

//向表尾插入的特殊情況處理表尾引用

if(position == length)else

}else

length++;

return true; }

/*** 刪除指定位置的節點 傳入0代表刪除表頭節點

* 鍊錶是從0開始計數

*/@override

public t remove(int givenposition)

node currentnode = getnodeat(givenposition);

node result = currentnode.next;

currentnode.next = currentnode.next.next;

return result.data; }

@override

public t replace(t newentry, int givenposition)

if (givenposition < 0 || givenposition >= length)

//要替換節點之前的節點

node currentnode = getnodeat(givenposition);

t result = currentnode.next.data;

currentnode.next.data = newentry;

return result; }

@override

public t getentry(int givenposition)

node currentnode = getnodeat(givenposition);

return currentnode.next.data;

} @override

public boolean contains(t anentry)

boolean result = false;

node currentnode = firstnode.next;

while(currentnode != null)

currentnode = currentnode.next;

} return result;

} /**

* 從鍊錶的頭節點開始列印鍊錶的資料域

*/@override

public void display()

} system.out.println(sb.tostring()); }

@override

public int getlength()

@override

public boolean isempty()

/*** 獲取指定位置的節點 節點的計數從0開始,getnodeat(0)代表獲取firstnode(哨兵節點)

* * @param givenposition

* @return 若要找的位置不存在節點返回null

*/private node getnodeat(int givenposition)

currentnode = currentnode.next;

} return null;

} /**

* 節點類

* * @author fsc

* */

private class node

public node(t data, node next)

}}

鍊錶 java實現

package com.shine.linearlist 單鏈表結點類 public class node public node package com.shine.linearlist 線性表介面 2015 01 19 qizhang public inte ce llist package c...

鍊錶 java實現雙向鍊錶

前面已經總結了單向鍊錶,有興趣的兄弟可以進我部落格看一下。大家對比就可以看出,實現同樣的功能單向鍊錶要比雙向鍊錶痛苦的多。所以呀不斷地總結前輩留下的東西,是社會進步的基礎呀。可以直接看linkedlist的原始碼,其就是個雙向鍊錶。一 雙向鍊錶的結構。1 首先節點的結構,其中包含本節點內容,同時需要...

Java實現雙向鍊錶的基本操作

雙向鍊錶即表中任一結點都是由data資料域,上乙個結點引用域lastnode,下乙個結點引用域nextnode組成。雙向鍊錶結點類 author liangxiamoyi param public class dlnode 構造方法 param item 資料域 param last 上乙個結點 p...