java 靜態(tài)鏈表實現(xiàn)示例詳解
更新時間:2023年06月02日 14:18:33 作者:雨翔河
這篇文章主要為大家介紹了java 靜態(tài)鏈表實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
正文
試著用 java 來實現(xiàn)下鏈表,因為 java 沒有指針概念的緣故,用數(shù)組的下標來代替指針,這樣一個靜態(tài)鏈表就出來了。
靜態(tài)雙向不循環(huán)鏈表
public class Test {
public static void main(String[] args) {
L l = new L();
l.init();
l.insert(123);
l.insert(456);
l.insert(789);
l.insert(222);
int removeIndex = l.insert(333);
l.show();
l.removeNode(removeIndex);
l.show();
System.exit(0);
}
}
/**
* 靜態(tài)鏈表
*/
class L {
private static final int MAX = 100; //鏈表的最大容量
private Node node[] = new Node[MAX];
private int head; //頭指針,頭指針區(qū)域可存儲數(shù)據(jù)也可以不存儲數(shù)據(jù),只用來做向導,我這里是存儲了數(shù)據(jù).
class Node {
public int next = -1; //指針指向的后一個節(jié)點
public int pre = -1; //指針指向的前一個節(jié)點
public long value = -1; //節(jié)點的值
}
/**
* 初始化鏈表空間,其實這個可以在實例化類的時候完成的.
*/
public void init() {
head = 0;
for (int i = 0; i < MAX; i++) {
node[i] = new Node();
node[i].next = -1; //為了簡單的實現(xiàn)下,假設空值為-1
node[i].pre = -1;
node[i].value = -1;
}
}
/**
* 分配節(jié)點空間,類似于c里的malloc
*
* @return int
*/
public int malloc() {
for (int i = 0; i < MAX; i++) {
if (node[i].pre < 0 && node[i].next < 0 && node[i].value < 0) {
return i;
}
}
System.out.println("malloc fail ,full");
return -1;
}
/**
* 移除節(jié)點
*
* @param indexNode indexNode
*/
public void removeNode(int indexNode) {
if (indexNode < 0) {
System.out.println("removeNode indexNode is error");
return;
}
int preNode = node[indexNode].pre;
int nextNode = node[indexNode].next;
node[indexNode].pre = -1;
node[indexNode].next = -1;
node[indexNode].value = -1;
if (nextNode >= 0) {
node[nextNode].pre = preNode;
}
//頭節(jié)點被移除
if (preNode < 0) {
head = nextNode;
} else {
node[preNode].next = nextNode;
}
}
/**
* 插入節(jié)點
*
* @param v v
*/
public int insert(long v) {
int index = head;
while (node[index].next >= 0) {
index = node[index].next;
}
int insertNodeIndex = malloc();
if (insertNodeIndex < 0) {
System.out.println("malloc error,please check malloc function.");
return -1;
}
node[insertNodeIndex].value = v;
if (insertNodeIndex == head) {
node[insertNodeIndex].pre = -1;
} else {
node[index].next = insertNodeIndex;
node[insertNodeIndex].pre = index;
}
return insertNodeIndex;
}
/**
* 測試下顯示這個鏈表
*/
public void show() {
int index = head;
System.out.println("show l:-------------------------");
while (node[index].next >= 0) {
System.out.println(node[index].value);
index = node[index].next;
}
System.out.println(node[index].value);
System.out.println("show l end:-----------------------");
System.out.println("test show l start:_______________");
while (node[index].pre >= 0) {
System.out.println(node[index].value);
index = node[index].pre;
}
System.out.println(node[index].value);
System.out.println("test show l end:_______________");
}
}以上就是java 靜態(tài)鏈表實現(xiàn)示例詳解的詳細內(nèi)容,更多關于java 靜態(tài)鏈表的資料請關注腳本之家其它相關文章!
相關文章
springboot掃描引入jar包的service等組件方式
這篇文章主要介紹了springboot掃描引入jar包的service等組件方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
解決mybatis-plus-boot-starter與mybatis-spring-boot-starter的錯誤問題
本文主要講述了在使用MyBatis和MyBatis-Plus時遇到的綁定異常問題,通過排查和總結,作者發(fā)現(xiàn)使用MyBatis-Plus?Boot?Starter可以解決這個問題,文章詳細對比了MyBatis-Plus?Boot?Starter和MyBatis?Spring?Boot?Starter的功能和使用場景2025-01-01

