LinkedList
- Eclipse: Oxygen
- Java: 1.8
A LinkedList is considered as a data structure similar to any other data structure like arrays, stacks, and queues. Here, every node is interconnected to the next node through a memory address link.
A LinkedList has three elements namely:
- Head
- Nodes
- Tails
This is one of the most useful data structures and highly used in real-world scenarios because of its high-speed capacity and fast addition and deletion. This basic collection structure is the data in and the address which points to the next node functions as the guide of the respective node (That is the reason for it being called the Linked List). Several methods on this include the addition, deletion of a node anywhere in the list.
The following tutorial deals with the operations regarding the Linked Lists such as add, adding in the specific position, printing, and removing the elements from the specific position in the linked list.
add() method is used to add data in a newly created linked list. But if the linked list is already created then add() will add data at the end of the linked list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package org.studyeasy; import java.util.LinkedList; public class App { public static void main(String[] args) { LinkedList<String> countries = new LinkedList<>(); countries.add("India"); countries.add("USA"); countries.add("Poland"); countries.add("Egypt"); countries.add("Thailand"); countries.add("Russia"); new App().printList(countries); } void printList(LinkedList<String> list){ for(String element: list){ System.out.println("Element: "+element); } System.out.println("*****************************"); } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Element: India Element: USA Element: Poland Element: Egypt Element: Thailand Element: Russia ***************************** |
In the following example, using add(int index, Object item) method, data can be added at the specific position of the linked list. Eg. Canada is added at the second position in the countries list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public class App1 { public static void main(String[] args) { LinkedList<String> countries = new LinkedList<>(); countries.add("India"); countries.add("USA"); countries.add("Poland"); countries.add("Egypt"); countries.add("Thailand"); countries.add("Russia"); new App().printList(countries); countries.add(1, "Canada"); countries.add("Bragil"); new App().printList(countries); } void printList(LinkedList<String> list){ for(String element: list){ System.out.println("Element: "+element); } System.out.println("*****************************"); } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
Element: India Element: USA Element: Poland Element: Egypt Element: Thailand Element: Russia ***************************** Element: India Element: Canada Element: USA Element: Poland Element: Egypt Element: Thailand Element: Russia Element: Bragil ***************************** |
In the following example, linked data can be modified using set() method.
Eg. Data is modified at the 7th index position.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public class App { public static void main(String[] args) { LinkedList<String> countries = new LinkedList<>(); countries.add("India"); countries.add("USA"); countries.add("Poland"); countries.add("Egypt"); countries.add("Thailand"); countries.add("Russia"); new App().printList(countries); countries.add(1, "Canada"); countries.add("Bragil"); new App().printList(countries); countries.set(7, "Brazil"); new App().printList(countries); } void printList(LinkedList<String> list){ for(String element: list){ System.out.println("Element: "+element); } System.out.println("*****************************"); } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
Element: India Element: USA Element: Poland Element: Egypt Element: Thailand Element: Russia ***************************** Element: India Element: Canada Element: USA Element: Poland Element: Egypt Element: Thailand Element: Russia Element: Bragil ***************************** Element: India Element: Canada Element: USA Element: Poland Element: Egypt Element: Thailand Element: Russia Element: Brazil ***************************** |
In the following example, remove() method is used to remove the data from the specified location.
Eg, remove(4) data will be removed from the 4th index position.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public class App { public static void main(String[] args) { LinkedList<String> countries = new LinkedList<>(); countries.add("India"); countries.add("USA"); countries.add("Poland"); countries.add("Egypt"); countries.add("Thailand"); countries.add("Russia"); new App().printList(countries); countries.add(1, "Canada"); countries.add("Bragil"); new App().printList(countries); countries.set(7, "Brazil"); new App().printList(countries); countries.remove(4); new App().printList(countries); } void printList(LinkedList<String> list){ for(String element: list){ System.out.println("Element: "+element); } System.out.println("*****************************"); } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
Element: India Element: USA Element: Poland Element: Egypt Element: Thailand Element: Russia ***************************** Element: India Element: Canada Element: USA Element: Poland Element: Egypt Element: Thailand Element: Russia Element: Bragil ***************************** Element: India Element: Canada Element: USA Element: Poland Element: Egypt Element: Thailand Element: Russia Element: Brazil ***************************** Element: India Element: Canada Element: USA Element: Poland Element: Thailand Element: Russia Element: Brazil ***************************** |
Contributed by: Poonam Tomar