June 04, 2012

DS: Array Linear List


import java.io.*;
import java.util.*;
class List
{
        Object element[];
        int size;
        public List(int value)
        {
                if(value<0)
                {
                        throw new IllegalArgumentException("we can't  create an array");
                }
                        element=new Object[value];
                        size=0;
               
        }
        public void add(int index,Object value)
        {
                if(index<0 || index>size)
                {
                        throw new IllegalArgumentException("we can't add the element in the list");
                       
                }
                if(element.length==size)
                {
                        System.out.println("we can't add the element");
                        return;
                }
                for(int i=size-1;i>=index;i--)
                   {
                        element[i+1]=element[i];
                   }
                element[index]=value;
                size++;
        }
        public int size()
        {
                return size;
        }
        public boolean empty()
        {
                return size==-1;
        }
        public  Object remove(int index)
        {
                Object x=element[index];
                for(int i=index;i
                {
                        element[i]=element[i+1];
                }
                size--;
                return x;
               
        }
        public int indexOf(Object value)
        {
                for(int i=0;i
                {
                        if(element[i]==value)
                                return i;
                }
                return -1;
        }
        public  void display()
        {
                System.out.print("[");
                for(int i=0;i
                {
                        System.out.print(element[i]+",");
                }
                System.out.print("]");
        }
        public Object get(int index)
        {
                if(index<0 || index>size)
                {
                        throw new IllegalArgumentException("we can't add the element in the list");
                }       
                
                return element[index];
               
        }
}
class  Array
{
        public static void main(String args[])
        {
                Scanner s=new Scanner(System.in);
                int n,c,index,i;
                Object value;
                System.out.println("Enter array size");
                n=s.nextInt();
                List a=new List(n);
                System.out.println("Enter"+ n +"array elements");
                for(i=0;i
                {
                        value=s.nextInt();
                        a.add(i,value);
                }
                do
                {
                        System.out.println("Array Linear List Operations");
                   System.out.println("\n 1.add()\n2.remove()\n3.indexOf()\n4.getvalue()\n5.empty()\n6.size()\n7.display()\n enter ur choice");
                        c=s.nextInt();
                        switch(c)
                        {
                                case 1:
                                        System.out.println("Enter index and value");
                                        index=s.nextInt();
                                        value=s.nextInt();
                                        a.add(index,value);
                                        break;
                                case 2:
                                        System.out.println("Enter the index");
                                        index=s.nextInt();
                                        System.out.println("The deleted element is="+a.remove(index));
                                        break;
                                case 3:
                                        System.out.println("Enter a value for searching in the list");
                                        value=s.nextInt();
                                        System.out.println("The index is="+a.indexOf(value));
                                        break;
                                case 4:
                                        System.out.println("Enter the index");
                                        index=s.nextInt();
                                        System.out.println("The  index element is="+a.get(index));
                                        break;
                                case 5:
                                        if(a.empty())
                                                System.out.println("List is empty");
                                        else
                                                System.out.println("List is not empty");
                                        break;
                                case 6:
                                        System.out.println("The size is="+a.size());
                                        break;
                                case 7:
                                        a.display();
                                        break;
                        }
                }while(c<=7);
               
        }
}
Output:
Enter array size
4
Enter4array elements
10  20   30   40
Array Linear List Operations
1.add()
2.remove()
3.indexOf()
4.getvalue()
5.empty()
6.size()
7.display()
 enter ur choice
1                                                               
Enter index and value
0  60  
we can't add the element
 Array Linear List Operations
1.add()  
2.remove() 
3.indexOf()
4.getvalue()
5.empty()    
6.size()
7.display()
Enter ur choice                                                                7                                                                               [10,20,30,40,]
Array Linear List Operations
1.add() 
2.remove()
3.indexOf() 
4.getvalue() 
5.empty()
6.size()
7.display()
 Enter ur choice                                                                                                                                                
 2                                                                            
Enter the index
0                     
The deleted element is=10
Array Linear List Operations
1.add()
2.remove()  
3.indexOf()  
4.getvalue() 
5.empty()
6.size()
7.display()
Enter ur choice
7


No comments:

Post a Comment

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts