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

C# में LinkedList में निर्दिष्ट मान वाला पहला नोड ढूंढना

C# में LinkedList<T> क्लास का Find() मेथड आपको लिंक्ड लिस्ट में किसी निर्दिष्ट मान वाला पहला नोड ढूंढने की सुविधा देता है। यह मेथड LinkedListNode<T> ऑब्जेक्ट लौटाता है, जिसमें वह मान होता है। यदि मान नहीं मिलता, तो यह null लौटाता है।

उदाहरण 1: स्ट्रिंग मान ढूंढना

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        LinkedList<string> list = new LinkedList<string>();
        list.AddLast("John");
        list.AddLast("Tim");
        list.AddLast("Kevin");
        list.AddLast("Jacob");
        list.AddLast("Emma");
        list.AddLast("Ryan");
        list.AddLast("Brad");
        list.AddLast("Carl");

        Console.WriteLine("LinkedList तत्व...");
        foreach (string str in list) {
            Console.WriteLine(str);
        }

        // "Jacob" मान वाला पहला नोड ढूंढें
        LinkedListNode<string> node = list.Find("Jacob");
        
        if (node != null) {
            Console.WriteLine("निर्दिष्ट मान = " + node.Value);
        } else {
            Console.WriteLine("मान नहीं मिला");
        }
    }
}

आउटपुट

LinkedList तत्व...
John
Tim
Kevin
Jacob
Emma
Ryan
Brad
Carl
निर्दिष्ट मान = Jacob

उदाहरण 2: इंटीजर मान ढूंढना

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        LinkedList<int> list = new LinkedList<int>();
        list.AddLast(100);
        list.AddLast(200);
        list.AddLast(300);
        list.AddLast(400);
        list.AddLast(500);

        Console.WriteLine("LinkedList तत्व...");
        foreach (int i in list) {
            Console.WriteLine(i);
        }

        // 300 मान वाला पहला नोड ढूंढें
        LinkedListNode<int> node = list.Find(300);
        
        if (node != null) {
            Console.WriteLine("निर्दिष्ट मान = " + node.Value);
        } else {
            Console.WriteLine("मान नहीं मिला");
        }
    }
}

आउटपुट

LinkedList तत्व...
100
200
300
400
500
निर्दिष्ट मान = 300

महत्वपूर्ण बिंदु

  • Find() मेथड केवल पहला मिलने वाला नोड लौटाता है।
  • यदि एक ही मान के कई नोड्स हों, तो केवल पहला मिलेगा।
  • सभी मिलने वाले नोड्स के लिए FindLast() या मैन्युअल इटरेशन का उपयोग करें।
  • यह मेथड O(n) समय जटिलता के साथ रैखिक खोज करता है।
  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

  1. - C# में लिंक्ड लिस्ट में नोड खोजना

    C# में System.Collections.Generic.LinkedList<T> क्लास डबल लिंक्ड लिस्ट प्रदान करती है। Find() मेथड का उपयोग करके किसी मान वाला पहला नोड खोजा जा सकता है। उदाहरण: नोड खोजें और उसके बाद नया नोड जोड़ें using System; using System.Collections.Generic; class Program { static void Main() {