Open In App

Java Program to Implement LinkedList API

Last Updated : 26 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Linked List is a part of the Collection framework That is present in java.util package. This class is an implementation of the LinkedList data structure which is a linear data structure in which the elements are not stored in contiguous locations and every element is a separate object with a data part and address part.

What is a Linked List API?

  • Linked List API aims at Implementing Linked List Collection which as a part of collection framework inherited from java.util package.
  • This API is a double-linked list implementation of list and deque interfaces.
  • This API implements all optional list operations and permits all elements(including null).
  • All the operations perform as could be expected for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.
  • Below is the Source Code for the java program to Implement LinkedList Collection API.

Example:

Java




// Java Program to Implement LinkedList API
 
// Importing utility classes from java.util package
import java.util.*;
import java.util.Collection;
 
// Class
// Main class consisting of all methods
public class LinkedListImpl<E> {
 
    // Member variable of this class
    private LinkedList<E> linkedList;
 
    // Constructors of this class
 
    // 1. Default constructor
    public LinkedListImpl()
    {
        linkedList = new LinkedList<E>();
    }
 
    // 2. Parameterized constructor
    public LinkedListImpl(Collection<? extends E> c)
    {
        linkedList = new LinkedList<E>(c);
    }
 
    // Method 1
    // To append specified element to end of this List
    public boolean add(E e)
    {
 
        // Returning the last element from the List
        return linkedList.add(e);
    }
 
    // Method 2
    // To insert specified element at
    // the specified position in this List
    public void add(int index, E element)
    {
        linkedList.add(index, element);
    }
 
    // Method 3
    // To add all the elements in this List
    public boolean addAll(Collection<? extends E> c)
    {
        return linkedList.addAll(c);
    }
 
    // Method 4
    // To add all the elements in this List
    public boolean addAll(int index,
                          Collection<? extends E> c)
    {
        return linkedList.addAll(index, c);
    }
 
    // Method 5
    // to inserts specified element at beginning of this
    // List
    public void addFirst(E e) { linkedList.addFirst(e); }
 
    // Method 6
    // To appends specified element to end of this List
    public void addLast(E e) { linkedList.addLast(e); }
 
    // Method 7
    // Removes all of the elements from this list.
    public void clear() { linkedList.clear(); }
 
    // Method 8
    // Returns a shallow copy of this ArrayList instance.
    public Object clone() { return linkedList.clone(); }
 
    // Method 9
    // Returns true if this list contains the specified
    // element.
    public boolean contains(Object o)
    {
        return linkedList.contains(o);
    }
 
    // Method 10
    // Returns an iterator over the elements in this
    // deque(reverse order)
    public Iterator<E> descendingIterator()
    {
        return linkedList.descendingIterator();
    }
 
    // Method 11
    // Retrieves, but does not remove, the head (first
    // element) of this list.
    public E element() { return linkedList.element(); }
 
    // Method 12
    // Returns the element at the specified position in this
    // list.
    public E get(int index)
    {
        return linkedList.get(index);
    }
 
    // Method 13
    // Returns the first element in this list.
    public E getFirst() { return linkedList.getFirst(); }
 
    // Method 14
    // Returns the last element in this list.
    public E getLast() { return linkedList.getLast(); }
 
    // Method 15
    // Returns the index of the first occurrence of the
    // specified element
    public int indexOf(Object o)
    {
 
        return linkedList.indexOf(o);
    }
 
    // Method 16
    // Returns true if this list contains no elements.
    public boolean isEmpty()
    {
        return linkedList.isEmpty();
    }
 
    // Method 17
    // Returns an iterator over the elements
    //  in this list in proper sequence.
    public Iterator<E> iterator()
    {
        return linkedList.iterator();
    }
 
    // Method 18
    public int lastIndexOf(Object o)
    {
        return linkedList.lastIndexOf(o);
    }
 
    // Method 19
    public ListIterator<E> listIterator()
    {
        return linkedList.listIterator();
    }
 
    // Method 20
    public ListIterator<E> listIterator(int index)
    {
        return linkedList.listIterator(index);
    }
 
    // Method 21
    // Adds the specified element as the tail (last element)
    // of this list.
    public boolean offer(E e)
    {
        return linkedList.offer(e);
    }
 
