Insert an
Element into Linked List Java
import
java.util.LinkedList;
public class
LinkedListInsertElementExample
{
public static void
main(String[] args)
{
LinkedList lList = new
LinkedList();
lList.add("1");
lList.add("2");
lList.add("3");
lList.add("4");
lList.add("5");
System.out.println("LinkedList
contains : " + lList);
lList.add(2,
"2.5");
System.out.println("After
inserting 2.5, LinkedList contains : " + lList);
}
}
Iterate through
elements of Java Linked List using List Iterator
import
java.util.ListIterator;
import
java.util.LinkedList;
public class
IterateLinkedListUsingListIteratorExample
{
public static void
main(String[] args)
{
LinkedList lList = new
LinkedList();
lList.add("1");
lList.add("2");
lList.add("3");
lList.add("4");
lList.add("5");
ListIterator itr =
lList.listIterator();
System.out.println("Iterating
through elements of Java LinkedList using
ListIterator in
forward direction...");
while(itr.hasNext())
{
System.out.println(itr.next());
}
System.out.println("Iterating
through elements of Java LinkedList using
ListIterator in
reverse direction...");
while(itr.hasPrevious())
System.out.println(itr.previous());
}
}
Remove all
elements or clear Linked List Java
import
java.util.LinkedList;
public class
RemoveAllElementsLinkedListExample
{
public static void
main(String[] args)
{
LinkedList lList = new
LinkedList();
lList.add("1");
lList.add("2");
lList.add("3");
lList.add("4");
lList.add("5");
System.out.println("LinkedList
contains : " + lList);
lList.clear();
System.out.println("LinkedList
now contains : " + lList);
}
}
Remove first
and last elements of Linked List Java
import java.util.LinkedList;
public class
RemoveFirstLastElementsLinkedListExample
{
public static void
main(String[] args)
{
LinkedList lList = new
LinkedList();
lList.add("1");
lList.add("2");
lList.add("3");
lList.add("4");
lList.add("5");
System.out.println("LinkedList
contains : " + lList);
Object object =
lList.removeFirst();
System.out.println(object
+ " has been removed from the first index
of LinkedList");
System.out.println("LinkedList
now contains : " + lList);
object = lList.removeLast();
System.out.println(object
+ " has been removed from the last index
of LinkedList");
System.out.println("LinkedList
now contains : " + lList);
}
}
Remove range of
elements from Linked List Java
import
java.util.LinkedList;
import
java.util.List;
public class
RemoveRangeElementsLinkedListExample
{
public static void
main(String[] args)
{
LinkedList lList = new
LinkedList();
lList.add("1");
lList.add("2");
lList.add("3");
lList.add("4");
lList.add("5");
System.out.println("LinkedList contains : " + lList);
lList.subList(2,5).clear();
System.out.println("Range
of elements removed from LinkedList");
System.out.println("LinkedList
now contains : " + lList);
}
}
Remove
specified element from Linked List Java
import java.util.LinkedList;
public class
RemoveElementLinkedListExample
{
public static void
main(String[] args)
{
LinkedList lList = new
LinkedList();
lList.add("1");
lList.add("2");
lList.add("3");
lList.add("4");
lList.add("5");
System.out.println("LinkedList
contains : " + lList);
boolean isRemoved =
lList.remove("2");
System.out.println("Is
2 removed from LinkedList ? :" + isRemoved);
System.out.println("LinkedList
now contains : " + lList);
Object obj =
lList.remove(2);
System.out.println(obj
+ " has been removed from LinkedList");
System.out.println("LinkedList
now contains : " + lList);
}
}
Replace an
Element of Linked List Java
import
java.util.LinkedList;
public class
LinkedListReplaceElementExample
{
public static void
main(String[] args)
{
LinkedList lList = new
LinkedList();
lList.add("1");
lList.add("2");
lList.add("3");
lList.add("4");
lList.add("5");
System.out.println("LinkedList
contains : " + lList);
lList.set(3,
"Replaced");
System.out.println("After
replacing 4, LinkedList contains : " + lList);
}
}
No comments:
Post a Comment