Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to add a node after the given node in a Linked List
A LinkedList in C# is a doubly linked list that allows efficient insertion and removal of elements at any position. The AddAfter() method inserts a new node immediately after a specified existing node in the list.
Syntax
Following is the syntax for the AddAfter() method −
public LinkedListNode<T> AddAfter(LinkedListNode<T> node, T value)
Parameters
-
node − The LinkedListNode after which to insert a new node containing value.
-
value − The value to add to the LinkedList.
Return Value
The method returns the new LinkedListNode<T> containing the specified value.
Using AddAfter with Node Reference
Example
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
string[] students = {"Beth", "Jennifer", "Amy", "Vera"};
LinkedList<string> list = new LinkedList<string>(students);
Console.WriteLine("Original LinkedList:");
foreach (var stu in list) {
Console.WriteLine(stu);
}
// adding a node at the end
var newNode = list.AddLast("Emma");
// adding a new node after the node added above
list.AddAfter(newNode, "Matt");
Console.WriteLine("\nLinkedList after adding new nodes:");
foreach (var stu in list) {
Console.WriteLine(stu);
}
}
}
The output of the above code is −
Original LinkedList: Beth Jennifer Amy Vera LinkedList after adding new nodes: Beth Jennifer Amy Vera Emma Matt
Using AddAfter with Find Method
You can also use the Find() method to locate an existing node and add a new node after it −
Example
using System;
using System.Collections.Generic;
class Program {
static void Main() {
LinkedList<int> numbers = new LinkedList<int>();
numbers.AddLast(10);
numbers.AddLast(20);
numbers.AddLast(30);
Console.WriteLine("Original list:");
foreach (int num in numbers) {
Console.Write(num + " ");
}
// Find the node with value 20 and add 25 after it
LinkedListNode<int> foundNode = numbers.Find(20);
if (foundNode != null) {
numbers.AddAfter(foundNode, 25);
}
Console.WriteLine("
\nAfter adding 25 after node 20:");
foreach (int num in numbers) {
Console.Write(num + " ");
}
}
}
The output of the above code is −
Original list: 10 20 30 After adding 25 after node 20: 10 20 25 30
Adding Multiple Nodes
Example
using System;
using System.Collections.Generic;
class MultipleNodes {
static void Main() {
LinkedList<string> colors = new LinkedList<string>();
colors.AddLast("Red");
colors.AddLast("Blue");
Console.WriteLine("Initial colors:");
foreach (string color in colors) {
Console.WriteLine(color);
}
// Add multiple nodes after "Red"
LinkedListNode<string> redNode = colors.Find("Red");
colors.AddAfter(redNode, "Orange");
colors.AddAfter(redNode, "Yellow"); // This adds Yellow right after Red
Console.WriteLine("\nAfter adding Orange and Yellow:");
foreach (string color in colors) {
Console.WriteLine(color);
}
}
}
The output of the above code is −
Initial colors: Red Blue After adding Orange and Yellow: Red Yellow Orange Blue
Conclusion
The AddAfter() method in C# LinkedList provides an efficient way to insert new nodes at specific positions. It requires a reference to an existing node and inserts the new node immediately after it, maintaining the doubly-linked structure of the list.
