Csharp/CSharp Tutorial/Data Structure/LinkList
Содержание
- 1 Add three elements to the end of the list
- 2 Add value to generic LinkList and check the element count
- 3 Display the linked list by manually walking through the list
- 4 Display the linked list by using a foreach loop
- 5 Display the list backwards by manually walking from last to first
- 6 Remove elements from the linked list
Add three elements to the end of the list
<source lang="csharp">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"); }
}</source>
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
<source lang="csharp">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); }
}</source>
Initial number of elements: 0 Adding 5 elements. Number of elements: 5
Display the linked list by manually walking through the list
<source lang="csharp">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 + " "); }
}</source>
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
<source lang="csharp">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"); }
}</source>
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
<source lang="csharp">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"); }
}</source>
Adding 5 elements. Follow links backwards: A B C D E
Remove elements from the linked list
<source lang="csharp">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); }
}</source>
Adding 5 elements. Removing 2 elements. Number of elements: 3