Cyberithub

Useful LinkedList Java(v1.8) Programming Examples

Advertisements

In this tutorial, I will take you through LinkedList Java Programming Examples. A Linked List considered as data structure similar to any other data structures like arrays, stacks and queues.

Linked List will have three major parts:-
a)Head
b)Nodes
c)Tail

Every node in the Linked List is interconnected using the address of the next node. We have three types of Linked Lists.

Singly Linked List

A Single Linked usually contains data of the current node and address of the next node.

Doubly Linked List

A doubly Linked List contains data of the node, address of the previous node and address of the next node.

Circular Linked List

A circular linked list is more like a singly linked list except the fact that in circular linked list last node contains address of the first node.

 

Useful LinkedList Java(v1.8) Programming Examples

LinkedList Java Class

Also Read: OpenJDK Installation on CentOS 7 with Easy Steps

boolean add(Object o)

You may want to use add() method of LinkedList Java Class to add the Integer data in Linked List as shown in below program.

import java.util.LinkedList;

class LinkList
{
  public static void main(String[] args)
  {
   LinkedList<Integer> l = new LinkedList<Integer>();
   l.add(8);
   l.add(7);
   System.out.println("Added Objects in the List are:" + l);
  }
}

Output

Added Objects in the List are:[8, 7]

void addFirst(Object o)

If you want to add data in the first position of a Linked List, then you need to use addFirst() method of LinkedList Java Class.

import java.util.LinkedList;

class LinkList
{
  public static void main(String[] args)
  { 
   LinkedList<Integer> l = new LinkedList<Integer>();
   l.add(8);
   l.add(7);
   System.out.println("Current List of Elements are:" + l);
   l.addFirst(9);
   System.out.println("Added Object in the First Position:" + l);
  }
}

Output

Current List of Elements are:[8, 7]
Add Object in the First Position:[9, 8, 7]

void addLast(Object o)

If you want to add data in last index of a Linked List, then you need to use addLast() method of LinkedList Java Class.

import java.util.LinkedList;

class LinkList
{
 public static void main(String[] args)
 {
  LinkedList<Integer> l = new LinkedList<Integer>();
  l.add(8);
  l.add(7);
  System.out.println("Current List of Elements are:" + l);
  l.addLast(10);
  System.out.println("Add Object in the Last Position:" + l);
 }
}

Output

Current List of Elements are:[8, 7]
Add Object in the Last Position:[8, 7, 10]

Object getFirst()

If you want to get the value of first index of a Linked List, then you need to use getFirst() method of LinkedList Java Class.

import java.util.LinkedList;

class LinkList
{
 public static void main(String[] args)
 {
  LinkedList<Integer> l = new LinkedList<Integer>();
  l.add(8);
  l.add(7);
  System.out.println("First Element is:" + l.getFirst());
 }
}

Output

First Element is: 8

Object getLast()

If you want to get the value of last index of a Linked List, then you can use getLast() function of LinkedList Java Class as shown in below example.

import java.util.LinkedList;

class LinkList
{
 public static void main(String[] args)
 {
  LinkedList<Integer> l = new LinkedList<Integer>();
  l.add(8);
  l.add(7);
  System.out.println("Last Element is:" + l.getLast());
 }
}

Output

Last Element is: 7

int indexOf(Object o)

If you want to find out the index of any element, you can find it by using indexOf() function of LinkedList Java Class.

import java.util.LinkedList;

class LinkList
{
 public static void main(String[] args)
{
 LinkedList<Integer> l = new LinkedList<Integer>();
 l.add(8);
 l.add(7);
 l.add(10);
 System.out.println("Index of Element 10 is: " + l.indexOf(10));
 }
}

Output

Index of Element 10 is: 2

Object remove(int index)

If you want to remove any element by its index number, then you can pass the index number in remove() function and remove that element as shown in the below example.

import java.util.LinkedList;

class LinkList
{
 public static void main(String[] args)
 {
  LinkedList<Integer> l = new LinkedList<Integer>();
  l.add(8);
  l.add(7);
  System.out.println("List of Elements: " + l);
  l.remove(1);
  System.out.println("List after removing element: " + l);
 }
}

Output

List of Elements: [8, 7]
List after removing element: [8]

Object removeFirst()

If you want to remove First Element of a Linked List, then you can remove it by using removeFirst() method as shown in the below example program.

import java.util.LinkedList;

class LinkList
{
 public static void main(String[] args)
 {
  LinkedList<Integer> l = new LinkedList<Integer>();
  l.add(8);
  l.add(7);
  System.out.println("Remove First Element:" + l.removeFirst());
  System.out.println("List After Removing First Element:" + l);
 }
}

Output

Remove First Element:8
List After Removing First Element:[7]

Object removeLast()

If you want to remove the last element of a LinkedList, then you need to use the removeLast() method LinkedList Java Class as shown in the below example.

import java.util.LinkedList;

class LinkList
{
 public static void main(String[] args)
 {
  LinkedList<Integer> l = new LinkedList<Integer>();
  l.add(8);
  l.add(7);
  System.out.println("Remove Last Element:" + l.removeLast());
  System.out.println("List After Removing Last Element:" + l);
 }
}

Output

Remove Last Element:7
List After Removing Last Element:[8]

 

Also Read: Java Interview Questions

Leave a Comment