Examples of Linked Lists C#
Examples of Linked Lists C#
Name Description
AddAfter(LinkedListNode<T>, Adds the specified new node after the specified existing
LinkedListNode<T>) node in the LinkedList<T>.
AddAfter(LinkedListNode<T>, T) Adds a new node containing the specified value after the
specified existing node in theLinkedList<T>.
AddBefore(LinkedListNode<T>, Adds the specified new node before the specified existing
LinkedListNode<T>) node in the LinkedList<T>.
AddBefore(LinkedListNode<T>, T) Adds a new node containing the specified value before the
specified existing node in theLinkedList<T>.
AddFirst(T) Adds a new node containing the specified value at the start
of the LinkedList<T>.
AddLast(T) Adds a new node containing the specified value at the end
of the LinkedList<T>.
Find Finds the first node that contains the specified value.
FindLast Finds the last node that contains the specified value.
GetObjectData Implements
the System.Runtime.Serialization.ISerializable interface
and returns the data needed to serialize
the LinkedList<T> instance.
OnDeserialization Implements
the System.Runtime.Serialization.ISerializable interface
and raises the deserialization event when the deserialization
is complete.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
//
// Create a new linked list object instance.
//
LinkedList<string> linked = new LinkedList<string>();
//
// Use AddLast method to add elements at the end.
// Use AddFirst method to add element at the start.
//
linked.AddLast("cat");
linked.AddLast("dog");
linked.AddLast("man");
linked.AddFirst("first");
//
// Loop through the linked list with the foreach-loop.
//
foreach (var item in linked)
{
Console.WriteLine(item);
}
}
}
Output
first
cat
dog
man
C# program that finds nodes in LinkedList
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Create a new linked list.
//
LinkedList<string> linked = new LinkedList<string>();
//
// First add three elements to the linked list.
linked.AddLast("one");
linked.AddLast("two");
linked.AddLast("three");
// Insert a node before the second node (after the first node)
//
LinkedListNode<string> node = linked.Find("one");
linked.AddAfter(node, "inserted");
//
// Loop through the linked list.
//
foreach (var value in linked)
{
Console.WriteLine(value);
}
}
}
Output
one
inserted
two
three
using System;
using System.Text;
using System.Collections.Generic;
try
{
sentence.AddBefore(current, mark1);
}
catch (InvalidOperationException ex)
{
Console.WriteLine("Exception message: {0}",
ex.Message);
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Test 17: Clear linked list. Contains
'jumped' = {0}",
//Test 17: Clear linked list. Contains 'jumped' = False
//
sentence.Contains("jumped"));
Console.ReadLine();
}
node = node.Next;
while (node != null)
{
result.Append(" " + node.Value);
node = node.Next;
}
Console.WriteLine(result);
Console.WriteLine();
}}
//Test 10: Throw exception by adding node (fox) already in the list:
//Exception message: The LinkedList node belongs a LinkedList.
//Test 11: Move a referenced node (fox) before the current node
(dog):
//the quick brown jumped over the lazy old fox (dog)
//Test 12: Remove current node (dog) and attempt to indicate it:
//Node 'dog' is not in the list.