我已经写了一个带有添加和遍历方法的单链表的小程序。现在我想将它转换为双向链表。我知道双链表的所有概念,但在我的程序中实现它却很困难。单独转换为单向链接列表双向链接列表
public class SingleLinkList<T> {
private Node<T> head;
private Node<T> tail;
public void add(T element)
{
Node<T> nd = new Node<T>();
nd.setValue(element);
if (head==null)
{
head = nd;
tail = nd;
}
else
{
tail.setNextRef(nd);
tail = nd;
}
}
public void traverse(){
Node<T> tmp = head;
while(true){
if(tmp == null){
break;
}
System.out.println(tmp.getValue());
tmp = tmp.getNextRef();
}
}
public static void main (String args[])
{
SingleLinkList<Integer> s1 = new SingleLinkList<Integer>();
s1.add(2);
s1.add(3);
s1.add(3);
s1.traverse();
}
}
class Node<T> {
private T value;
private Node<T> nextRef;
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
public Node<T> getNextRef() {
return nextRef;
}
public void setNextRef(Node<T> nextRef) {
this.nextRef = nextRef;
}
public int compareTo(T arg)
{
if (arg==this.value)
{
return 0;}
else
{return 1;}
}
}
_什么难度? – Idos
如何摆放额外的食物。到一个节点...我不认为这是一个问题,你可以downvote它 – user1111880
我认为@Idos试图说什么是你的具体问题是什么?你尝试过什么吗?你说得对,这不是一个坏问题,但告诉我们问题是什么。 –