Linked list allows us to arrange elements into order. It maintain the order of elements in which they have added.
Its iterator traverse on element into same order in they have added. It has all common operations related to linked list in our data structure like insert, remove, traverse etc.
LinkedList class is an implementation of the List interface. It implements all optional list operations, and permits all elements.
In addition to implementing the List interface, the LinkedList class provides uniformly named methods to get, remove and insert an element at the beginning and end of the list.
These operations allow linked lists to be used as a stack, queue, or double-ended queue.
package collection.demos;
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;
import java.util.ListIterator;
public class LinkedListDemo1 {
public static void main(String[] args) {
LinkedList<String> lists = new LinkedList<String>();
System.out.println("Initial List :" + lists);
lists.add("a");
lists.add("b");
lists.add("c");
lists.add("d");
lists.add("e");
System.out.println("After adding five alphabets :" + lists);
// Getting first and last element of list
System.out.println("First Element: " + lists.getFirst());
System.out.println("Last Element: " + lists.getLast());
// Removing elements from first and last position
lists.removeFirst();
lists.removeLast();
System.out.println("List after removing first and last elements : " + lists);
// Adding first elements at first and last postion
lists.addFirst("a");
lists.addLast("e");
System.out.println("List after adding elements at first and last position : " + lists);
// Checkeng for specific element
System.out.println("List contains element d : " + lists.contains("d"));
// Getting the number of elements in this list
System.out.println("List size : " + lists.size());
// Appending the specified element to the specified index of this list.
lists.add(2, "x");
System.out.println("List after appending element at 2 index : " + lists);
// Removing the first occurrence of the specified element from this list
lists.remove("x");
System.out.println("List after removing element x : " + lists);
// Getting element of specified index
System.out.println("Element at index 2 : " + lists.get(2));
// Replacing the element at the specified position in this list with the specified element.
lists.set(2, "x");
System.out.println("List after replacing element at 2 index with x " + lists);
// Checking index of specified element into list
System.out.println("Index of d in list is " + lists.indexOf("d"));
System.out.println("Last Index of d in list is " + lists.lastIndexOf("d"));
// Retrieving, but does not removing, the head (first element) of this list.
System.out.println("head element " + lists.peek());
System.out.println("peaking first element " + lists.peekFirst());
// Retrieving and removing the head (first element) of this list
System.out.println("List before polling " + lists);
System.out.println("Head element " + lists.poll());
System.out.println("List after polling " + lists);
// Using LikedList as stack
// Pushes an element onto the stack represented by this list. In other words, inserts the element at the front of this list.
lists.push("a");
System.out.println("List after push element a " + lists);
// Pops an element from the stack represented by this list. In other words, removes and returns the first element of this list.
System.out.println("Poped Element " + lists.pop());
System.out.println("List after pop element a " + lists);
// Removing first and last accorance of element
lists.add("x");
lists.add("d");
System.out.println("List contetns " + lists);
System.out.println("after removeFirstOccurrence of x : " + lists.removeFirstOccurrence("x"));
System.out.println("after removeLastOccurrence of d : " + lists.removeLastOccurrence("d"));
System.out.println("List contetns " + lists);
// list-iterator of the elements, in proper sequence
System.out.println("List iterator");
for (ListIterator<String> it = lists.listIterator(); it.hasNext();) {
String string = it.next();
System.out.println(string);
}
// list-iterator of the elements,starting at the specified position in the list, in proper sequence
System.out.println("List iterator start from index 2");
for (ListIterator<String> it = lists.listIterator(2); it.hasNext();) {
String string = it.next();
System.out.println(string);
}
// Creating sublist of list
List<String> sub = lists.subList(1, 3);
System.out.println("List " + lists);
System.out.println("Sub List " + sub);
// Returns an iterator over the elements in this deque in reverse sequential order.
// The elements will be returned in order from last (tail) to first (head).
System.out.println("Using descendingIterator");
for (Iterator<String> it = lists.descendingIterator(); it.hasNext();) {
String string = it.next();
System.out.println(string);
}
// Creating array of elements
String[] arr = lists.toArray(new String[0]);
for (String e : arr) {
System.out.println(e);
}
}
}
Subscribe to:
Post Comments (Atom)
Popular Posts
-
HSQLDB is a portable RDBMS implemented in pure java. It can be embedded with your application as well as can be used separately. It is very ...
-
If you want to use the database into your web application, you can use the HSQLDB in In_Process mode. In this mode, you can embed the HSQLD...
-
HTML Page < html > < head > < title > welcome </ title > < script language = "javascript"...
-
In web application ApplicationContext is created using Context Loaders. there are two implementations of context loader. ContextLoaderLis...
-
HSQLDB database it a very small database that can be controlled from you application. you can start and stop the database by writing the ...
-
Some time you will see the form containing the button " Add More " . This facility is provided for the user to get the values ...
-
JSP code <%@page import="java.sql.*"%> <%! int prod_id=1; String prod_name="Laptop"; int qty=2; float p...
-
StudentRegistration │ index.html │ login.html │ └───WEB-INF │ web.xml │ └───classes login.class lo...
-
package process; import javax.swing. * ; import javax.swing.table. * ; import java.sql. * ; /** * This class create JTable from Da...
-
Hibernate ORM framework does not provide any approch to connect to MS Access database. Unfortunately, Access is not supported officially...
No comments:
Post a Comment