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

Android में लिंक्ड लिस्ट को कैसे रिवर्स करें?

Android में लिंक्ड लिस्ट को रिवर्स करना

यह उदाहरण दर्शाता है कि Android में लिंक्ड लिस्ट को कैसे रिवर्स (उल्टा) किया जाता है।

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio में File → New Project पर जाएं और आवश्यक विवरण भरकर एक नया प्रोजेक्ट बनाएं।

चरण 2: लेआउट XML में कोड जोड़ें

res/layout/activity_main.xml में निम्नलिखित कोड जोड़ें:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text"
        android:textSize="30sp"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

उपरोक्त कोड में, हमने लिंक्ड लिस्ट के रिवर्स को दिखाने के लिए TextView लिया है।

चरण 3: MainActivity.java में कोड जोड़ें

package com.example.myapplication;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.LinkedList;

public class MainActivity extends AppCompatActivity {
    @RequiresApi(api = Build.VERSION_CODES.P)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinkedList<Integer> linkedList = new LinkedList<>();
        linkedList.add(2);
        linkedList.add(8);
        linkedList.add(2);
        linkedList.add(5);
        linkedList.add(7);
        linkedList.add(5);
        linkedList.add(9);

        // 1. सॉर्ट करें
        Collections.sort(linkedList); // [2, 2, 5, 5, 7, 8, 9]
        
        // 2. रिवर्स करें
        Collections.reverse(linkedList); // [9, 8, 7, 5, 5, 2, 2]
        
        // 3. डुप्लिकेट हटाएं (LinkedHashSet ऑर्डर बनाए रखता है)
        LinkedHashSet<Integer> hashSet = new LinkedHashSet<>(linkedList);

        TextView textView = findViewById(R.id.text);
        textView.setText("रिवर्स और यूनिक: " + hashSet.toString());
    }
}

एप्लिकेशन चलाएं

अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में प्रोजेक्ट की एक्टिविटी फाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और परिणाम देखें।

Android में लिंक्ड लिस्ट को कैसे रिवर्स करें?

परिणाम: [9, 8, 7, 5, 2] - सॉर्टेड, रिवर्स्ड, और डुप्लिकेट-मुक्त।

लिंक्ड लिस्ट रिवर्स करने के तरीके

1. Collections.reverse() - सबसे सरल

LinkedList<Integer> list = new LinkedList<>();
list.add(1); list.add(2); list.add(3);
Collections.reverse(list); // [3, 2, 1]

2. Iterator का उपयोग करके (मैन्युअल)

public static <E> void reverseManually(LinkedList<E> list) {
    for (int i = 0, j = list.size() - 1; i < j; i++, j--) {
        E temp = list.get(i);
        list.set(i, list.get(j));
        list.set(j, temp);
    }
}

3. कस्टम Node क्लास के साथ (इंटरव्यू स्टाइल)

class Node {
    int data;
    Node next;
    Node(int data) { this.data = data; }
}

public Node reverseList(Node head) {
    Node prev = null;
    Node current = head;
    Node next = null;
    
    while (current != null) {
        next = current.next;    // अगला नोड सेव करें
        current.next = prev;    // लिंक रिवर्स करें
        prev = current;         // prev आगे बढ़ाएं
        current = next;         // current आगे बढ़ाएं
    }
    return prev; // नया हेड
}

4. रिकर्सिव तरीका

public Node reverseRecursive(Node head) {
    if (head == null || head.next == null) {
        return head;
    }
    Node newHead = reverseRecursive(head.next);
    head.next.next = head;
    head.next = null;
    return newHead;
}

तुलना

तरीका समय स्पेस उपयोग
Collections.reverse() O(n) O(1) प्रोडक्शन कोड
इटरेटिव (कस्टम Node) O(n) O(1) इंटरव्यू, कस्टम लिस्ट
रिकर्सिव O(n) O(n) शैक्षिक, छोटी लिस्ट

मुख्य बिंदु

  • Collections.reverse() किसी भी List पर काम करता है
  • कस्टम लिंक्ड लिस्ट के लिए इटरेटिव तरीका बेस्ट है (O(1) स्पेस)
  • रिकर्सिव तरीका स्टैक ओवरफ्लो का कारण बन सकता है
  • Java के LinkedList में descendingIterator() भी है
  • मूल लिस्ट बदल जाती है (इन-प्लेस)
  1. अलर्ट डायलॉग में लिस्ट कैसे जोड़ें? अलर्ट डायलॉग में लिस्ट कैसे जोड़ें?

    अलर्ट डायलॉग में सिंपल लिस्ट यह उदाहरण अलर्ट डायलॉग में लिस्ट जोड़ने को दर्शाता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project पर जाएं और आवश्यक विवरण भरकर एक नया प्रोजेक्ट बनाएं। चरण 2: res/layout/activity_main.xml में कोड जोड़ें <?xml version=1.0 encoding=utf-8?> <

  1. एंड्रॉइड में SearchView का उपयोग कैसे करें? एंड्रॉइड में SearchView का उपयोग कैसे करें?

    एंड्रॉइड में SearchView के साथ लिस्ट फिल्टरिंग यह ट्यूटोरियल SearchView को ListView और ArrayAdapter के साथ इंटीग्रेट करके रियल-टाइम फिल्टरिंग लागू करने का तरीका दिखाता है। लेआउट (activity_main.xml) <RelativeLayout xmlns:android=https://schemas.android.com/apk/res/android android:layout_width

  1. Android में इंस्टॉल ऐप्स की सूची कैसे प्राप्त करें? Android में इंस्टॉल ऐप्स की सूची कैसे प्राप्त करें?

    इंस्टॉल ऐप्स की सूची प्राप्त करना यह उदाहरण दर्शाता है कि PackageManager का उपयोग करके डिवाइस पर इंस्टॉल सभी थर्ड-पार्टी ऐप्स की सूची कैसे प्राप्त करें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: लेआउट में ListView (activity_main.xml) <?xml version=1.0 encodi