Javatpoint Logo
Javatpoint Logo

Generic LinkedList in Java

In computer science, LinkedLists are a common data structure that are frequently used to store and manage collections of data. A LinkedList is made up of nodes, each of which has a value and a connection to the node after it in the list. There are various different varieties of LinkedLists, including single, double, and circular LinkedLists. This article will go over how a generic LinkedList is implemented in Java.

Any sort of data can be stored in a generic LinkedList, which is a type of LinkedList. This is accomplished in Java by leveraging the generics idea. In Java 5, generics were added to offer type safety and do away with casting. We can declare the kind of data that a class can handle with generics at build time.

We must first develop a Node class that corresponds to a LinkedList node in order to construct a generic LinkedList in Java. Two instance variables will be present in the Node class: one to hold the data and the other to hold a reference to the subsequent node in the list.

A Node class that is generic over the type T has been defined in the code above. The class has two instance variables: next and data, which both keep references to the next node in the list and store data of type T. The constructor receives an argument of type T and initialises the data variable with the supplied value before setting the subsequent variable to null.

The logic to manipulate the LinkedList must then be defined in a class called LinkedList, which needs to be defined next. The head of the list, which is a reference to the first node in the list, will be stored in a single instance variable of the LinkedList class.

In the code above, we have constructed a method called add that adds a new node to the end of the list and accepts a parameter of type T. The method first generates a new node with the value of the parameter before determining whether the list is empty. The new node is set as the head if the list is empty. If the list contains any items, the procedure goes along the list until it reaches the last node, at which point it sets the final node's subsequent reference to the new node.

In the code above, we have constructed a method called add that adds a new node to the end of the list and accepts a parameter of type T. The method first generates a new node with the value of the parameter before determining whether the list is empty. The new node is set as the head if the list is empty. If the list contains any items, the procedure goes along the list until it reaches the last node, at which point it sets the final node's subsequent reference to the new node.

Next, we need a method to remove a node from the list by its data value.

The remove method in the code above takes an argument of type T and removes the first node in the list that has the specified data value. The method determines whether the list is empty first and returns true if it is. The function ends and the head is set to the next node if the head node contains the specified data value. When the requested data value cannot be found in the head node, the procedure iterates over the list until it either finds a node that contains the requested data value or reaches the end of the list. The procedure removes the node if the node with the specified data value is located by updating the next reference of the preceding node t the next node.

Finally, we need a method to print the contents of the LinkedList.

We have defined a method called print in the code above, and it outputs the data for each node in the LinkedList. The method first establishes a reference to the list's head and then iterates through the list, publishing the data associated with each node until it reaches the end.

We can use a generic LinkedList to store and manage collections of data of any type now that the fundamental functionality of one has been provided in Java. To utilise the standard LinkedList, follow this example:

In the code above, a new LinkedList of type String was constructed, five strings were added to the list, its contents were printed, the string "how" was then deleted from the list, and the list's contents were printed once more.

Here is the complete code for a generic LinkedList in Java:

LinkedListExample.java

Output:

hello world how are you 
hello world are you 

In the code above, a new LinkedList of type String was created, five strings were added to it, its contents were printed, the string "how" was then deleted from the list, and the list's contents were printed once more. As you can see, the result displays both the LinkedList's initial contents and its contents once the string "how" has been removed.

In Conclusion, A powerful data structure that can store and manage collections of data of any type is a generic LinkedList. We can make a type-safe generic LinkedList in Java without the use of casting by using generics. A generic LinkedList is easily implemented in Java and has a wide range of uses.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA