Hello guys, if you are wondering how to add an element at the start and at the end of a linked list in Java then you are at the right place. LinkedList is an implementation of linked list data structure in Java and whenever you need a linked list you can use this. It also provides convenient methods to add elements like addFirst() to add any element at the fist position and addLast() to add element at the last position or tail of the linked list. Since LinkedList also implement List and Queue interface in Java, you can also use the common methods defined in the List and Queue interface to interact with LinkedList object in Java. In the past, I have also shared how to sort a LinkedList in Java and in this article, I will teach how you can use LinkedList in Java and how you can add object at the head and tail position of a linked list in Java.
How to add element at first and last position in linked list
Following example shows how to add an element at the first and last position of a linked list by using addFirst() and addLast() method of Linked List class.package test; import java.util.LinkedList; /** * Java program to add element at first and last position of linked list. */ public class Test { public static void main(String args[]) { LinkedList programmingLanguages = new LinkedList<>(); programmingLanguages.add("Java"); programmingLanguages.add("Perl"); programmingLanguages.add("Ruby"); programmingLanguages.add("Python"); programmingLanguages.add("C"); programmingLanguages.add("C++"); programmingLanguages.add("C#"); programmingLanguages.add("Scala"); System.out.println("Linked List before addition :"); System.out.println(programmingLanguages); System.out.println("Adding 'haskell' at first position of linked list"); programmingLanguages.addFirst("Haskell"); System.out.println(programmingLanguages); System.out.println("Adding 'Lisp' at last position of linked list"); programmingLanguages.addLast("Lisp"); System.out.println(programmingLanguages); } } Output: Linked List before addition : [Java, Perl, Ruby, Python, C, C++, C#, Scala] Adding 'haskell' at first position of linked list [Haskell, Java, Perl, Ruby, Python, C, C++, C#, Scala] Adding 'Lisp' at last position of linked list [Haskell, Java, Perl, Ruby, Python, C, C++, C#, Scala, Lisp]
You can see that initially we had a linked list of 8 elements then we added Haskell at the first position and when we printed the list we can see that Haskell was there at the start and total number of elements was also increased to 9. After this we added Lisp at the end of the linked list and when we printed it was present at the tail and total number of element was 10.
That's all about how to add element at the start and at the end of a linked list in Java. While there are not many scenarios where you would need to use LinkedList as most of the time you will be using ArrayList, its important to know about this class. If you have to keep adding elements into a data structure then using a LinkedList is also a great option.
Other Java LinkedList articles you may like to read
Thanks for reading this Java LinkedList tutorial so far. If you find this tutorial use then please share with your friends and colleagues. If you have any doubt then please drop a note.
No comments :
Post a Comment