    // Method 22
    // Inserts the specified element at the front of this
    // list.
    public boolean offerFirst(E e)
    {
        return linkedList.offerFirst(e);
    }
 
    // Method 23
    // Inserts the specified element at the end of this
    // list.
    public boolean offerLast(E e)
    {
        return linkedList.offerLast(e);
    }
 
    // Method 24
    // Retrieves, but does not remove, the head (first
    // element) of this list.
    public E peek() { return linkedList.peek(); }
 
    // Method 25
    public E peekFirst() { return linkedList.peekFirst(); }
 
    // Method 26
    // Retrieves, but does not remove, the last element of
    // this list
 
    public E peekLast() { return linkedList.peekLast(); }
 
    // Method 27
    // Retrieves and removes the head (first element) of
    // this list.
    public E poll() { return linkedList.poll(); }
 
    // Method 28
    // Retrieves and removes the first element of this list,
    // or returns null
    public E pollFirst() { return linkedList.pollFirst(); }
 
    // Method 29
    // Retrieves and removes the last element of this list,
    // or returns null
    public E pollLast() { return linkedList.peekLast(); }
 
    // Method 30
    // Pops an element from the stack represented by this
    // list.
    public E pop() { return linkedList.pop(); }
 
    // Method 31
    // Pushes an element onto the stack represented by this
    // list.
    public void push(E e) { linkedList.push(e); }
 
    // Method 32
    // Removes the element at the specified position in this
    // list.
    public E remove(int index)
    {
 
        return linkedList.remove(index);
    }
 
    // Method 33
    // Removes the first occurrence of the specified element
    // from this list(if present)
    public boolean remove(Object o)
    {
        return linkedList.remove(o);
    }
 
    // Method 34
    public boolean removeAll(Collection<?> c)
    {
 
        return linkedList.removeAll(c);
    }
 
    // Method 35
    // Removes and returns the first element from this list.
    public E removeFirst()
    {
 
        return linkedList.removeFirst();
    }
 
    // Method 36
    // To remove th first occurrences in this List
    public boolean removeFirstOccurrence(Object o)
    {
 
        return linkedList.removeFirstOccurrence(o);
    }
 
    // Method 37
    // Removes and returns the last element from this list.
    public E removeLast()
    {
 
        return linkedList.removeLast();
    }
 
    // Method 38
    // Removes the last occurrence of the specified element
    // in this list
    public boolean removeLastOccurrence(Object o)
    {
 
        return linkedList.removeLastOccurrence(o);
    }
 
    // Method 39
    // Retains only the elements in this list
    // contained in specific position
    public boolean retainAll(Collection<?> c)
    {
 
        return linkedList.removeAll(c);
    }
 
    // Method 40
    // Replaces the element at the specified position
    public E set(int index, E element)
    {
        return linkedList.set(index, element);
    }
 
    // Method 41
    // Returns the number of elements in this list.
    public int size() { return linkedList.size(); }
 
    // Method 42
    // Returns a view of the portion of this list
    public List<E> subList(int fromIndex, int toIndex)
    {
 
        return linkedList.subList(fromIndex, toIndex);
    }
 
    // Method 43
    // Returns an array containing all of the elements
    // in this list(proper sequence)
    public Object[] toArray()
    {
 
        return linkedList.toArray();
    }
 
    // Method 44
    // Returns an array containing all of the elements in
    // this list
    public <T> T[] toArray(T[] a)
    {
        return linkedList.toArray(a);
    }
 
