将单项链表改为双向链表,初学者
public class Node <T>{private T item;
private Node<T>next;
public Node()
{
item=null;
next=null;
}
public Node(T item,Node<T>next)
{
this.item=item;
this.next=next;
}
public T getItem()
{
return item;
}
public void setItem(T item)
{
this.item=item;
}
public Node<T>getNext()
{
return next;
}
public String toString()
{
return item.toString();
}
}
public class GenericList <N>{
private Node<N>head;
public GenericList()
{
head=null;
}
public void add(N item)
{
head=new Node<N>(item,head);
}
public Node<N>getHead()
{
return head;
}
public String toString()
{
String string="";
for(Node<N>node=head;node!=null;node=node.getNext())
{
string+=(node+" ");
}
return string;
}
}
public class TestGenericList {
public static void main(String[] args)
{
GenericList<Integer>inits=new GenericList<Integer>();
for(int index=0;index<10;index++)
{
inits.add(index);
}
System.out.println(inits);
}
}
这是单向链表。我不懂它是怎么实现了,而且怎么改为双向链表,求大神