在Java中如何實現雙向連結串列

2021-04-21 01:33:41 字數 1528 閱讀 4993

1樓:匿名使用者

雙向連結串列:就是有雙向指標,即雙向的鏈域。

鏈結點的結構:

│ data │ next │ previous │

└────┴────┴────────┘

雙向連結串列不必是雙端連結串列(持有對最後一個鏈結點的引用),雙端連結串列插入時是雙向的。

有兩條鏈:一條從頭到尾,一條從尾到頭,刪除遍歷時也是雙向的。

/*** 雙向連結串列

*/public class doublylinkedlist

public t peekhead()

return null;

}public boolean isempty()

public void insertfirst(t data) else

newlink.next = head; //新結點的下結點舊頭結點

head = newlink; //賦值後,頭結點的下結點是舊頭結點,上結點null

}public void insertlast(t data) else

newlink.previous = rear;

rear = newlink; //賦值後,尾結點的上結點是舊尾結點,下結點null

}public t deletehead() else

return temp.data;

}public t deleterear() else

return temp.data;

}public t find(t t)

linkfind = head;

while (find != null) else

}if (find == null)

return find.data;

}public t delete(t t)

linkcurrent = head;

while (!current.data.equals(t))

}if (current == head)

} else if (current == rear)

} else

return current.data;

}public boolean insertafter(t key, t data)

linkcurrent = head;

while (!current.data.equals(key))

}linknewlink = new link(data);

if (current == rear) else

current.next = newlink;

newlink.previous = current;

return true;

}public void displaylist4head()

}public void displaylist4rear()

}class link

void displaylink()

}public static void main(string args)}

c語言實現的雙向連結串列插入程式,C語言實現的雙向連結串列插入程式

雙向連結串列 include include typedef struct node node node create list int j node head,p1,p2 p2 head node malloc sizeof node head i a 0 head next head prior...

在Java介面A的實現類B中如何呼叫A中的變數

1.num有歧義 不表示哪一個num 2.會3.介面b的實現類c吧 因為介面中的變數都是public static 的,用b.num試試 1 介面中你寫實現在繼承類?繼承下來當然報錯 the field num is ambiguous 2 介面中的變數 1 會得到但是前提是隻繼承b不能中間加個a ...

關於java實現連結串列的問題,求高手解惑啊

while p null 解釋下你為什麼錯了 q p q.next rev.header rev.header q p p.next 第一步 q p 這兩個同時指向一個node。第二步 q.next rev.header 將當前node的next指向新連結串列的頭。但是,此時p的next也指向了這個...