    // Method 45
    // Main driver method
    public static void main(String... arg)
    {
 
        // Creating an object of above class
        // User-defined
        LinkedListImpl<Integer> linkedList
            = new LinkedListImpl<>();
 
        // Adding custom elements to above object
 
        // Custom input elements addition
        // using add() and addAll() methods
        linkedList.add(100);
        linkedList.add(20);
        linkedList.addFirst(101);
        linkedList.addLast(200);
 
        // Creating a Set class object of integer type
        Set<Integer> set = new HashSet<Integer>();
 
        // Custom input elements addition
        // using add() and addAll() methods
        set.add(101);
        set.add(30);
        set.add(32);
        linkedList.addAll(4, set);
 
        if (linkedList.contains(300))
            System.out.println(
                "the linked list contains 300");
        else
            System.out.println(
                "the linked list does not contain 300");
 
        System.out.println(
            "the elements in descending order is");
        Iterator<Integer> descendingitr
            = linkedList.descendingIterator();
        while (descendingitr.hasNext()) {
            System.out.print(descendingitr.next() + "\t");
        }
        System.out.println();
 
        System.out.println("the head of this list is "
                           + linkedList.element());
       
        System.out.println("the element at index 2 is "
                           + linkedList.get(2));
       
        System.out.println("the element first pos is  "
                           + linkedList.getFirst());
       
        System.out.println("the element at last pos is"
                           + linkedList.getLast());
 
        System.out.println("the index of element 200 is "
                           + linkedList.indexOf(200));
        System.out.println(
            "the last index of element 101 is "
            + linkedList.lastIndexOf(101));
 
        System.out.println("the elements of list are");
        Iterator<Integer> itr = linkedList.iterator();
       
        while (itr.hasNext()) {
            System.out.print(itr.next() + "\t");
        }
        System.out.println();
 
        linkedList.offer(45);
        linkedList.offerFirst(32);
        linkedList.offerLast(19);
 
        System.out.println("the head of the linkedlist is "
                           + linkedList.peek());
       
        System.out.println(
            "the first element of linkedList is "
            + linkedList.peekFirst());
       
        System.out.println(
            "the last element of linked List is "
            + linkedList.peekLast());
 
        System.out.println("the elements of list are");
        itr = linkedList.iterator();
       
        while (itr.hasNext()) {
            System.out.print(itr.next() + "\t");
        }
        System.out.println();
 
        System.out.println(
            "the first element of linkedList is (poll) "
            + linkedList.poll());
       
        System.out.println("the first element polled is "
                           + linkedList.pollFirst());
       
        System.out.println("the last element polled is "
                           + linkedList.pollLast());
       
        linkedList.push(36);
       
        System.out.println(
            "the element popped from linked List is "
            + linkedList.pop());
       
        System.out.println(
            "index 3 element removed from list "
            + linkedList.remove(3));
       
        System.out.println(
            "last occurrence of 101 removed "
            + linkedList.removeLastOccurrence(101));
       
        linkedList.clear();
       
        if (linkedList.isEmpty())
            System.out.println("the linkedList is empty");
        else
            System.out.println(
                "the linked list is not empty");
    }
}


 
 Output :  

 Program Explanation: 

  1. We create a Collection method LinkedListImpl<E> to invoke Linked List object.
  2. We create a boolean method add() to Append the specified element to the end of this list.
  3. We create a void add() method to Insert the specified element at the specified position in this list.
  4. We create a boolean addAll() method to Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s Iterator.
  5. We create a void addFirst() method to Insert the specified element at the beginning of this list.
  6. We create a void addLast() method to Appends the specified element to the end of this list.
  7. We create clear() method to Removes all of the elements from this list.
  8. We create a clone() method which Returns a shallow copy of this ArrayList instance.
  9. We create a contains() method which Returns Returns true if this list contains the specified element
  10. We create a descendingIterator() which Returns an iterator over the elements in this deque in reverse sequential order.
  11. We create element() method which Retrieves, but does not remove, the head (first element) of this list.
  12. We create getFirst() method which Returns the first element in this list.
  13. We create getLast() method which Returns the last element in this list.
  14. We create indexOf() method with object as parameter which Returns the index of the first occurrence of the specified element in list or -1 if this list does not contain the element.
  15. We create isEmpty() to return true if this list contains no elements.
  16. We create iterator() method for arrayList which Returns an iterator over the elements in this list in proper sequence.
  17. We create lastIndexOf() to Returns the index of the last occurrence of the specified element in this or else -1.
  18. We create listIterator() method for ArrayList listIterator<E> to Returns a list iterator over the elements in this list.
  19. We write listIterator(int index) to Returns a list iterator over the elements in this list.
  20. We write offer() method to Add the specified element as the tail (last element) of this list.
  21. We write offerFirst(E e) to  Inserts the specified element at the front of this list.

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads