Kotlin LinkedList
$count++; if($count == 1) { include "../mobilemenu.php"; } if ($count == 2) { include "../sharemediasubfolder.php"; } ?>
LinkedList provides a doubly linked list data structure implementation. It implements both the List and Deque interface. It belongs to the Java collections framework. Benefit of linked list is that it is faster to insert and delete an element from LinkedList, it doesn't need rearrangement of entire underlying array.
LinkedList is dynamic in size. So we can add elements and the size will dynamically increase, and if we remove elements, the size will dynamically reduce. It is handled by the the underlying collections framework.
Creating a LinkedList
We can create a linkedlist in Kotlin using java.util.LinkedList. In the following example, we created a linked list of planets.
import java.util.LinkedList
fun main(args: Array<String>)
{
var planets = LinkedList<String>();
planets.addAll(listOf("Earth", "Venus", "Mars"))
println("Planets = " + planets)
}
Output:
Planets = [Earth, Venus, Mars]
We can get the first element of the LinkedList using first member variable. In the following example, we are retrieving the first element and displaying it.
import java.util.LinkedList
fun main(args: Array<String>)
{
var planets = LinkedList<String>();
planets.addAll(listOf("Earth", "Venus", "Mars"))
println("First planet = " + planets.first)
}
Output:
First planet = Earth
We can get the last element of the LinkedList using last member variable. In the following example, we are retrieving the last element and displaying it.
import java.util.LinkedList
fun main(args: Array<String>)
{
var planets = LinkedList<String>();
planets.addAll(listOf("Earth", "Venus", "Mars"))
println("Last planet = " + planets.last)
}
Output:
Last planet = Mars
We can add element to the start of the linked list, using addFirst method. In the following example we used addFirst method to add Mercury planet to the top of the LinkedList.
import java.util.LinkedList
fun main(args: Array<String>)
{
var planets = LinkedList<String>();
planets.addAll(listOf("Earth", "Venus", "Mars"))
planets.addFirst("Mercury")
println("planets = " + planets)
}
Output:
planets = [Mercury, Earth, Venus, Mars]
We can add element to the end of the linked list, using addLast method. In the following example, we used addLast method to add Pluto planet to the end of the LinkedList.
import java.util.LinkedList
fun main(args: Array<String>)
{
var planets = LinkedList<String>();
planets.addAll(listOf("Earth", "Venus", "Mars"))
planets.addLast("Pluto")
println("planets = " + planets)
}
Output:
planets = [Earth, Venus, Mars, Pluto]
We can remove element from the start of the LinkedList using removeFirst function. In the following example, we are removing the first planet "Mercury" from the planet list using removeFirst function.
import java.util.LinkedList
fun main(args: Array<String>)
{
var planets = LinkedList<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
planets.removeFirst()
println("Planets = " + planets)
}
Output:
Planets = [Venus, Earth, Pluto]
We can remove element from the end of the LinkedList using removeLast function. In the following example, we are removing the last planet "Pluto" from the planet list using removeLast function.
import java.util.LinkedList
fun main(args: Array<String>)
{
var planets = LinkedList<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
planets.removeLast()
println("Planets = " + planets)
}
Output:
Planets = [Mercury, Venus, Earth]
We can use for loop to iterate through a LinkedList in Kotlin. In the following example, we are iterating through linked list of planets and printing each planet.
import java.util.LinkedList
fun main(args: Array<String>)
{
var planets = LinkedList<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
for(planet in planets)
{
println(planet)
}
}
Output:
Mercury
Venus
Earth
Pluto
We can use method addAll to add a list of elements to linked list. In the following example, we are adding more planets to our list of planets using addAll method.
import java.util.LinkedList
fun main(args: Array<String>)
{
var planets = LinkedList<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
planets.addAll(listOf("Jupiter", "Saturn"))
for(planet in planets)
{
println(planet);
}
}
Output:
Mercury
Venus
Earth
Pluto
Jupiter
Saturn
We can use add method, specifying an index where to add the element followed by the element. This accomplishes adding element to that location. In the below example, we are using add method to insert Jupiter in between the LinkedList.
import java.util.LinkedList
fun main(args: Array<String>)
{
var planets = LinkedList<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
planets.add(2, "Jupiter");
for(planet in planets)
{
println(planet);
}
}
Output:
Mercury
Venus
Jupiter
Earth
Pluto
We can use contains method to check if LinkedList contains the element or not. In the below example, we are checking if LinkedList contains the planet Pluto.
import java.util.LinkedList
fun main(args: Array<String>)
{
var planets = LinkedList<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
if(planets.contains("Pluto"))
{
println("Pluto is a Planet!")
}
else
{
println("Pluto is NOT a Planet!")
}
}
Output:
Pluto is a Planet!
We can use remove method to remove an element from LinkedList. In the below example, we are removing the planet Pluto from the LinkedList of planets.
import java.util.LinkedList
fun main(args: Array<String>)
{
var planets = LinkedList<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
planets.remove("Pluto")
for(planet in planets)
{
println(planet)
}
}
Output:
Mercury
Venus
Earth
import java.util.LinkedList
fun main(args: Array<String>)
{
var planets = LinkedList<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
planets.remove("Jupiter")
for(planet in planets)
{
println(planet)
}
}
Output:
Mercury
Venus
Earth
Pluto
import java.util.LinkedList
fun main(args: Array<String>)
{
var planets = LinkedList<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
planets.removeAll(listOf("Earth", "Pluto"))
for(planet in planets)
{
println(planet)
}
}
Output:
Mercury
Venus
import java.util.LinkedList
fun main(args: Array<String>)
{
var planets = LinkedList<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
planets.removeAt(2)
for(planet in planets)
{
println(planet)
}
}
Output:
Mercury
Venus
Pluto
import java.util.LinkedList
fun main(args: Array<String>)
{
var planets = LinkedList<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
planets.removeIf { x -> x.startsWith("E") }
for(planet in planets)
{
println(planet)
}
}
Output:
Mercury
Venus
Pluto
We can reverse the elements in place for the LinkedList using reverse function. In the below example, we are reversing the planets linked list and printing the linked list.
import java.util.LinkedList
fun main(args: Array<String>)
{
var planets = LinkedList<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
planets.reverse()
for(planet in planets)
{
println(planet)
}
}
Output:
Pluto
Earth
Venus
Mercury
import java.util.LinkedList
fun main(args: Array<String>)
{
var planets = LinkedList<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
var planetsReversed = planets.reversed()
println("Original Planets = ")
for(planet in planets)
{
println(planet)
}
println("\n\nPlanets Reversed = ")
for(planet in planetsReversed)
{
println(planet)
}
}
Output:
Original Planets =
Mercury
Venus
Earth
Pluto
Planets Reversed =
Pluto
Earth
Venus
Mercury
We can clear the contents of LinkedList using clear function. In the following example, we are using the clear function to clear the contents of the LinkedList. We also demonstrate a property of LinkedList called size. size property can be used to find out the number of elements in the linked list.
import java.util.LinkedList
fun main(args: Array<String>)
{
var planets = LinkedList<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
planets.clear()
for(planet in planets)
{
println(planet)
}
println("Size of linkedlist = " + planets.size)
}
Output:
Size of linkedlist = 0
We can get the count of elements in LinkedList using count function. In the below example, we are using the count function to get the count for the number of planets in the LinkedList.
import java.util.LinkedList
fun main(args: Array<String>)
{
var planets = LinkedList<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
println("Count of elements in linkedlist = " + planets.count())
}
Output:
Count of elements in linkedlist = 4
import java.util.LinkedList
fun main(args: Array<String>)
{
var planets = LinkedList<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto", "Eldorado"))
var numOfPlanetsStartingWithE = planets.count { x -> x.startsWith("E") }
println("Count of planets starting with E in linkedlist = " + numOfPlanetsStartingWithE)
}
Output:
Count of planets starting with E in linkedlist = 2
We can remove duplicate elements from LinkedList and get a new List, using distinct function. In the below example, we have some duplicate entries in our planets linkedlist. We then used distinct function call to get a new list without the duplicates.
import java.util.LinkedList
fun main(args: Array<String>)
{
var planets = LinkedList<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Earth", "Venus", "Pluto"))
var uniquePlanets = planets.distinct()
for(planet in uniquePlanets)
{
println(planet)
}
}
Output:
Mercury
Venus
Earth
Pluto
We can check for emptiness of a LinkedList using isEmpty function. In the following example, we check if linkedlist of planets is empty or not and print a message accordingly.
import java.util.LinkedList
fun main(args: Array<String>)
{
var planets = LinkedList<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Earth", "Venus", "Pluto"))
planets.clear()
if(planets.isEmpty())
{
println("Planets list is empty!")
}
else
{
println("Planets list is NOT Empty!")
}
}
Output:
Planets list is empty!
We can use the max function on linkedlist to find the maximum element. In the below example, we used max function to determine the highest integer in our linkedlist.
import java.util.LinkedList
fun main(args: Array<String>)
{
var numbers = LinkedList<Int>();
numbers.addAll(listOf(21, 44, 99, 74, 33))
var maxValue = numbers.max()
println("LinkedList Max value = " + maxValue)
}
Output:
LinkedList Max value = 99
We can use the min function on linked list to find the minimum element. In the below example, we used min function to determine the smallest integer in our linked list.
import java.util.LinkedList
fun main(args: Array<String>)
{
var numbers = LinkedList<Int>();
numbers.addAll(listOf(21, 44, 99, 74, 33))
var minValue = numbers.min()
println("LinkedList Max value = " + minValue)
}
Output:
LinkedList Max value = 21
We can use the get function to get value at a given index. In the following example, we used get function to retrieve the value at position 2 of the linkedlist.
import java.util.LinkedList
fun main(args: Array<String>)
{
var numbers = LinkedList<Int>();
numbers.addAll(listOf(21, 44, 99, 74, 33))
var value = numbers.get(2)
println("value = " + value)
}
Output:
value = 99
import java.util.LinkedList
fun main(args: Array<String>)
{
var numbers = LinkedList<Int>();
numbers.addAll(listOf(21, 44, 99, 74, 33))
var value = numbers.indexOf(99)
println("value = " + value)
}
Output:
value = 2