Skip to main content

Linked List

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(13);

        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);

        }

    }

}

Comments

Popular posts from this blog

Using HyperSQL (HSQLDB)

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 a small database that supports almost all features of the standard database system. It comes with small jar file that can be found in lib folder. The HSQLDB jar package is located in the /lib directory of the ZIP package and contains several components and programs. Core components of jar file are : HyperSQL RDBMS Engine (HSQLDB), HyperSQL JDBC Driver, Database Manager, and Sql Tool. Installing and Using Download: download latest release of HyperSQL database from http://hsqldb.org website and extract it. You will see following contents. Here "bin" directory contains some batch files those can be used to run a swing based GUI tool. You can use runManagerSwing.bat to connect to database, but database must be on before running it. Directory lib contains File hsqldb.jar . It is the database to be used by you. Running database First

In Process Mode of HSQLDB in web application.

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 HSQLDB into your web application and it runs as a part of your web application programm in the same JVM. In this mode, the database does not open any port to connect to the application on the hosing machine and you don't need to configure anything to access it. Database is not expposed to other application and can not be accessed from any dabase tools like dbVisualizer etc. In this mode ,database will be unknown from any other person except you. But in the 1.8.0 version, you can use Server intance for external as well as in process access.  To close the databse, you can issue SHUTDOWN command as an SQL query.   In the in-process mode, database starts from JDBC with the associated databse file provided through  connection URL. for example   DriverManager.getConnection("jdbc:hsqldb:mydatabase","SA","");   Here myd

How to handle values from dynamically generated elements in web page using struts2

Some time you will see the form containing the button " Add More " . This facility is provided for the user to get the values for unknown number of repeating for some information. for example when you are asking to get the projects details from user, you need to put the option to add the more project for the user since you don't known how many projects user have. In the HTML form, you repeat the particular section to get the multiple values for those elements. In Html page , you can put the option to add new row of elements or text fields by writing the java script or using JQuery API. Now, the question is that how to capture the values of dynamically generated text fields on the server. Using the servlet programming you can get the values by using getParameters() method that resultants the array of the parameter having the same name. But this limit you to naming the text fields in the HTML form. To ally this approach, you have to take the same name for t