- 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 AddAfter method in C#
Set a LinkedList.
int [] num = {1, 2, 3, 4, 5}; LinkedList<int> list = new LinkedList<int>(num);
Now add a node at the end using AddLast() method.
var newNode = list.AddLast(20);
To add a node after the above added node, use the AddAfter() method.
list.AddAfter(newNode, 30);
Example
using System; using System.Collections.Generic; class Demo { static void Main() { int [] num = {1, 2, 3, 4, 5}; LinkedList<int> list = new LinkedList<int>(num); foreach (var n in list) { Console.WriteLine(n); } // adding a node at the end var newNode = list.AddLast(20); // adding a new node after the node added above list.AddAfter(newNode, 30); Console.WriteLine("LinkedList after adding new nodes..."); foreach (var n in list) { Console.WriteLine(n); } } }
Output
1 2 3 4 5 LinkedList after adding new nodes... 1 2 3 4 5 20 30
Advertisements