import java.util.*;
class LinkedListDemo
{
public static void main(String args[])
{
// create a linked list
LinkedList<String> ll = new LinkedList<String>();
// add elements to the linked list
ll.add("F");
ll.add("B");
ll.add("D");
ll.add("E");
ll.add("C");
System.out.println("Original contents of ll: " + ll);
//adding first and last element in linkedlist
ll.addLast("Z");
ll.addFirst("A");
ll.add(1, "A2");
System.out.println("contents of ll after adding first and last: " + ll);
// remove elements from the linked list
ll.remove("F");
ll.remove(2);
System.out.println("Contents of ll after deletion: " + ll);
// remove first and last elements
ll.removeFirst();
ll.removeLast();
System.out.println("ll after deleting first and last: " + ll);
// get and set a value
Object val = ll.get(2);
ll.set(2, (String) val + " Changed");
System.out.println("ll after change: " + ll);
//Iterator to display elements
System.out.print("Displaying contents of ll using Iterator: ");
Iterator itr = ll.iterator();
while(itr.hasNext())
{
Object element = itr.next();
System.out.print(element + " ");
}
System.out.println();
//For each, alternative to iterator
System.out.print("Displaying contents of ll using for each: ");
for(String s:ll)
{
System.out.print(s + " ");
}
System.out.println();
// modify objects being iterated
ListIterator litr = ll.listIterator();
while(litr.hasNext())
{
Object element = litr.next();
litr.set(element + "+");
}
System.out.print("Modified contents of ll: ");
itr = ll.iterator();
while(itr.hasNext())
{
Object element = itr.next();
System.out.print(element + " ");
}
System.out.println();
// now, display the list backwards
System.out.print("Modified list backwards: ");
while(litr.hasPrevious())
{
Object element = litr.previous();
System.out.print(element + " ");
}
System.out.println();
}
}
Output:
Original contents of ll: [F, B, D, E, C]
contents of ll after adding first and last: [A, A2, F, B, D, E, C, Z]
Contents of ll after deletion: [A, A2, D, E, C, Z]
ll after deleting first and last: [A2, D, E, C]
ll after change: [A2, D, E Changed, C]
Displaying contents of ll using Iterator: A2 D E Changed C
Displaying contents of ll using for each: A2 D E Changed C
Modified contents of ll: A2+ D+ E Changed+ C+
Modified list backwards: C+ E Changed+ D+ A2+
Note:
LinkedList implements List, Deque and Queue.
class LinkedListDemo
{
public static void main(String args[])
{
// create a linked list
LinkedList<String> ll = new LinkedList<String>();
// add elements to the linked list
ll.add("F");
ll.add("B");
ll.add("D");
ll.add("E");
ll.add("C");
System.out.println("Original contents of ll: " + ll);
//adding first and last element in linkedlist
ll.addLast("Z");
ll.addFirst("A");
ll.add(1, "A2");
System.out.println("contents of ll after adding first and last: " + ll);
// remove elements from the linked list
ll.remove("F");
ll.remove(2);
System.out.println("Contents of ll after deletion: " + ll);
// remove first and last elements
ll.removeFirst();
ll.removeLast();
System.out.println("ll after deleting first and last: " + ll);
// get and set a value
Object val = ll.get(2);
ll.set(2, (String) val + " Changed");
System.out.println("ll after change: " + ll);
//Iterator to display elements
System.out.print("Displaying contents of ll using Iterator: ");
Iterator itr = ll.iterator();
while(itr.hasNext())
{
Object element = itr.next();
System.out.print(element + " ");
}
System.out.println();
//For each, alternative to iterator
System.out.print("Displaying contents of ll using for each: ");
for(String s:ll)
{
System.out.print(s + " ");
}
System.out.println();
// modify objects being iterated
ListIterator litr = ll.listIterator();
while(litr.hasNext())
{
Object element = litr.next();
litr.set(element + "+");
}
System.out.print("Modified contents of ll: ");
itr = ll.iterator();
while(itr.hasNext())
{
Object element = itr.next();
System.out.print(element + " ");
}
System.out.println();
// now, display the list backwards
System.out.print("Modified list backwards: ");
while(litr.hasPrevious())
{
Object element = litr.previous();
System.out.print(element + " ");
}
System.out.println();
}
}
Output:
Original contents of ll: [F, B, D, E, C]
contents of ll after adding first and last: [A, A2, F, B, D, E, C, Z]
Contents of ll after deletion: [A, A2, D, E, C, Z]
ll after deleting first and last: [A2, D, E, C]
ll after change: [A2, D, E Changed, C]
Displaying contents of ll using Iterator: A2 D E Changed C
Displaying contents of ll using for each: A2 D E Changed C
Modified contents of ll: A2+ D+ E Changed+ C+
Modified list backwards: C+ E Changed+ D+ A2+
Note:
LinkedList implements List, Deque and Queue.
No comments:
Post a Comment