
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
LinkedList AddBefore method in C#
Add a node before a given node in C# using the AddBefore() method.
Our LinkedList with string nodes.
string [] students = {"Henry","David","Tom"}; LinkedList<string> list = new LinkedList<string>(students);
Now, let’s add node at the end.
// adding a node at the end var newNode = list.AddLast("Brad");
Use AddBefore() method to add a node before the node added above.
list.AddBefore(newNode, "Emma");
Example
using System; using System.Collections.Generic; class Demo { static void Main() { string [] students = {"Henry","David","Tom"}; LinkedList<string> list = new LinkedList<string>(students); foreach (var stu in list) { Console.WriteLine(stu); } // adding a node at the end var newNode = list.AddLast("Brad"); // adding a new node before the node added above list.AddBefore(newNode, "Emma"); Console.WriteLine("LinkedList after adding new nodes..."); foreach (var stu in list) { Console.WriteLine(stu); } } }
Output
Henry David Tom LinkedList after adding new nodes... Henry David Tom Emma Brad
- Related Articles
- LinkedList AddFirst method in C#
- LinkedList AddAfter method in C#
- LinkedList Remove method in C#
- LinkedList RemoveFirst() Method in C#
- LinkedList RemoveLast() Method in C#
- LinkedList Clear() Method in C#
- LinkedList Contains Method in C#
- LinkedList AddLast method in C#
- LinkedList in C#
- Removing all nodes from LinkedList in C#
- Singly LinkedList Traversal using C#
- Copy the entire LinkedList to Array in C#
- C# Program to create a LinkedList
- Check if a value is in LinkedList in C#
- LinkedList in Java

Advertisements