Implementation of C# LinkedList class: AddLast() and AddFirst() methods
Program.cs
using System.Collections.Generic;

LinkedList<string> ll = new LinkedList<string>();
ll.AddLast("Apple");
ll.AddLast("Orange");
ll.AddLast("Mango");
ll.AddLast("Grapes");
ll.AddLast("Cherry");
ll.AddLast("Apple");
ll.AddFirst("Pineapple");
foreach (string s in ll)
{
    Console.Write(s + " ");
}
Console.WriteLine("\nSize: " + ll.Count);
Output
kodingwindow@kw:~/csharp$ dotnet run
Pineapple Apple Orange Mango Grapes Cherry Apple 
Size: 7
kodingwindow@kw:~/csharp$ 
Implementation of C# LinkedList class: AddAfter() and AddBefore() methods
Program.cs
using System.Collections.Generic;

LinkedList<string> ll = new LinkedList<string>();
ll.AddLast("Apple");
ll.AddLast("Orange");
ll.AddLast("Mango");
ll.AddLast("Grapes");
ll.AddLast("Cherry");
ll.AddLast("Apple");

ll.AddAfter(ll.Find("Apple")!, "Blueberry");
ll.AddBefore(ll.Find("Orange")!, "Pineapple");
foreach (string s in ll)
{
    Console.Write(s + " ");
}
Console.WriteLine("\n" + ll.Contains("Pineapple"));
Output
kodingwindow@kw:~/csharp$ dotnet run
Apple Blueberry Pineapple Orange Mango Grapes Cherry Apple 
True
kodingwindow@kw:~/csharp$ 
Implementation of C# LinkedList class: RemoveFirst() and RemoveLast() methods
Program.cs
using System.Collections.Generic;

LinkedList<string> ll = new LinkedList<string>();
ll.AddLast("Apple");
ll.AddLast("Orange");
ll.AddLast("Mango");
ll.AddLast("Grapes");
ll.AddLast("Cherry");
ll.AddLast("Apple");
ll.AddLast("Apple");

ll.RemoveFirst();
ll.Remove("Orange");
ll.RemoveLast();
foreach (string s in ll)
{
    Console.Write(s + " ");
}
ll.Clear();
Console.WriteLine("\nSize: " + ll.Count);
Output
kodingwindow@kw:~/csharp$ dotnet run
Mango Grapes Cherry Apple  
Size: 0
kodingwindow@kw:~/csharp$ 
Advertisement