Java LinkedList example

The LinkedList implements the List interface.We have stores collections of data in LinkedList.The linked list is a data structure which can change during execution.

Java LinkedList example

The LinkedList implements the List interface.We have stores collections of data in LinkedList.The linked list is a data structure which can change during execution.

Java LinkedList example

Java LinkedList example

The LinkedList implements the List interface.We have stores collections of data in LinkedList.The linked list is a data structure which can change during execution.The LinkedList class provides uniformly named method to remove, and insert an element at the beginning and end of the list. Linkedlist has many methods. These are add(elem), add(index, elem), get(index),  remove(index), and indexOf(elem) as well as creating point for LinkedList

Commonly operations performed on LinkedList are :-

  • List creation
  • Inserting element to the list.
  • Removing an element from the list.

List creation :-We have created a LinkedList object :-

Syntax :-

public void run{

LinkedList.<Integer> list = new LinkedList<Integer>();

}

Inserting element to the list:-The record is created holding the new items. The element inserting add() method and add pass by value string and Integer.

Syntax
public void run(){
LinkedList list = new LinkedList();
         list.add(1234);
         list.add(123456);

 

Removing an element :-The removing an element use for remove() method and pass by value string or index.

Syntax :-

  • remove();

Example:-

package Collection;

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

public class LinkedListExample {
	public static void main(String[] args) {
		List list = new LinkedList();
                list.add(6);
		list.add(5);
		list.add(4);
		list.add(3);
		list.add(2);
		list.add(1);
		list.add(0);

	System.out.println("Size :" + list.size());
		list.remove(6);// remove method
		Iterator it = list.iterator();
		while (it.hasNext())
			System.out.println(it.next());
		System.out.println("Size :" + list.size());
	}
}

OutPut:-

Download Source Code