2010-07-19 8 views

Répondre

1

pseudocode:

list-node 
{ 
    some-data 
    pointer-or-reference to prev-list-node 
    pointer-or-reference to next-list-node 
} 

De cette façon, vous pouvez:

current-node = beginning 
current-node = current-node -> next-list-node 
current-node = current-node -> prev-list-node 

de "déplacer" avant et en arrière. La syntaxe dépend de la langue.

0

En C#:

internal class ListNode<T> { 

    public T Item { get; set; } 
    public ListNode Prev { get; set; } 
    public ListNode Next { get; set; } 

} 

public class List<T> { 

    public List() { Head = Tail = null; } 

    public void Add(T item) { 
     var node = new ListNode<T>(item); 
     if(IsEmpty) 
      Head = Tail = node; 
     else { 
      node.Prev = Tail; 
      Tail.Next = node; 
      Tail = node; 
     }   
    } 

    protected ListNode<T> Head { get; set; } 
    protected ListNode<T> Tail { get; set; } 

    public bool IsEmpty { get { return Head == null; } } 

} 

et ainsi de suite ...