LinkedList in Java is linear data structures that are different from arrays. There are certain advantages as well as disadvantages of using Linked List in a Java program. Each element in a linkedlist is stored in a cell known as Node. Each node has a specific address. The main disadvantage of LinkedList is that the nodes are not easily accessible at each point.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
There are pointers that are used to address each node, and to access a specific node, there has to start from the head, and then the specific pointer to which the node is to be accessed is reached. The LinkedList class also consists of many constructors and methods like other Java interfaces. In this article, we are going to see two constructors which are being used in LinkedList.
They are:
There are many methods or functions which are part of the Java LinkedList class. We will see some of the functions that are part of the Java LinkedList class in this article.
They are:
Given below are the examples mentioned:
In this coding example, we are going to see the LinkedList methods of inserting certain elements in the linkedlist and then removing them, and finally displaying the linkedlist.
Code:
import java.util.*; public class Example3 { public static void main(String args[]) { LinkedList<String> object = new LinkedList<String>(); // Adding elements to the linked list object.add("A"); object.add("B"); object.addLast("C"); object.addFirst("D"); object.add(2, "E"); object.add("F"); object.add("G"); System.out.println("Linked list : " + object); object.remove("C"); object.remove(3); object.removeFirst(); object.removeLast(); System.out.println("Linked list after deletion: " + object); } }
Output:
In the sample output, we see that there are certain elements in the linkedlist, and finally, certain elements are deleted, and then the linkedlist after all the deletion of the elements is shown.
In this program, we are going to see four names being printed using sequential order in LinkedList. We use a String LinkedList and use it to print names that can be of any number. We use the While loop here for printing the names which are present in the program.
Code:
import java.util.*; public class LinkedList1 { public static void main(String args[]) { LinkedList<String> al=new LinkedList<String>(); al.add("Ishankamal Mitra"); al.add("Sourya Mukherjee"); al.add("Satyaki Das"); al.add("Debodooty Sarkar"); Iterator<String> itr=al.iterator(); while(itr.hasNext()) { System.out.println(itr.next()); } } }
Output:
In this program, we check how the coding helps us to print four names in sequential order as mentioned in the LinkedList. In the next program, we are going to see how the sequence is changed; that is, the names are printed in reverse order of the input.
In this code, the program inputs the name and then prints the names in the reverse order of their sequence.
Code:
import java.util.*; public class LinkedList4 { public static void main(String args[]) { LinkedList<String> ll=new LinkedList<String>(); ll.add("Ishankamal Mitra"); ll.add("Sourya Mukherjee"); ll.add("Satyaki Das"); //Going through the list of elements in Reverse order Iterator i=ll.descendingIterator(); while(i.hasNext()) { System.out.println(i.next()); } } }
Output:
In this program, we use the DescendingIterator(), and we use it to print the names in the reverse order of the input. We can see it very clearly through the program.
In this article, we saw the different constructors and methods which are present in the LinkedList class. Plus, we saw a Java program to illustrate the insertion and deletion of elements in a LinkedList. We also saw the advantages and disadvantages of using LinkedList over arrays. They contain nodes that are not easily accessible and have to be accessed through the LinkedList head. We also notice three examples of coding where names are printed in reverse order, sequential order, and removing elements from a LinkedList. These programs help us to understand the methodology of the LinkedList class.
The above is the detailed content of LinkedList in Java. For more information, please follow other related articles on the PHP Chinese website!