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);
}
}
程序是这样的,但是我不懂这怎么实现了单向链表,并且要怎么才可以改成双向链表,请大神解释
------解决思路----------------------
Node多加一个引用,previous用来指向前面的结点就可以了。