Exemple #1
0
 private void LihatData()
 {
     Console.WriteLine("*******************************");
     if (_doublyLinkedList.Count > 0)
     {
         Console.WriteLine("Nilai Head/Start: {0}", _doublyLinkedList.Head.Value);
         Console.WriteLine("Nilai Tail/End: {0}\n", _doublyLinkedList.Tail.Value);
         DoublyLinkedListNode <int> counter = _doublyLinkedList.Head;
         while (counter != null)
         {
             string prev    = counter.Previous == null ? "NULL" : counter.Previous.Value.ToString();
             string current = counter.Value.ToString();
             string next    = counter.Next == null ? "NULL" : counter.Next.Value.ToString();
             Console.WriteLine("  |Prev-Link: {0} | (Nilai: {1}) | Next-Link: {2} |", prev, current, next);
             counter = counter.Next;
         }
     }
     else
     {
         Console.WriteLine("Tidak ada data di linked list");
     }
     Console.WriteLine("\n*******************************\n");
 }
 /// <summary>
 /// Removes an item from the list.
 /// </summary>
 /// <param name="value">Item to remove. Generic type.</param>
 /// <returns>True if an item is removed. Otherwise false.</returns>
 /// <exception cref="InvalidOperationException">List is empty.</exception>
 public bool Remove(T value)
 {
     if (_count == 0)
     {
         throw new InvalidOperationException("List is empty!");
     }
     if (Head.Value.Equals(value))
     {
         RemoveFirst();
         return(true);
     }
     else if (Tail.Value.Equals(value))
     {
         RemoveLast();
         return(true);
     }
     else
     {
         DoublyLinkedListNode <T> current = Head;
         while (current != null)
         {
             if (current.Value.Equals(value))
             {
                 DoublyLinkedListNode <T> prev = current.Previous;
                 DoublyLinkedListNode <T> next = current.Next;
                 prev.Next     = next;
                 next.Previous = prev;
                 current       = null;
                 _count--;
                 return(true);
             }
             current = current.Next;
         }
     }
     return(false);
 }
 /// <summary>
 /// Clears the items in the list.
 /// </summary>
 public void Clear()
 {
     _count = 0;
     Head   = Tail = null;
 }
 public DoublyLinkedListNode(T value)
 {
     this.Value    = value;
     this.Previous = null;
     this.Next     = null;
 }
 public DoublyLinkedListNode()
 {
     this.Previous = null;
     this.Next     = null;
 }