LinkedList in Java
Linked lists are among the simplest and most common data structures . Arrays and Linked lists are similar since they both store collections of data. An array allocates memory for all its elements lumped together as one block of memory. In contrast, a linked list allocates space for each element separately in its own block of memory called a Node. The main disadvantage of using arrays to store data is that arrays are static structures and therefore cannot be easily extended or reduced to fit the data set. One disadvantage of a linked list over an array is that it does not allow direct access to the individual elements. If you want to access a particular item then you have to start at the head and follow the link until you get to that item.
Linked list is a data structure consisting of a group of nodes which together represent a sequence. It is dynamic in nature which allocates the memory when required. That means, the number of nodes in a list is not fixed and can grow and shrink on demand. Any application which has to deal with an unknown number of objects is a must to use a linked list. Each node in a Linked list contains two fields: a "data" field to store whatever element type the list holds for its client, and a "next" field which is a pointer used to link one node to the next node. The last node in the list has its .next field set to NULL to signify the end of the list.
There are three types of linked-list. They are:
- Single Linked List
- Doubly Linked List
- Circular Linked List
The following Java program illustrates several of the methods supported by this LinkedList collection Framework
import java.util.*;
class TestClass
{
public static void main (String[] args) throws java.lang.Exception
{
//create a new linked list object
LinkedList days = new LinkedList();
// add elements to the linked list
days.add("Monday");
days.add("Tuesday");
days.add("Wednesday");
days.add("Thursday");
days.addLast("Friday");
//Display all the contents of the LinkedList
Iterator<String> itr=days.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
/*Add First and Last Element in linked list*/
days.addFirst("Sunday");
days.addLast("Saturday");
System.out.println("After Addition: " + days);
//Insert an element in linked list
days.add(0, "Days in a Week"); //add start of linked list
days.add(4,"Middle"); //add in the middle of linked list
days.add(9, "End"); //add in the lst of linked list
System.out.println("After Insertion: " + days);
//remove
days.removeFirst();
days.remove(3); //remove forth element from list
days.removeLast();
System.out.println("After Addition: " + days);
//Remove an element from linked list
days.remove("Monday"); //remove monday from list
//System.out.println("Contents : " + days);
// Number of items in the linked list
int elements = days.size();
System.out.println("No. of elements linked list: " + elements);
// Finding elements in the linked list
boolean check = days.contains("Sunday");
if(check)
System.out.println("Item found !!");
else
System.out.println("Not in the list!! ");
// update linked list or get and set a value in a linked list
Object item = days.get(0);
days.set(0, (String) item + "-First Day");
System.out.println("After modification : " + days);
}
}
Related Topics