
- 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
C# Program to add a node before the given node in a Linked List
Declare a LinkedList and add nodes to it.
string [] students = {"Tim","Jack","Henry","David","Tom"}; LinkedList<string> list = new LinkedList<string>(students);
Let us add a new node.
var newNode = list.AddLast("Kevin");
Now, to add a node before the given node, use the AddBefore() method.
list.AddBefore(newNode, "Matt");
Let us now see the complete code.
Example
using System; using System.Collections.Generic; class Demo { static void Main() { string [] students = {"Tim","Jack","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("Kevin"); // adding a new node before the node added above list.AddBefore(newNode, "Matt"); Console.WriteLine("LinkedList after adding new nodes..."); foreach (var stu in list) { Console.WriteLine(stu); } } }
Output
Tim Jack Henry David Tom LinkedList after adding new nodes... Tim Jack Henry David Tom Matt Kevin
- Related Articles
- C# Program to add a node after the given node in a Linked List
- Golang Program to add the first node in a given linked list.
- Golang Program to add a node at the end of a given linked list.
- C# Program to add a node at the first position in a Linked List
- C# Program to add a node at the last position in a Linked List
- C++ Program to Delete the First Node in a given Singly Linked List
- C# program to find node in Linked List
- Delete a Linked List node at a given position in C++
- Delete a Doubly Linked List node at a given position in C++
- C# Program to remove a node at the beginning of a Linked List
- Write a program in C++ to insert a Node at the beginning of the given Singly linked list
- C# program to remove the first occurrence of a node in a Linked List
- Linked List Random Node in C++
- Delete a tail node from the given singly Linked List using C++
- JavaScript Program for Inserting a Node in a Linked List

Advertisements