Thursday, October 3, 2013

LinkedList

In this post, you learn to use the LinkedList data structure (in java.util package) that comes with the Java standard API. LinkedList is a collection class in Java. It is used to store numbers, text, or other objects. In your every day work of writing computer software, you might want to store data in a list and at the same time, the list should be easy to manipulate and maintain good performance. By using the LinkedList class, you reduce programming effort (effort to create a LinkedList by yourself) and improve program's performance because it was written with data structures and algorithms that are optimized for good performance.

The LinkedList class provides the add(int index, Object item) method that can be used to add a new item at any position of the list. Alternatively, in case that you want to add the new item at the beginning and at the end of the list, you can use the addFirst(Object item) and addLast(Object item). Besides adding one item to the list at a time, you can add multiple items to the list by using the addAll(Collection c) method. The Collection, c can be LinkedList, ArrayList, Vector, etc.
In the example below, the add method is used to add one Organization object to the LinkedList, llist.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.Scanner;

class Organization implements Serializable{

private String OrName;
private String OrPhone;
Organization(String OrName, String OrPhone){
this.OrName=OrName;
this.OrPhone=OrPhone;
}

public void printData(){
System.out.println(OrName+"\t"+OrPhone);
}
}
public class LinkedListDemo {
private static LinkedList<Organization> llist;
public static void main(String[] args){
llist=new LinkedList<Organization>();
try {
addOrg();
printList();
updateOrg();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void addOrg() throws IOException{
Organization NewOrg=readUserInput(); //read input from the user
llist.add(NewOrg); //add the new organization to the list
System.out.println("The organization was added to the list.");
}
        public static Organization readUserInput() throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter organization name:");
String OrName=br.readLine();
System.out.println("Enter organization phone:");
String OrPhone=br.readLine();
return new Organization(OrName,OrPhone);
}
}

If you want to remove an item from the linked list, you can use the remove(int index) method to remove an item at any position. Similarly to the adding item to the list, you can use the removeFirst() method to remove the first item of the list and use the removeLast() method remove the last item of the list. To remove all items of the list, you will use the clear() method. The example method below removes the item whose index is input by the user.

public static void removeOrg() throws IOException{
Scanner scanner=new Scanner(System.in);
System.out.println("Enter item index:");
int index=scanner.nextInt();
if(index>=0 && index<=llist.size()){
llist.remove(index); //remove the existing organization
System.out.println("The organization at index "+index+" is removed.");
}
scanner.close();
}


The get(int index) method is used to return an item at the specified index. You will use it to search for an item of list by position. The LinkedList data structure also allows you to update any item of the list by using the set(int index, Object NewItem) method. In the example code below, the get method is used to get the position of the organization that the user wants to update. Then the set method is used to replace the old organization with the new one so that the organization information is updated.

public static void updateOrg() throws IOException{
Scanner scanner=new Scanner(System.in);
System.out.println("Enter item index:");
int index=scanner.nextInt();
if(index>=0){
Organization NewOrg=readUserInput();
llist.set(index, NewOrg);
System.out.println("The organization was updated.");
}
else
System.out.println("Cannot update the organization");
scanner.close();
}

To print all items of the list, you can use the ListIterator interface to traverse the list and get the information printed. The code below does this task.

public static void printList(){
ListIterator<Organization> iterator=llist.listIterator();
if(llist.size()>0){
System.out.println("Contents of the list:");
while(iterator.hasNext()){
Organization Or=(Organization)iterator.next();
Or.printData();
}
}
else
System.out.println("No item in the list");
}

1 comment:

  1. When it comes to Digital Marketing, you should rest assured it has been a great mode to earn money. It would offer you several options to make the most of your talent online and earn money without investing a huge amount.

    ReplyDelete