鍊錶的實現

2021-08-30 03:22:24 字數 2370 閱讀 2630

/**  

*

*/

package link;

/**

* @author sunxboy

* */

public class node

}

/**  

* 資料結構之鍊錶

*/

package link;

/**

* @author sunxboy

* */

public class linktable

//將新插入的數的位址指向原來的第一位數

node.next = head;

//更新插入值後的新鍊錶的頭

head = node;

return true;

}

/**

* 在鍊錶的最末插入乙個值

* @param data 要插入的資料

* @return 是否成功

*/

public boolean insertlast(int data)

node p = head;

node pre = head;

//遍歷整個鍊錶,從而最終得到乙個最後的節點

while(!isend(p))

// 將要插入的值插入到最後乙個節點上

pre.next = node;

return false;

}

/**

* 在某節點前插入一新資料

* @param olddata 原節點

* @param newdata 新節點

* @return 是否插入成功

*/

public boolean insert(int olddata,int newdata)

node newnode = new node(newdata);

if(prenode==head) else

return true;

}

/**

* 刪除某一節點

* @param data 節點資料

* @return 是否刪除成功

*/

public boolean delete(int data)

node prenode = find(data, true);

if(prenode == head) else

return true;

}

/**

* 將某節點資料更新為新的資料

* @param olddata

* @param newdata

* @return

*/

public boolean update(int olddata,int newdata)

return false;

}

/**

* 查詢數值為data的節點

* @param flag 為false時表示返回要找資料的節點,

* 為true時表示返回要找資料之前的節點

* @param data 要查詢的數值

* @return

*/

public node find(int data,boolean flag)

if(isend(p))

if(flag) return pre;

else return p;

}

/**

* 鍊錶是否為空

* @return

*/

public boolean isempty()

/**

* 此節點是否是最後乙個節點

* @param node 要判斷的節點

* @return 是否是最後乙個節點

*/

public boolean isend(node node)

/**

* 顯示鍊錶所有節點資訊

* */

public void display()

}

public static void main(string args)

}

鍊錶的實現

鍊錶是一種非常重要的資料結構,比起陣列來雖然操作繁瑣,查詢效率也不如陣列效率高,但在進行插入刪除操作時,鍊錶具有陣列無法比擬的效率,下面的 是鍊錶的實現 include include include define n 100 typedef struct node link link node i...

鍊錶的實現

include using namespace std template class linklist node head public linklist t a,int n 0 利用尾插法來構建線性鍊錶 linklist bool isempty 不為空,則返回0,為空則返回非0 t getnod...

鍊錶的實現

記憶體結構 鍊錶也是資料結構的一種,但是和陣列不一樣,陣列在記憶體中每個節點的位置是相連的。而鍊錶的每個節點在物件中是分散的,依靠引用相連。優點1 單鏈表在增加和刪除上要比陣列結構更加快捷。原因 因為順序表在記憶體中是相連的,所以刪除乙個節點,在該節點之後的節點都要隨之前移,所以效率不高。而單鏈表使...