In Java, LinkedList class implements the list interface.

This class consists of the following methods :

1. boolean add(Object element) : It appends the element to the end of the list.

2. void add(int index, Object element): It inserts the element at the position ‘index’ in the list.

3. void addFirst(Object element) : It inserts the element at the beginning of the list.

4. void addLast(Object element) : It appends the element at the end of the list.

5. boolean contains(Object element) : It returns true if the element is present in the list.

6. Object get(int index) : It returns the element at the position ‘index’ in the list. It throws ‘IndexOutOfBoundsException’ if the index is out of range of the list.

7. int indexOf(Object element) : If element is found, it returns the index of the first occurrence of the element. Else, it returns -1.

8. Object remove(int index) : It removes the element at the position ‘index’ in this list. It throws ‘NoSuchElementException’ if the list is empty.

9. int size() : It returns the number of elements in this list.

10. void clear() : It removes all of the elements from the list.

[ad type=”banner”]

Java programming

[pastacode lang=”java” manual=”%2F%2F%20Java%20code%20for%20Linked%20List%20implementation%0A%20%0Aimport%20java.util.*%3B%0A%20%0Apublic%20class%20Test%0A%7B%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Creating%20object%20of%20class%20linked%20list%0A%20%20%20%20%20%20%20%20LinkedList%3CString%3E%20object%20%3D%20new%20LinkedList%3CString%3E()%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Adding%20elements%20to%20the%20linked%20list%0A%20%20%20%20%20%20%20%20object.add(%22A%22)%3B%0A%20%20%20%20%20%20%20%20object.add(%22B%22)%3B%0A%20%20%20%20%20%20%20%20object.addLast(%22C%22)%3B%0A%20%20%20%20%20%20%20%20object.addFirst(%22D%22)%3B%0A%20%20%20%20%20%20%20%20object.add(2%2C%20%22E%22)%3B%0A%20%20%20%20%20%20%20%20object.add(%22F%22)%3B%0A%20%20%20%20%20%20%20%20object.add(%22G%22)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22Linked%20list%20%3A%20%22%20%2B%20object)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Removing%20elements%20from%20the%20linked%20list%0A%20%20%20%20%20%20%20%20object.remove(%22B%22)%3B%0A%20%20%20%20%20%20%20%20object.remove(3)%3B%0A%20%20%20%20%20%20%20%20object.removeFirst()%3B%0A%20%20%20%20%20%20%20%20object.removeLast()%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22Linked%20list%20after%20deletion%3A%20%22%20%2B%20object)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Finding%20elements%20in%20the%20linked%20list%0A%20%20%20%20%20%20%20%20boolean%20status%20%3D%20object.contains(%22E%22)%3B%0A%20%0A%20%20%20%20%20%20%20%20if(status)%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22List%20contains%20the%20element%20’E’%20%22)%3B%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22List%20doesn’t%20contain%20the%20element%20’E’%22)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Number%20of%20elements%20in%20the%20linked%20list%0A%20%20%20%20%20%20%20%20int%20size%20%3D%20object.size()%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22Size%20of%20linked%20list%20%3D%20%22%20%2B%20size)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Get%20and%20set%20elements%20from%20linked%20list%0A%20%20%20%20%20%20%20%20Object%20element%20%3D%20object.get(2)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22Element%20returned%20by%20get()%20%3A%20%22%20%2B%20element)%3B%0A%20%20%20%20%20%20%20%20object.set(2%2C%20%22Y%22)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22Linked%20list%20after%20change%20%3A%20%22%20%2B%20object)%3B%0A%20%20%20%20%7D%0A%7D” message=”” highlight=”” provider=”manual”/]

Output :

Linked list : [D, A, E, B, C, F, G]
Linked list after deletion: [A, E, F]
List contains the element 'E' 
Size of linked list = 3
Element returned by get() : F
Linked list after change : [A, E, Y]
[ad type=”banner”]