Linked list with java program
Operating System. Computer Network. Compiler Design. Computer Organization. Discrete Mathematics. Ethical Hacking. Computer Graphics. Software Engineering. Web Technology. Cyber Security. C Programming. Control System. Data Mining. Data Warehouse. Javatpoint Services JavaTpoint offers too many high quality services. A node contains two fields i. The last node of the list contains pointer to the null.
Uses of Linked List The list is not required to be contiguously present in the memory. The node can reside any where in the memory and linked together to make a list.
This achieves optimized utilization of space. Empty node can not be present in the linked list. We can store values of primitive types or objects in the singly linked list. Why use linked list over array? Array contains following limitations: The size of array must be known in advance before using it in the program.
Increasing size of the array is a time taking process. It is almost impossible to expand the size of the array at run time. Web Technology. Cyber Security. C Programming. Control System. Data Mining. Data Warehouse. Javatpoint Services JavaTpoint offers too many high quality services. Algorithm Create a class Node which has two attributes: data and next. Next is a pointer to the next node.
Create another class which has two attributes: head and tail. It first checks, whether the head is equal to null which means the list is empty. If the list is empty, both head and tail will point to the newly added node. If the list is not empty, the new node will be added to end of the list such that tail's next will point to the newly added node. This new node will become the new tail of the list. Collection Framework. Multi-threading in Java.
Table of Contents. Improve Article. Save Article. Like Article. Previous Stack Class in Java. Next Queue Interface In Java. Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments. In the above example, we have created a LinkedList named animals.
Here, we have used the add method to add elements to animals. Here, we have used the index number parameter. It is an optional parameter that specifies the position where the new element is added.
The get method of the LinkedList class is used to access an element from the LinkedList. In the above example, we have used the get method with parameter 1. Here, the method returns the element at index 1. We can also access elements of the LinkedList using the iterator and the listIterator method. To learn more, visit the Java program to access elements of LinkedList. The set method of LinkedList class is used to change elements of the LinkedList. Here, the set method changes the element at index 3 to Kotlin.
The remove method of the LinkedList class is used to remove an element from the LinkedList. Here, the remove method takes the index number as the parameter. And, removes the element specified by the index number.
To learn more about removing elements from the linkedlist, visit the Java program to remove elements from LinkedList. Since the LinkedList class also implements the Queue and the Deque interface, it can implement methods of these interfaces as well.
Here are some of the commonly used methods:. We can use the Java for-each loop to iterate through LinkedList. However, there exists some difference between them. Here, if the LinkedList is created using one interface, then we cannot use methods provided by other interfaces.
0コメント