Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 AddFirst method in C#
In a Linked List, if you want to add a node at the first position, use AddFirst method.
Let’s first set a LinkedList.
string [] students = {"Jenifer","Angelina","Vera"};
LinkedList<string> list = new LinkedList<string>(students);
Now, to add an element as a first node, use AddFirst() method.
List.AddFirst(“Natalie”);
Example
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
string [] students = {"Jenifer","Angelina","Vera"};
LinkedList<string> list = new LinkedList<string>(students);
foreach (var stu in list) {
Console.WriteLine(stu);
}
// add a node
Console.WriteLine("Node added at the first position...");
list.AddFirst("Natalie");
foreach (var stu in list) {
Console.WriteLine(stu);
}
}
}
Output
Jenifer Angelina Vera Node added at the first position... Natalie Jenifer Angelina Vera
Advertisements