- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements