Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C#

C# में LinkedList से प्रारंभिक नोड हटाएं

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() से अंतिम नोड हटा सकते हैं
  1. - C# में LinkedList में अंतिम स्थिति में नोड जोड़ना

    नोड्स के साथ LinkedList सेट करें: string[] students = { Tim, Jack, Henry, David, Tom }; LinkedList<string> list = new LinkedList<string>(students); अब अंतिम स्थिति में नोड जोड़ने के लिए AddLast() विधि का उपयोग करें: list.AddLast(Kevin); पूर्ण कोड using System; using System.Collections.Ge

  1. - C# में LinkedList में दिए गए नोड के बाद नोड जोड़ना

    LinkedList सेट करें और तत्व जोड़ें: string[] students = { Beth, Jennifer, Amy, Vera }; LinkedList<string> list = new LinkedList<string>(students); पहले अंत में नया नोड जोड़ें: var newNode = list.AddLast(Emma); अब दिए गए नोड के बाद नोड जोड़ने के लिए AddAfter() विधि का उपयोग करें: list.Add

  1. - C# प्रोग्राम: लिंक्ड लिस्ट में पहले स्थान पर नोड जोड़ना

    सबसे पहले, नोड्स के साथ एक LinkedList सेट करें।string [] students = {"Tim","Jack","Henry","David","Tom"}; LinkedList<string> list = new LinkedList<string>(students);पहले स्थान पर नोड जोड़ने के लिए, AddFirst() मेथड का उपयोग करें।list.AddFi