Skip to content

Create java-linkedList-examples #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 200 additions & 0 deletions java-linkedList-examples
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class CreateLinkedListExample {
public static void main(String[] args) {
// Creating a LinkedList
LinkedList<String> friends = new LinkedList<>();

// Adding new elements to the end of the LinkedList using add() method.
friends.add("Rajeev");
friends.add("John");
friends.add("David");
friends.add("Chris");

System.out.println("Initial LinkedList : " + friends);

// Adding an element at the specified position in the LinkedList
friends.add(3, "Lisa");
System.out.println("After add(3, \"Lisa\") : " + friends);

// Adding an element at the beginning of the LinkedList
friends.addFirst("Steve");
System.out.println("After addFirst(\"Steve\") : " + friends);

// Adding an element at the end of the LinkedList (This method is equivalent to the add() method)
friends.addLast("Jennifer");
System.out.println("After addLast(\"Jennifer\") : " + friends);

// Adding all the elements from an existing collection to the end of the LinkedList
List<String> familyFriends = new ArrayList<>();
familyFriends.add("Jesse");
familyFriends.add("Walt");

friends.addAll(familyFriends);
System.out.println("After addAll(familyFriends) : " + friends);
}
}

***********************************************************************************************************************************************************************

import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

public class IterateOverLinkedListExample {
public static void main(String[] args) {
LinkedList<String> humanSpecies = new LinkedList<>();

humanSpecies.add("Homo Sapiens");
humanSpecies.add("Homo Neanderthalensis");
humanSpecies.add("Homo Erectus");
humanSpecies.add("Home Habilis");

System.out.println("=== Iterate over a LinkedList using Java 8 forEach and lambda ===");
humanSpecies.forEach(name -> {
System.out.println(name);
});


System.out.println("\n=== Iterate over a LinkedList using iterator() ===");
Iterator<String> humanSpeciesIterator = humanSpecies.iterator();
while (humanSpeciesIterator.hasNext()) {
String speciesName = humanSpeciesIterator.next();
System.out.println(speciesName);
}

System.out.println("\n=== Iterate over a LinkedList using iterator() and Java 8 forEachRemaining() method ===");
humanSpeciesIterator = humanSpecies.iterator();
humanSpeciesIterator.forEachRemaining(speciesName -> {
System.out.println(speciesName);
});

System.out.println("\n=== Iterate over a LinkedList using descendingIterator() ===");
Iterator<String> descendingHumanSpeciesIterator = humanSpecies.descendingIterator();
while (descendingHumanSpeciesIterator.hasNext()) {
String speciesName = descendingHumanSpeciesIterator.next();
System.out.println(speciesName);
}


System.out.println("\n=== Iterate over a LinkedList using listIterator() ===");
// ListIterator can be used to iterate over the LinkedList in both forward and backward directions
// In this example, we start from the end of the list and traverse backwards
ListIterator<String> humanSpeciesListIterator = humanSpecies.listIterator(humanSpecies.size());
while (humanSpeciesListIterator.hasPrevious()) {
String speciesName = humanSpeciesListIterator.previous();
System.out.println(speciesName);
}

System.out.println("\n=== Iterate over a LinkedList using simple for-each loop ===");
for(String speciesName: humanSpecies) {
System.out.println(speciesName);
}
}
}

***********************************************************************************************************************************************************************

import java.util.LinkedList;

public class RemoveElementsFromLinkedListExample {
public static void main(String[] args) {
LinkedList<String> programmingLanguages = new LinkedList<>();

programmingLanguages.add("Assembly");
programmingLanguages.add("Fortran");
programmingLanguages.add("Pascal");
programmingLanguages.add("C");
programmingLanguages.add("C++");
programmingLanguages.add("Java");
programmingLanguages.add("C#");
programmingLanguages.add("Kotlin");

System.out.println("Initial LinkedList = " + programmingLanguages);

// Remove the first element in the LinkedList
String element = programmingLanguages.removeFirst(); // Throws NoSuchElementException if the LinkedList is empty
System.out.println("Removed the first element " + element + " => " + programmingLanguages);

// Remove the last element in the LinkedList
element = programmingLanguages.removeLast(); // Throws NoSuchElementException if the LinkedList is empty
System.out.println("Removed the last element " + element + " => " + programmingLanguages);

// Remove the first occurrence of the specified element from the LinkedList
boolean isRemoved = programmingLanguages.remove("C#");
if(isRemoved) {
System.out.println("Removed C# => " + programmingLanguages);
}

// Remove all the elements that satisfy the given predicate
programmingLanguages.removeIf(programmingLanguage -> programmingLanguage.startsWith("C"));
System.out.println("Removed elements starting with C => " + programmingLanguages);

// Clear the LinkedList by removing all elements
programmingLanguages.clear();
System.out.println("Cleared the LinkedList => " + programmingLanguages);
}
}

************************************************************************************************************************************************************************

import java.util.LinkedList;

public class RetrieveLinkedListElementsExample {
public static void main(String[] args) {
// A LinkedList containing Stock Prices of a company for the last 6 days
LinkedList<Double> stockPrices = new LinkedList<>();

stockPrices.add(45.00);
stockPrices.add(51.00);
stockPrices.add(62.50);
stockPrices.add(42.75);
stockPrices.add(36.80);
stockPrices.add(68.40);

// Getting the first element in the LinkedList using getFirst()
// The getFirst() method throws NoSuchElementException if the LinkedList is empty
Double firstElement = stockPrices.getFirst();
System.out.println("Initial Stock Price : " + firstElement);

// Getting the last element in the LinkedList using getLast()
// The getLast() method throws NoSuchElementException if the LinkedList is empty
Double lastElement = stockPrices.getLast();
System.out.println("Current Stock Price : " + lastElement);

// Getting the element at a given position in the LinkedList
Double stockPriceOn3rdDay = stockPrices.get(2);
System.out.println("Stock Price on 3rd Day : " + stockPriceOn3rdDay);
}
}

************************************************************************************************************************************************************************

import java.util.LinkedList;

public class SearchLinkedListExample {
public static void main(String[] args) {
LinkedList<String> employees = new LinkedList<>();

employees.add("John");
employees.add("David");
employees.add("Lara");
employees.add("Chris");
employees.add("Steve");
employees.add("David");

// Check if the LinkedList contains an element
System.out.println("Does Employees LinkedList contain \"Lara\"? : " + employees.contains("Lara"));

// Find the index of the first occurrence of an element in the LinkedList
System.out.println("indexOf \"Steve\" : " + employees.indexOf("Steve"));
System.out.println("indexOf \"Mark\" : " + employees.indexOf("Mark"));

// Find the index of the last occurrence of an element in the LinkedList
System.out.println("lastIndexOf \"David\" : " + employees.lastIndexOf("David"));
System.out.println("lastIndexOf \"Bob\" : " + employees.lastIndexOf("Bob"));
}
}