इस ट्यूटोरियल में, हम उदाहरणों की मदद से जावा लिंक्डलिस्ट के बारे में विस्तार से जानेंगे।
LinkedList
जावा संग्रह ढांचे का वर्ग जुड़ा हुआ सूची डेटा संरचना (दोगुना लिंक्डलिस्ट) की कार्यक्षमता प्रदान करता है।

लिंक की गई सूची में प्रत्येक तत्व को नोड के रूप में जाना जाता है । इसमें 3 फ़ील्ड शामिल हैं:
- Prev - सूची में पिछले तत्व का एक पता संग्रहीत करता है। यह
null
पहले तत्व के लिए है - अगला - सूची में अगले तत्व का पता संग्रहीत करता है। यह
null
अंतिम तत्व के लिए है - डेटा - वास्तविक डेटा को संग्रहीत करता है
एक जावा लिंक्डलिस्ट बनाना
यहाँ हम जावा में लिंक्ड लिस्ट बना सकते हैं:
LinkedList linkedList = new LinkedList();
यहां, टाइप एक लिंक्ड सूची के प्रकार को इंगित करता है। उदाहरण के लिए,
// create Integer type linked list LinkedList linkedList = new LinkedList(); // create String type linked list LinkedList linkedList = new LinkedList();
उदाहरण: जावा में लिंक्डलिस्ट बनाएं
import java.util.LinkedList; class Main ( public static void main(String() args)( // create linkedlist LinkedList animals = new LinkedList(); // Add elements to LinkedList animals.add("Dog"); animals.add("Cat"); animals.add("Cow"); System.out.println("LinkedList: " + animals); ) )
आउटपुट
लिंक्डलिस्ट: (कुत्ता, बिल्ली, गाय)
उपरोक्त उदाहरण में, हमने एक LinkedList
नामित जानवर बनाया है ।
यहां, हमने add()
लिंक्डलिस्ट में तत्वों को जोड़ने के लिए विधि का उपयोग किया है । हम add()
इस ट्यूटोरियल में बाद में विधि के बारे में अधिक जानेंगे ।
जावा लिंक्डलिस्ट का कार्य करना
लिंक की गई सूचियों में तत्व अनुक्रम में संग्रहीत नहीं हैं। इसके बजाय, वे बिखरे हुए हैं और लिंक ( Prev और Next ) के माध्यम से जुड़े हुए हैं ।

यहां हमारे पास लिंक की गई सूची में 3 तत्व हैं।
- कुत्ता - यह पहला तत्व है जो पिछले पते के रूप में और अगले पते के रूप में बिल्ली के पते के रूप में अशक्त है
- कैट - यह दूसरा तत्व है जो पिछले पते के रूप में डॉग का पता और अगले पते के रूप में गाय का पता रखता है
- गाय - यह आखिरी तत्व है जो बिल्ली के पते को पिछले पते के रूप में रखता है और अगले तत्व के रूप में अशक्त है
अधिक जानने के लिए, लिंक्डलिस्ट डेटा संरचना पर जाएं।
जावा लिंक्डलिस्ट के तरीके
LinkedList
विभिन्न तरीकों को प्रदान करता है जो हमें लिंक किए गए सूचियों में विभिन्न ऑपरेशन करने की अनुमति देता है। हम इस ट्यूटोरियल में चार सामान्यतः उपयोग किए जाने वाले लिंक्डलिस्ट संचालकों को देखेंगे:
- तत्वों को जोड़ें
- तत्वों को एक्सेस करें
- तत्वों को बदलें
- तत्वों को हटा दें
1. LinkedList में तत्व जोड़ें
हम add()
लिंक्डलिस्ट के अंत में एक तत्व (नोड) जोड़ने के लिए विधि का उपयोग कर सकते हैं । उदाहरण के लिए,
import java.util.LinkedList; class Main ( public static void main(String() args)( // create linkedlist LinkedList animals = new LinkedList(); // add() method without the index parameter animals.add("Dog"); animals.add("Cat"); animals.add("Cow"); System.out.println("LinkedList: " + animals); // add() method with the index parameter animals.add(1, "Horse"); System.out.println("Updated LinkedList: " + animals); ) )
आउटपुट
लिंक्डलिस्ट: (कुत्ता, बिल्ली, गाय) अपडेटेड लिंक्डलिस्ट: (कुत्ता, घोड़ा, बिल्ली, गाय)
उपरोक्त उदाहरण में, हमने एक लिंक्डलिस्ट नाम का जानवर बनाया है। यहां, हमने add()
जानवरों को तत्वों को जोड़ने के लिए विधि का उपयोग किया है ।
कथन पर ध्यान दें,
animals.add(1, "Horse");
यहां, हमने इंडेक्स नंबर पैरामीटर का उपयोग किया है । यह एक वैकल्पिक पैरामीटर है जो उस स्थिति को निर्दिष्ट करता है जहां नया तत्व जोड़ा जाता है।
लिंक्डलिस्ट में तत्वों को जोड़ने के बारे में अधिक जानने के लिए, लिंक्डलिस्ट में तत्वों को जोड़ने के लिए जावा प्रोग्राम पर जाएं।
2. लिंक्डलिस्ट तत्वों तक पहुंचें
get()
लिंक्डलिस्ट क्लास की विधि का उपयोग लिंक्डलिस्ट से किसी तत्व तक पहुंचने के लिए किया जाता है। उदाहरण के लिए,
import java.util.LinkedList; class Main ( public static void main(String() args) ( LinkedList languages = new LinkedList(); // add elements in the linked list languages.add("Python"); languages.add("Java"); languages.add("JavaScript"); System.out.println("LinkedList: " + languages); // get the element from the linked list String str = languages.get(1); System.out.print("Element at index 1: " + str); ) )
आउटपुट
LinkedList: (पायथन, जावा, जावास्क्रिप्ट) 1 इंडेक्स पर तत्व: जावा
उपरोक्त उदाहरण में, हमने get()
पैरामीटर 1 के साथ विधि का उपयोग किया है । यहां, विधि तत्व को इंडेक्स 1 पर लौटाता है ।
हम लिंक्डलिस्ट के तत्वों का उपयोग विधि iterator()
और listIterator()
विधि से भी कर सकते हैं । अधिक जानने के लिए, लिंक्डलिस्ट के तत्वों तक पहुंचने के लिए जावा प्रोग्राम पर जाएं।
3. एक लिंक्डलिस्ट के तत्वों को बदलें
लिंक्डलिस्ट के तत्वों को बदलने के लिए कक्षा की set()
विधि का LinkedList
उपयोग किया जाता है। उदाहरण के लिए,
import java.util.LinkedList; class Main ( public static void main(String() args) ( LinkedList languages = new LinkedList(); // add elements in the linked list languages.add("Java"); languages.add("Python"); languages.add("JavaScript"); languages.add("Java"); System.out.println("LinkedList: " + languages); // change elements at index 3 languages.set(3, "Kotlin"); System.out.println("Updated LinkedList: " + languages); ) )
आउटपुट
लिंक्डलिस्ट: (जावा, पायथन, जावास्क्रिप्ट, जावा) लिंक्डलिस्ट अपडेट किया गया: (जावा, पायथन, जावास्क्रिप्ट, कोटलिन)
उपरोक्त उदाहरण में, हमने एक लिंक्डलिस्ट नाम की भाषाएँ बनाई हैं। लाइन नोटिस करें,
languages.set(3, "Kotlin");
यहां, set()
विधि तत्व को इंडेक्स 3 से कोटलिन में बदल देती है।
4. एक लिंक्डलिस्ट से तत्व निकालें
The remove()
method of the LinkedList
class is used to remove an element from the LinkedList. For example,
import java.util.LinkedList; class Main ( public static void main(String() args) ( LinkedList languages = new LinkedList(); // add elements in LinkedList languages.add("Java"); languages.add("Python"); languages.add("JavaScript"); languages.add("Kotlin"); System.out.println("LinkedList: " + languages); // remove elements from index 1 String str = languages.remove(1); System.out.println("Removed Element: " + str); System.out.println("Updated LinkedList: " + languages); ) )
Output
LinkedList: (Java, Python, JavaScript, Kotlin) Removed Element: Python New LinkedList: (Java, JavaScript, Kotlin)
Here, the remove()
method takes the index number as the parameter. And, removes the element specified by the index number.
To learn more about removing elements from the linkedlist, visit the Java program to remove elements from LinkedList…
Other Methods
Methods | Description |
---|---|
contains() | checks if the LinkedList contains the element |
indexOf() | returns the index of the first occurrence of the element |
lastIndexOf() | returns the index of the last occurrence of the element |
clear() | removes all the elements of the LinkedList |
iterator() | returns an iterator to iterate over LinkedList |
LinkedList as Deque and Queue
Since the LinkedList
class also implements the Queue and the Deque interface, it can implement methods of these interfaces as well. Here are some of the commonly used methods:
Methods | Descriptions |
---|---|
addFirst() | adds the specified element at the beginning of the linked list |
addLast() | adds the specified element at the end of the linked list |
getFirst() | returns the first element |
getLast() | returns the last element |
removeFirst() | removes the first element |
removeLast() | removes the last element |
peek() | returns the first element (head) of the linked list |
poll() | returns and removes the first element from the linked list |
offer() | adds the specified element at the end of the linked list |
Example: Java LinkedList as Queue
import java.util.LinkedList; import java.util.Queue; class Main ( public static void main(String() args) ( Queue languages = new LinkedList(); // add elements languages.add("Python"); languages.add("Java"); languages.add("C"); System.out.println("LinkedList: " + languages); // access the first element String str1 = languages.peek(); System.out.println("Accessed Element: " + str1); // access and remove the first element String str2 = languages.poll(); System.out.println("Removed Element: " + str2); System.out.println("LinkedList after poll(): " + languages); // add element at the end languages.offer("Swift"); System.out.println("LinkedList after offer(): " + languages); ) )
Output
LinkedList: (Python, Java, C) Accessed Element: Python Removed Element: Python LinkedList after poll(): (Java, C) LinkedList after offer(): (Java, C, Swift)
Example: LinkedList as Deque
import java.util.LinkedList; import java.util.Deque; class Main ( public static void main(String() args)( Deque animals = new LinkedList(); // add element at the beginning animals.add("Cow"); System.out.println("LinkedList: " + animals); animals.addFirst("Dog"); System.out.println("LinkedList after addFirst(): " + animals); // add elements at the end animals.addLast("Zebra"); System.out.println("LinkedList after addLast(): " + animals); // remove the first element animals.removeFirst(); System.out.println("LinkedList after removeFirst(): " + animals); // remove the last element animals.removeLast(); System.out.println("LinkedList after removeLast(): " + animals); ) )
Output
LinkedList: (Cow) LinkedList after addFirst(): (Dog, Cow) LinkedList after addLast(): (Dog, Cow, Zebra) LinkedList after removeFirst(): (Cow, Zebra) LinkedList after removeLast(): (Cow)
Iterating through LinkedList
We can use the Java for-each loop to iterate through LinkedList. For example,
import java.util.LinkedList; class Main ( public static void main(String() args) ( // Creating a linked list LinkedList animals = new LinkedList(); animals.add("Cow"); animals.add("Cat"); animals.add("Dog"); System.out.println("LinkedList: " + animals); // Using forEach loop System.out.println("Accessing linked list elements:"); for(String animal: animals) ( System.out.print(animal); System.out.print(", "); ) ) )
Output
LinkedList: (Cow, Cat, Dog) Accessing linked list elements: Cow, Cat, Dog,
LinkedList Vs. ArrayList
Both the Java ArrayList and LinkedList
implements the List
interface of the Collections
framework. However, there exists some difference between them.
LinkedList | ArrayList |
---|---|
Implements List , Queue , and Deque interfaces. | Implements List interface. |
Stores 3 values (previous address, data, and next address) in a single position. | एक ही स्थिति में एक एकल मूल्य रखता है। |
डबल-लिंक्ड सूची कार्यान्वयन प्रदान करता है। | एक resizable सरणी कार्यान्वयन प्रदान करता है। |
जब भी कोई तत्व जोड़ा जाता है, prev और next पता बदल दिया जाता है। | जब भी कोई तत्व जोड़ा जाता है, तो उस स्थिति के बाद के सभी तत्व स्थानांतरित हो जाते हैं। |
किसी तत्व तक पहुंचने के लिए, हमें शुरुआत से तत्व तक चलना होगा। | इंडेक्स का उपयोग करके तत्वों को बेतरतीब ढंग से एक्सेस कर सकते हैं। |
नोट : हम जावा में इंटरफेस का उपयोग करके एक लिंक्डलिस्ट भी बना सकते हैं। उदाहरण के लिए,
// create linkedlist using List List animals1 = new LinkedList(); // creating linkedlist using Queue Queue animals2 = new LinkedList(); // creating linkedlist using Deque Deque animals3 = new LinkedList();
यहाँ, यदि LinkedList एक इंटरफ़ेस का उपयोग करके बनाया गया है, तो हम अन्य इंटरफेस द्वारा प्रदान किए गए तरीकों का उपयोग नहीं कर सकते हैं। यही है, जानवरों 1 विशिष्ट Queue
और Deque
इंटरफेस के तरीकों का उपयोग नहीं कर सकता है।