Csharp/CSharp Tutorial/Data Structure/LinkList

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Add three elements to the end of the list

using System;  
using System.Collections.Generic;  
  
class MainClass {  
  public static void Main() {  
    LinkedList<char> ll = new LinkedList<char>();  
      
    Console.WriteLine("Adding 5 elements.");  
    ll.AddFirst("A");  
    ll.AddFirst("B");  
    ll.AddFirst("C");  
    ll.AddFirst("D");  
    ll.AddFirst("E");  
    ll.AddLast("X"); 
    ll.AddLast("Y"); 
    ll.AddLast("Z"); 
 
    Console.Write("Contents after addition to end: ");  
    foreach(char ch in ll)  
      Console.Write(ch + " ");  
 
    Console.WriteLine("\n");  
  }  
}
Adding 5 elements.
Contents after addition to end: E D C B A X Y Z

Add value to generic LinkList and check the element count

using System;  
using System.Collections.Generic;  
  
class MainClass {  
  public static void Main() {  
    // Create an linked list. 
    LinkedList<char> ll = new LinkedList<char>();  
      
    Console.WriteLine("Initial number of elements: " +  
                       ll.Count);  
  
    Console.WriteLine();  
  
    Console.WriteLine("Adding 5 elements.");  
    // Add elements to the linked list  
    ll.AddFirst("A");  
    ll.AddFirst("B");  
    ll.AddFirst("C");  
    ll.AddFirst("D");  
    ll.AddFirst("E");  
  
    Console.WriteLine("Number of elements: " +  
                       ll.Count);  
  }
}
Initial number of elements: 0
Adding 5 elements.
Number of elements: 5

Display the linked list by manually walking through the list

using System;  
using System.Collections.Generic;  
  
class MainClass {  
  public static void Main() {  
    // Create an linked list. 
    LinkedList<char> ll = new LinkedList<char>();  
      
    Console.WriteLine("Adding 5 elements.");  
    // Add elements to the linked list  
    ll.AddFirst("A");  
    ll.AddFirst("B");  
    ll.AddFirst("C");  
    ll.AddFirst("D");  
    ll.AddFirst("E");  
  
    Console.WriteLine("Number of elements: " +  
                       ll.Count);  
    LinkedListNode<char> node; 
 
    Console.Write("Display contents by following links: "); 
    for(node = ll.First; node != null; node = node.Next)  
      Console.Write(node.Value + " ");  
  }
}
Adding 5 elements.
Number of elements: 5
Display contents by following links: E D C B A

Display the linked list by using a foreach loop

using System;  
using System.Collections.Generic;  
  
class MainClass {  
  public static void Main() {  
    // Create an linked list. 
    LinkedList<char> ll = new LinkedList<char>();  
      
    Console.WriteLine("Adding 5 elements.");  
    // Add elements to the linked list  
    ll.AddFirst("A");  
    ll.AddFirst("B");  
    ll.AddFirst("C");  
    ll.AddFirst("D");  
    ll.AddFirst("E");  
  
    Console.WriteLine("Number of elements: " +  
                       ll.Count);  
     
    Console.Write("Display contents with foreach loop: "); 
    foreach(char ch in ll) 
      Console.Write(ch + " ");  
 
    Console.WriteLine("\n");  
  }
}
Adding 5 elements.
Number of elements: 5
Display contents with foreach loop: E D C B A

Display the list backwards by manually walking from last to first

using System;  
using System.Collections.Generic;  
  
class MainClass {  
  public static void Main() {  
    // Create an linked list. 
    LinkedList<char> ll = new LinkedList<char>();  
      
    Console.WriteLine("Adding 5 elements.");  
    // Add elements to the linked list  
    ll.AddFirst("A");  
    ll.AddFirst("B");  
    ll.AddFirst("C");  
    ll.AddFirst("D");  
    ll.AddFirst("E");  
    LinkedListNode<char> node; 
    
    Console.Write("Follow links backwards: "); 
      for(node = ll.Last; node != null; node = node.Previous)  
      Console.Write(node.Value + " ");  
 
    Console.WriteLine("\n");  
  }
}
Adding 5 elements.
Follow links backwards: A B C D E

Remove elements from the linked list

using System;  
using System.Collections.Generic;  
  
class MainClass {  
  public static void Main() {  
    // Create an linked list. 
    LinkedList<char> ll = new LinkedList<char>();  
      
    Console.WriteLine("Adding 5 elements.");  
    // Add elements to the linked list  
    ll.AddFirst("A");  
    ll.AddFirst("B");  
    ll.AddFirst("C");  
    ll.AddFirst("D");  
    ll.AddFirst("E");  

    Console.WriteLine("Removing 2 elements.");  
    ll.Remove("C");  
    ll.Remove("A");  
  
    Console.WriteLine("Number of elements: " +  
                       ll.Count);  
  }
}
Adding 5 elements.
Removing 2 elements.
Number of elements: 3