import java.util.*;
public class LinkedList_TechSarvam {
public static void main(String args[]) {
LinkedList<String> al = new LinkedList<String>(); // Create a LinkedList of Strings
al.add("Rajendra"); // Adding object in LinkedList
al.add("Mahendra");
al.add("Raja");
al.add("TechSarvam");
// Creating an iterator to traverse the LinkedList
Iterator<String> itr = al.iterator();
// Iterating through the LinkedList using the iterator
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
}
Step-by-Step Explanation:
1. Class Declaration:
public class LinkedList_TechSarvam {
The program consists of a main class named LinkedList_TechSarvam, which would be used to show how a LinkedList is implemented and how elements could be iterated over with an iterator.
2. Main Method:
public static void main(String args[]) {
It is the entry point of the program, where execution starts at the main() method.
3. Creating LinkedList
LinkedList<String> al = new LinkedList<String>();
- Declaring a linked list: There’s a declaration for a LinkedList called al to store String values.
- The LinkedList class is a Java util package where elements are not stored in continuous memory. The insertion and deletion of elements would be easier and faster compared with arrays.
4. Adding Elements to the LinkedList:
al.add(“Rajendra”);
al.add(“Mahendra”);
al.add(“Raja”);
al.add(“TechSarvam”);
- Four strings are added to the LinkedList using the add() method. These strings are “Rajendra”, “Mahendra”, “Raja”, and “TechSarvam”.
- After these additions, the list al will be: [“Rajendra”, “Mahendra”, “Raja”, “TechSarvam”].
5. Iterator Creation
Iterator<String> itr = al.iterator();
An iterator is created on the LinkedList through the iterator() method. This iterator lets us traverse the list without an index. It includes methods such as hasNext() and next() that are used in iterating over a collection.
6. Traversing through the LinkedList:
while (itr.hasNext()) {
System.out.println(itr.next());
}
- The while loop executes on the LinkedList until all elements within it have been used up (that is, itr.hasNext() returns false).
- In each iteration of the while loop, the method itr.next() retrieves the next element from the list, while System.out.println() prints that retrieved element.
- The loop will continue running and printing the elements within the LinkedList until all of them have printed.
Output of the Program:
When you execute the program, it will print the following output:
Rajendra
Mahendra
Raja
TechSarvam
Output Explanation:
All the strings of LinkedList are printed in new lines. Iterator starts at the beginning of the list i.e., “Rajendra” and then iterates each next element, “Mahendra”, “Raja”, “TechSarvam”, and prints them one after another till all the elements are traversed in the list.
Important Points:
1. LinkedList: A class in Java that implements the List interface and stores elements in a doubly linked list. It allows for fast insertions and deletions, especially at the beginning or end of the list.
2. add(): The add() method is used to add elements to the LinkedList.
3. Iterator: An interface that provides methods to traverse the elements of a collection. It is used here to traverse the LinkedList without the need for indexing.
4. hasNext(): This method checks if there are more elements to iterate over.
5. next(): This method returns the next element in the collection.
Example of Iterating through a LinkedList:
LinkedList<String> list = new LinkedList<String>();
list.add(“Rajendra”);
Iterator<String> itr = list.iterator();
while (itr.hasNext())
System.out.println(itr.next()); // Prints each element in the LinkedList
}
This program demonstrates how to use a LinkedList and an Iterator to traverse the list and print its contents.