当前位置: 代码迷 >> Java相关 >> 如何将单向链表改为双向链表
  详细解决方案

如何将单向链表改为双向链表

热度:30   发布时间:2016-04-22 20:53:30.0
怎么将单向链表改为双向链表
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用来指向前面的结点就可以了。
  相关解决方案