C# में LinkedList<T> वर्ग की RemoveFirst() विधि का उपयोग करके सूची के पहले नोड को हटाया जा सकता है। यह विधि सूची के शुरुआत से पहला तत्व हटाती है और शेष तत्वों को बरकरार रखती है।
उदाहरण 1: एक बार RemoveFirst() कॉल करना
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
LinkedList list = new LinkedList();
list.AddLast("One");
list.AddLast("Two");
list.AddLast("Three");
list.AddLast("Three");
list.AddLast("Three");
list.AddLast("Four");
Console.WriteLine("नोड्स की संख्या = " + list.Count);
Console.WriteLine("LinkedList में तत्व...");
foreach (string res in list) {
Console.WriteLine(res);
}
list.RemoveFirst();
Console.WriteLine("नोड्स की संख्या (अपडेटेड) = " + list.Count);
Console.WriteLine("LinkedList में तत्व... (अपडेटेड)");
foreach (string res in list) {
Console.WriteLine(res);
}
}
}
आउटपुट
नोड्स की संख्या = 6 LinkedList में तत्व... One Two Three Three Three Four नोड्स की संख्या (अपडेटेड) = 5 LinkedList में तत्व... (अपडेटेड) Two Three Three Three Four
उदाहरण 2: कई बार RemoveFirst() कॉल करना
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
LinkedList list = new LinkedList();
list.AddLast("One");
list.AddLast("Two");
list.AddLast("Three");
list.AddLast("Three");
list.AddLast("Three");
list.AddLast("Four");
Console.WriteLine("नोड्स की संख्या = " + list.Count);
Console.WriteLine("LinkedList में तत्व...");
foreach (string res in list) {
Console.WriteLine(res);
}
list.RemoveFirst();
Console.WriteLine("नोड्स की संख्या (अपडेटेड) = " + list.Count);
Console.WriteLine("LinkedList में तत्व... (अपडेटेड)");
foreach (string res in list) {
Console.WriteLine(res);
}
list.RemoveFirst();
Console.WriteLine("नोड्स की संख्या (फिर अपडेटेड) = " + list.Count);
Console.WriteLine("LinkedList में तत्व... (फिर अपडेटेड)");
foreach (string res in list) {
Console.WriteLine(res);
}
}
}
आउटपुट
नोड्स की संख्या = 6 LinkedList में तत्व... One Two Three Three Three Four नोड्स की संख्या (अपडेटेड) = 5 LinkedList में तत्व... (अपडेटेड) Two Three Three Three Four नोड्स की संख्या (फिर अपडेटेड) = 4 LinkedList में तत्व... (फिर अपडेटेड) Three Three Three Four
मुख्य बिंदु
RemoveFirst()पहली नोड को हटाता है- यदि सूची खाली है तो
InvalidOperationExceptionफेंकता है - समय जटिलता O(1) है
RemoveLast()से अंतिम नोड हटा सकते हैं