Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Linked List Example 
 

This section discusses an example to demonstrate the various methods of List interface. We are using two classes ArrayList and LinkedList in the example code.

 

Linked List Example

                         

This section discusses an example to demonstrate the various methods of List interface. We are using two classes ArrayList and LinkedList in the example code. The code below is similar to the previous example, but it performs many List operations. Lets discuss the example code.

Description of program:

This program helps you in storing the large amount of data as a collection. The LinkedList is a part of collection that constructs a list containing the elements of the specified collection. Iterator methods returns the values in the order in which they are stored.

If you want to insert the data in the linkedList then use add() method. The hasNext() method returns true if the iterator contains more elements and the next() method returns the next element in the iteration. To insert and remove the data at first, last and specified position in the linkedList, you use the addFirst(), addLast(), add(), removeFirst(), removeLast() and remove() methods. To retrieve the element with respect to a specified position use the getFirst(), getLast() and get() methods.  

Here is the code of program:

import java.util.*;

public class LinkedListExample{
  public static void main(String[] args) {
    System.out.println("Linked List Example!");
    LinkedList <Integer>list = new LinkedList<Integer>();
    int num1 = 11, num2 = 22, num3 = 33, num4 = 44;
    int size;
    Iterator iterator;
    //Adding data in the list
    list.add(num1);
    list.add(num2);
    list.add(num3);
    list.add(num4);
    size = list.size();
    System.out.print"Linked list data: ");  
    //Create a iterator
    iterator = list.iterator();     
    while (iterator.hasNext()){
      System.out.print(iterator.next()+" ");  
    }
    System.out.println();
    //Check list empty or not
    if (list.isEmpty()){
      System.out.println("Linked list is empty");
    }
    else{
      System.out.println"Linked list size: " + size);
    }
    System.out.println("Adding data at 1st location: 55");
    //Adding first
    list.addFirst(55);
    System.out.print("Now the list contain: ");
    iterator = list.iterator();
    while (iterator.hasNext()){
      System.out.print(iterator.next()+" ");  
    }
    System.out.println();
    System.out.println("Now the size of list: " + list.size());
    System.out.println("Adding data at last location: 66");
    //Adding last or append
    list.addLast(66);
    System.out.print("Now the list contain: ");
    iterator = list.iterator();
    while (iterator.hasNext()){
      System.out.print(iterator.next()+" ");  
    }
    System.out.println();
    System.out.println("Now the size of list: " + list.size());
    System.out.println("Adding data at 3rd location: 55");
    //Adding data at 3rd position
    list.add(2,99);
    System.out.print("Now the list contain: ");
    iterator = list.iterator();
    while (iterator.hasNext()){
      System.out.print(iterator.next()+" ");  
    }
    System.out.println();
    System.out.println("Now the size of list: " + list.size());
    //Retrieve first data
    System.out.println("First data: " + list.getFirst());
    //Retrieve lst data
    System.out.println("Last data: " + list.getLast());
    //Retrieve specific data
    System.out.println("Data at 4th position: " + list.get(3));
    //Remove first
    int first = list.removeFirst();
    System.out.println("Data removed from 1st location: " + first);
    System.out.print("Now the list contain: ");
    iterator = list.iterator();
    //After removing data
    while (iterator.hasNext()){
      System.out.print(iterator.next()+" ");  
    }
    System.out.println();
    System.out.println("Now the size of list: " + list.size());
    //Remove last
    int last = list.removeLast();
    System.out.println("Data removed from last location: " + last);
    System.out.print("Now the list contain: ");
    iterator = list.iterator();
    //After removing data
    while (iterator.hasNext()){
      System.out.print(iterator.next()+" ");  
    }
    System.out.println();
    System.out.println("Now the size of list: " + list.size());
    //Remove 2nd data
    int second = list.remove(1);
    System.out.println("Data removed from 2nd location: " + second);
    System.out.print("Now the list contain: ");
    iterator = list.iterator();
    //After removing data
    while (iterator.hasNext()){
      System.out.print(iterator.next()+" ");  
    }
    System.out.println();
    System.out.println("Now the size of list: " + list.size());
    //Remove all
    list.clear();
    if (list.isEmpty()){
      System.out.println("Linked list is empty");
    }
    else{
      System.out.println"Linked list size: " + size);
    }
  }
}

Download this example.

Output of program:

C:\vinod\collection>javac LinkedListExample.java

C:\vinod\collection>java LinkedListExample
Linked List Example!
Linked list data: 11 22 33 44
Linked list size: 4
Adding data at 1st location: 55
Now the list contain: 55 11 22 33 44
Now the size of list: 5
Adding data at last location: 66
Now the list contain: 55 11 22 33 44 66
Now the size of list: 6
Adding data at 3rd location: 55
Now the list contain: 55 11 99 22 33 44 66
Now the size of list: 7
First data: 55
Last data: 66
Data at 4th position: 22
Data removed from 1st location: 55
Now the list contain: 11 99 22 33 44 66
Now the size of list: 6
Data removed from last location: 66
Now the list contain: 11 99 22 33 44
Now the size of list: 5
Data removed from 2nd location: 99
Now the list contain: 11 22 33 44
Now the size of list: 4
Linked list is empty

C:\vinod\collection>

                         

» View all related tutorials
Related Tags: c algorithm api ide collections orm ant data form interface framework software io multiple ip order vi collection tag int

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

3 comments so far (
post your own) View All Comments Latest 10 Comments:

fine but give some more examples on implementing ArrayList interface

Posted by venkatesh on Thursday, 11.1.07 @ 18:42pm | #35329

plz try to write more complex codes. with favourite of professionlism.

Posted by haitham on Sunday, 10.21.07 @ 01:09am | #34480

nice but give some more examples on Array lists,and collections also.pls

Posted by gopal on Tuesday, 08.14.07 @ 15:13pm | #23359

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.