So in continuation of JAVA Collections series today we are going to talk about LinkedLst.

 Collection interface get implemented by LinkedList which uses a doubly linked list internally to store the elements.

like ArrayList It can also store the duplicate elements but It maintains the insertion order and is not synchronized.

Benefit if using LinkedList is that manipulation is fast because no shifting is required.

Example of LinkedList :-

import java.util.*;  
public class TestJavaCollection2{  
public static void main(String args[]){  
LinkedList<String> linkedList=new LinkedList<String>();  
linkedList.add("BlogPost1");  
linkedList.add("BlogPost2");  
linkedList.add("BlogPost1");  // it allows duplicate element
linkedList.add("BlogPost3");  
Iterator<String> itr=linkedList.iterator();  
while(itr.hasNext()){  
System.out.println(itr.next());  
}  
}  
}  

output of above program will be

BlogPost1
BlogPost2
BlogPost1
BlogPost3

Lets see one program of LinkedList

Implementation of LinkedList in JAVA

import java.io.*; 

// Simple Java program to implement 
a Singly Linked List 
public class LinkedList { 

	Node head; 
 // head of list 

//Linked list Node 
// This inner class is made static 
so that main() can access it 
	static class Node { 

	int data; 
	Node next; 

	// here we have Constructor 
	Node(int d) 
	{ 
		data = d; 
		next = null; 
	} 
	} 

	// here we have method to insert a new node 
	public static LinkedList insert(LinkedList list, int data) 
	{ 
// demonstration of Creation of a new node with given data 
		Node new_node = new Node(data); 
		new_node.next = null; 

		// If the Linked List is empty, 
		// then make the new node as head 
		if (list.head == null) { 
			list.head = new_node; 
		} 
		else { 
			// Else traverse till the last node 
			// and insert the new_node there 
			Node last = list.head; 
			while (last.next != null) { 
				last = last.next; 
			} 

			// Insert the new_node at last node 
			last.next = new_node; 
		} 

		// Return the list by head 
		return list; 
	} 

	// Method to print the LinkedList. 
	public static void printList(LinkedList list) 
	{ 
		Node currNode = list.head; 

		System.out.print("LinkedList: "); 

	// this is how we Traverse through the LinkedList 
		while (currNode != null) { 
			// Print the data at current node 
			System.out.print(currNode.data + " "); 

			// Go to next node 
			currNode = currNode.next; 
		} 
	} 

	// main class where 
we will perform insertion operation
	public static void main(String[] args) 
	{ 
		/* Start with the empty list. */
		LinkedList list = new LinkedList(); 

		// 
		// ******INSERTION****** 
		// 

		// Insert the values 
		list = insert(list, 10); 
		list = insert(list, 20); 
		list = insert(list, 30); 
		list = insert(list, 40); 
		list = insert(list, 50); 
		list = insert(list, 60); 
		list = insert(list, 70); 
		list = insert(list, 80); 

		// simple way to Print the LinkedList 
		printList(list); 
	} 
} 

Output of above program will be

LinkedList: 1 2 3 4 5 6 7 8

Advantages of Linked List

  1. Dynamic Data Structure so it can grow and shrink at runtime by allocating and deallocating memory.
  2. Insertion and deletion of nodes become easy as here we don’t have to shift elements after insertion or deletion of an element.
  3. Since the size of the linked list can increase or decrease at run time so there is no memory wastage at all as memory is allocated only when required
We will discuss Vector in next post till then keep practicing LinkedList.

Link for next Collection series is below :- 

https://siriusq.com/collections-in-java-concepts-of-collections-3/

1 Comment

  1. All posts are amazing. You have shared a very informative article, it will help me a lot, I do not expect that we believe you will keep similar posts in future. Thanks a lot for the useful post and keep it up. You can get information about any type of fasting festival, Katha, birth anniversary of great men and birthday, national and international day of politicians, actors, cricketers etc. in Hindi from my blog.

Leave a Reply

Your email address will not be published. Required fields are marked *

Copy link