Write a java program which reads a series of first names & stores them in Linked List The program should not allow duplicate names and it should also allow the user to search first name.

import java.io.*;
import java.util.*;
class Slip30
{
    public static void main(String ar[]) throws IOException
    {
        int n;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter size of linkedList...");
        n = Integer.parseInt(br.readLine());
        LinkedList <String> ll = new LinkedList <String>();
        for(int i=0;i<n;i++)
        {
            System.out.println("Enter the element...");
            String str = br.readLine();
            if(ll.contains(str)==true)
            {
                System.out.println("The element is already existing...");
                i--;
            }
            else
            ll.add(str);
        }
        Iterator <String> it = ll.iterator();
        System.out.println("Contents of Linked List using iterator...");
        while(it.hasNext())
          System.out.println("\t" + it.next());
        System.out.println("Enter the element to search : ");
        String search = br.readLine();
        int pos = ll.indexOf(search);
        if(pos== -1)
           System.out.println(" The element not found" );
        else
           System.out.println(" The element found at location :  " +pos);
    }
}

2 comments:

  1. Write a program that reads a file and counts the number of occurrences of each letter

    ReplyDelete
  2. Nice Blog, Thanks for sharing such a informative blog with us. keep sharing!!

    Are you looking for free online java tutorial?


    free java tutorial!


    hello java programing

    ReplyDelete