LinkedList is a class implementation of the LinkedList data structure which is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. As we all know that class contains various methods so do here we will be discussing and implementing add() method to grasp a better understanding of how elements are added in a LinkedList.
For that refer to the below flowchart to get a better understanding of any methods. Note that it is very important to go through flowcharts in programming . Here two cases arises that is default addition of elements and custom addition of elements. Here we will be covering both of them as follows:
Case 1: Default addition by adding at last of List
This method appends the specified element to the end of this list. This function accepts a single parameter element as shown in the above syntax
Syntax:
boolean add(Object element)
Parameters: The element specified by this parameter is appended to the end of the list.
Return Value: Boolean true after execution.
Example:
// Java Program to Illustrate add() Method // of LinkedList class // Where we are Adding at Last of List // Importing required classes import java.io.*;
import java.util.LinkedList;
// Main class public class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an empty LinkedList
LinkedList list = new LinkedList();
// Adding elements in the list
// using add() method
list.add( "Geeks" );
list.add( "for" );
list.add( "Geeks" );
list.add( "10" );
list.add( "20" );
// Printing the elements of current LinkedList
System.out.println( "The list is:" + list);
// Adding new elements to the end
// Note: Default addition happens from last
list.add( "Last" );
list.add( "Element" );
// Printing elements of updated LinkedList
System.out.println( "The new List is:" + list);
}
} |
Output:
The list is:[Geeks, for, Geeks, 10, 20] The new List is:[Geeks, for, Geeks, 10, 20, Last, Element]
Case 2: Adding at the specified index
This method inserts an element at a specified index in the list. It shifts the element currently at that position (if any) and any subsequent elements to the right (will add one to their indices).
Syntax:
void add(int index, Object element)
Parameters: This method accepts two parameters as described below.
- index: The index at which the specified element is to be inserted.
- element: The element which is needed to be inserted.
Return Value: Boolean true after execution.
Example:
// Java Program to Illustrate add() Method // of LinkedList class // Adding at a Specified Index // Importing required classes import java.util.LinkedList;
import java.io.*;
// Main class class GFG {
// Main driver method
public static void main(String[] args) {
// Creating an empty LinkedList of string type
LinkedList<String> ll = new LinkedList();
// Adding elements to LinkedList
// using add() method
ll.add( "Geeks" );
ll.add( "For" );
ll.add( "Geeks" );
// Printing the current elements of LinkedList
System.out.println(ll);
// Adding element at a particular index
// by passing as an argument
ll.add( 2 , "Java" );
// Printing the updated elements of LinkedList
// after insertion at specified index
System.out.println(ll);
}
} |
Output:
[Geeks, For, Geeks] [Geeks, For, Java, Geeks]