Java 双向链表
来源:百度知道 编辑:UC知道 时间:2024/06/21 23:30:56
public class DoubleLinkList {
Object data;
DoubleLinkList prev; //前驱引用
DoubleLinkList next; //后继引用
public DoubleLinkList() {
}
public DoubleLinkList(Object obj) {
data = obj;
}
public DoubleLinkList(Object obj, DoubleLinkList prevlink,
DoubleLinkList nextlink) {
data = obj;
prev = prevlink;
next = nextlink;
}
public void insert(Object val, DoubleLinkList prior) {
DoubleLinkList ins = new DoubleLinkList(val);
if (prior == null) {
ins.next = head;
head = ins;
if (tail == null) {
tail = ins;
} else {
ins.next.prev = ins;
}
} else {
ins.next = prior.next;
prior.next =
实现什么东西?什么要求都不说,这样问问题...
import java.util.*;
public class DoublyLinkedList {
//overview: a list that is doublylinked
private ListNode firstNode;
private ListNode lastNode;
//constructors:
public DoublyLinkedList(){
//EFFECTS: initialize this to be empty
firstNode = null;
lastNode = null;
}
//methods:
public synchronized void insertAtFront(Object o){
//EFFECTS: add o to the front of this
//MODIFIES: this
if(isEmpty()){
firstNode = lastNode = firstNode.nextNode = firstNode.prevNode = lastNode.nextNode = lastNode.prevNode = new ListNode(o);
}
else{
firstNode = new ListNode(o , firstNode , lastNode);
lastNode.nextNode = firstNode;
}
}
public synchronized void insertAtBack(Object o){
//EFFECTS: add o to the back of this
//MODIFIES: this
if(isEmpty()){
firstN