1.3.31实现一个嵌套类DoubleNode用来构造双向链表,其中每个结点都含有一个指向前驱元素的引用和一项指向后续元素引用(如果不存在则为null)。为以下任务实现若干静态方法:在表头插入结点、在表尾插入结点、从表头删除结点、从表尾删除结点、在指定结点之前插入新结点、在指定的结点之后插入新结点、删除指定结点。
public class Node<Item>
{
Item item;
Node prev;
Node next;
}
public class LinkedListOfDouble
{
public static void insertBeforeFirst(Node first,Node newNode)
{
newNode.prev=null;
newNode.next=first;
first.prev=newNode;
}
public static void insertAfterTail(Node first,Node newNode)
{
Node current=new Node();
current.next=first;
while(current.next!=null)
current=current.next;
current.next=newNode;
newNode.prev=current;
}
public static Node deleteFirst(Node first)
{
if(first==null) return null;
Node newFirst=first.next;
newFirst.prev=null;
first.next=null;
return newFirst;
}
public static Node deleteTail(Node first)
{
if(first==null) return null;
Node current= new Node();
current.next=first;
while(current.next!=null)
current=current.next;
current.prev.next=null;
current.prev=null;
return current;
}
public static void insertBefore(Node current,Node newNode)
{
newNode.prev=current.prev;
newNode.next=current;
current.prev=newNode;
}
public static void insertAfter(Node current,Node newNode)
{
newNode.prev=current;
newNode.next=current.next;
current.next=newNode;
}
public static void delete(Node deleteNode)
{
if(deleteNode.prev==null && deleteNode.next!=null)
{
deleteNode.next.prev=null;
deleteNode.next=null;
}
else if(deleteNode.prev!=null && deleteNode.next==null)
{
deleteNode.prev.next=null;
deleteNode.prev=null;
}
else if(deleteNode.prev==null && deleteNode.next==null)
{
deleteNode.prev=null;
deleteNode.next=null;
}
else
{
deleteNode.prev.next=deleteNode.next;
deleteNode.next.prev=deleteNode.prev;
deleteNode.prev=null;
deleteNode.next=null;
}
}
}
public class Node<Item>
{
Item item;
Node prev;
Node next;
}
public class LinkedListOfDouble
{
public static void insertBeforeFirst(Node first,Node newNode)
{
newNode.prev=null;
newNode.next=first;
first.prev=newNode;
}
public static void insertAfterTail(Node first,Node newNode)
{
Node current=new Node();
current.next=first;
while(current.next!=null)
current=current.next;
current.next=newNode;
newNode.prev=current;
}
public static Node deleteFirst(Node first)
{
if(first==null) return null;
Node newFirst=first.next;
newFirst.prev=null;
first.next=null;
return newFirst;
}
public static Node deleteTail(Node first)
{
if(first==null) return null;
Node current= new Node();
current.next=first;
while(current.next!=null)
current=current.next;
current.prev.next=null;
current.prev=null;
return current;
}
public static void insertBefore(Node current,Node newNode)
{
newNode.prev=current.prev;
newNode.next=current;
current.prev=newNode;
}
public static void insertAfter(Node current,Node newNode)
{
newNode.prev=current;
newNode.next=current.next;
current.next=newNode;
}
public static void delete(Node deleteNode)
{
if(deleteNode.prev==null && deleteNode.next!=null)
{
deleteNode.next.prev=null;
deleteNode.next=null;
}
else if(deleteNode.prev!=null && deleteNode.next==null)
{
deleteNode.prev.next=null;
deleteNode.prev=null;
}
else if(deleteNode.prev==null && deleteNode.next==null)
{
deleteNode.prev=null;
deleteNode.next=null;
}
else
{
deleteNode.prev.next=deleteNode.next;
deleteNode.next.prev=deleteNode.prev;
deleteNode.prev=null;
deleteNode.next=null;
}
}
}