Algorithm


Algorithm to Access an Element at a Specific Index:

Function: accessElementAtIndex(LinkedList list, int index)
Input: A LinkedList 'list' and an integer 'index' representing the position of the element to be accessed
Output: Print the element at the specified index or print an error message if the index is invalid

1. If index is greater than or equal to 0 and less than the size of the list, then do the following:
    1.1 Get the element at the specified index using list.get(index)
    1.2 Print the element
2. Else, print an error message indicating that the index is invalid

Algorithm to Access an Element Using ListIterator:

Function: accessElementUsingIterator(LinkedList list, Object targetElement)
Input: A LinkedList 'list' and an object 'targetElement' representing the element to be found
Output: Print the found element using ListIterator or print a message if the element is not found

1. Create a ListIterator for the LinkedList using list.listIterator()
2. While the iterator has a next element, do the following:
    2.1 Get the next element using iterator.next()
    2.2 If the current element is equal to the target element, then print the element and return
3. If the target element is not found, print a message indicating that the element is

Code Examples

#1 Code Example- Java Programing Access elements from a linkedlist

Code - Java Programming

import java.util.LinkedList;

class Main {
  public static void main(String[] args) {
    LinkedList<String> languages = new LinkedList < >();

    // add elements in the LinkedList
    languages.add("Python");
    languages.add("Java");
    languages.add("JavaScript");
    System.out.println("LinkedList: " + languages);

    // get the element from the LinkedList
    String str = languages.get(1);
    System.out.print("Element at index 1: " + str);
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
LinkedList: [Python, Java, JavaScript]
Element at index 1: Java

#2 Code Example- Java Programing Using iterator() method

Code - Java Programming

import java.util.LinkedList;
import java.util.Iterator;

class Main {
    public static void main(String[] args) {
        LinkedList<String> animals= new LinkedList < >();

        // Add elements in LinkedList
        animals.add("Lion");
        animals.add("Horse");
        animals.add("Cat");

        // Creating an object of Iterator
        Iterator<String> iterate = animals.iterator();
        System.out.print("LinkedList: ");

        while(iterate.hasNext()) {
            System.out.print(iterate.next());
            System.out.print(", ");
        }
    }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
LinkedList: Lion, Cat, Horse,

#3 Code Example- Java programing Using listIterator() method

Code - Java Programming

import java.util.LinkedList;
import java.util.ListIterator;

class Main {
    public static void main(String[] args) {
        LinkedList<String> animals= new LinkedList < >();

        // Add elements in LinkedList
        animals.add("Lion");
        animals.add("Horse");
        animals.add("Cat");

        // Create an object of ListIterator
        ListIterator<String> listIterate = animals.listIterator();
        System.out.print("LinkedList: ");

        while(listIterate.hasNext()) {
            System.out.print(listIterate.next());
            System.out.print(", ");
        }

        // Iterate backward
        System.out.print("\nReverse LinkedList: ");

        while(listIterate.hasPrevious()) {
            System.out.print(listIterate.previous());
            System.out.print(", ");
        }
    }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
LinkedList: Lion, Horse, Cat,
Reverse LinkedList: Cat, Horse, Lion,
Advertisements

Demonstration


Java Programing Example to Access elements from a LinkedList-DevsEnv