最近在看《算法》,这是课后的一道关于双向链表习题的解决方案。
package com.xh.node;
/**
* 实现双向链表
* @author kali
*
*/
public class TwoLink<Item>{
//链表开始节点
private DoubleNode first;
//链表最后节点
private DoubleNode last;
//链表节点数
private int num;
public boolean isEmpty()
{
return num==0;
}
public int size()
{
return num;
}
//在第一个节点前加一个
public void addFirst(Item item)
{ if(num==0)
{
first=new DoubleNode();
first.item=item;
last=first;
num++;
}
else
{
DoubleNode oldFirst=first;
first=new DoubleNode();
first.item=item;
first.next=oldFirst;
oldFirst.previous=first;
num++;
}
}
//删除第一个节点
public Item delFirst()
{
if(isEmpty()) return null;
Item item=first.item;
first=first.next;
num--;
return item;
}
//在最后节点前加一个
public void addLast(Item item)
{
if(num==0)
{
last=new DoubleNode();
last.item=item;
first=last;
num++;
}
else
{
DoubleNode oldLast=last;
last=new DoubleNode();
last.item=item;
last.previous=oldLast;
oldLast.next=last;
num++;
}
}
//删除最后一个节点
public Item delLast()
{
if(isEmpty()) return null;
Item item=last.item;
last=last.previous;
num--;
return item;
}
//在指定节点前增加
public void add2Before(Item item,Item newitem)
{
DoubleNode node= getNode(item, first);
DoubleNode newnode=new DoubleNode();
newnode.item=newitem;
if(node!=null)
{ DoubleNode before=node.previous;
before.next=newnode;//重点
newnode.previous=node.previous;
newnode.next=node;
node.previous=newnode;
num++;
}
}
//在指定节点后增加
public void add2Last(Item item,Item newitem)
{
DoubleNode node= getNode(item, first);
DoubleNode newnode=new DoubleNode();
newnode.item=newitem;
if(node!=null)
{ DoubleNode after=node.next;
after.previous=newnode;//重点
newnode.next=node.next;
newnode.previous=node;
node.next=newnode;
num++;
}
}
//删除指定节点
public void del2It(Item item)
{
DoubleNode node= getNode(item, first);
if(node!=null)
{ DoubleNode before=node.previous;
DoubleNode after=node.next;
after.previous=before;
before.next=after;
num--;
}
}
//查询所有元素
public String getAll(DoubleNode f)
{
String all="";
if(f!=null)
{
all=f.item.toString();
all+=getAll(f.next);
}
return all;
}
//查询指定元素节点
public DoubleNode getNode(Item item,DoubleNode f)
{
DoubleNode node=f;
if(f!=null)
{
if(node.item.equals(item))
{
return node;
}
else{
//这样高效,不要在else{}外面return;
return getNode(item,f.next);
}
}else{
return null;
}
}
public static void main(String[] args) {
TwoLink<String> twoLink=new TwoLink<>();
twoLink.addFirst("a");
twoLink.addFirst("b");
twoLink.addFirst("c");
twoLink.addFirst("d");
twoLink.addFirst("e");
twoLink.addFirst("f");
twoLink.addFirst("g");
twoLink.addFirst("h");
twoLink.addLast("1");
twoLink.addLast("2");
twoLink.addLast("3");
System.out.println(twoLink.num);
//System.out.println(twoLink.delFirst());
System.out.println(twoLink.delLast());
System.out.println(twoLink.num);
System.out.println(twoLink.getAll(twoLink.first));
System.out.println(twoLink.num);
System.out.println(twoLink.getNode("2",twoLink.first));
twoLink.add2Before("2", "4");
twoLink.add2Last("2", "5");
twoLink.del2It("2");
System.out.println(twoLink.getAll(twoLink.first));
}
//定义节点
private class DoubleNode {
//当前节点前一个
private DoubleNode previous;
//当前节点后一个
private DoubleNode next;
//当前节点的内容
private Item item;
}
}