Friday, 23 January 2015

implement the List ADT using Arrays

import java.io.*;
interface ListADT
{
void traversal();
boolean isFull();
boolean isEmpty();
int search(int key);
void insertAtBeg(int item);
void insertAtEnd(int item);
void insertAtPos(int pos,int item);
void insertAfterPos(int pos,int item);
void insertAfterKey(int key,int item);
void delete(int key);
void deletePos(int pos);
}
class ArrayDS implements ListADT
{
int a[],n,max;
ArrayDS(int cap)
{
max=cap;
a=new int[max];
n=0;
}
public void traversal()
{
for(int i=0;i<n;i++)
System.out.print(a[i]+" ");
System.out.println("\n");
}
public boolean isFull()
{
if(n==max)
return true;
return false;
}
public boolean isEmpty()
{
if(n==0)
return true;
return false;
}
public int search(int key)
{
for(int i=0;i<n;i++)
if(a[i]==key)
return i;
return -1;
}
public void insertAtBeg(int item)
{
if(!isFull())
{
for(int i=n-1;i>=0;i--)
a[i+1]=a[i];
a[0]=item;
n++;
}
else
System.out.println("Array is Full, cannot insert");
}
public void insertAtEnd(int item)
{
if(!isFull())
{
a[n++]=item;
}
else
System.out.println("Array is Full, cannot insert");
}
public void insertAtPos(int pos,int item)
{
if(!isFull())
{
for(int i=n-1;i>=pos;i--)
a[i+1]=a[i];
a[pos]=item;
n++;
}
else
System.out.println("Cannot insert, Array is Full");
}
public void insertAfterPos(int pos,int item)
{
if(pos<n)
insertAtPos(pos+1,item);
else
System.out.println("Array is Full, cannot insert");
}
public void insertAfterKey(int key,int item)
{
int p=search(key);
if(p==-1)
System.out.println("Cannot insert");
else
insertAfterPos(p,item);
}
public void delete(int key)
{
if(!isEmpty())
{
int p=search(key);
deletePos(p);
}
else
System.out.println("Array is Empty");
}
public void deletePos(int pos)
{
if(!isEmpty())
{
if(pos<0||pos>n-1)
{
System.out.println("Cannot delete, Element not found");
}
else
{
for(int i=pos;i<n-1;i++)
a[i]=a[i+1];
n--;
}
}
else
System.out.println("Array is Empty");
}
}
class ArrayTest
{
public static void main(String arg[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the size of the Array");
int n=Integer.parseInt(br.readLine());
ArrayDS ar=new ArrayDS(n);
System.out.println("Array is created");
ar.traversal();
int t,k,m=1;
do
{
System.out.println("Enter ur choice of operation");
System.out.println("1-insertAtBeg\n2-insertAtEnd\n3-insertAtPos\n4-insertAfterPos\n5-insertAfterKey\n6-deleteItem\n7-deletePos\n0-exit");
n=Integer.parseInt(br.readLine());
switch(n)
{
case 1:
System.out.println("Enter the element to be inserted");
t=Integer.parseInt(br.readLine());
ar.insertAtBeg(t);
ar.traversal();
break;
case 2:
System.out.println("Enter the element to be inserted");
t=Integer.parseInt(br.readLine());
ar.insertAtEnd(t);
ar.traversal();
break;
case 3:
System.out.println("Enter the element to be inserted:");
t=Integer.parseInt(br.readLine());
System.out.println("Enter the index position to insert at:");
k=Integer.parseInt(br.readLine());
ar.insertAtPos(k,t);
ar.traversal();
break;
case 4:
System.out.println("Enter the element to be inserted");
t=Integer.parseInt(br.readLine());
System.out.println("Enter the index position to insert after");
k=Integer.parseInt(br.readLine());
ar.insertAfterPos(k,t);
ar.traversal();
break;
case 5:
System.out.println("Enter the element to be inserted:");
t=Integer.parseInt(br.readLine());
System.out.println("Enter the key element to insert aftter:");
k=Integer.parseInt(br.readLine());
ar.insertAfterKey(k,t);
ar.traversal();
break;
case 6:
System.out.println("Enter the element to be deleted");
t=Integer.parseInt(br.readLine());
ar.delete(t);
ar.traversal();
break;
case 7:
System.out.println("Enter the position to be deleted");
t=Integer.parseInt(br.readLine());
ar.deletePos(t);
ar.traversal();
break;
case 0:
m=0;
break
default:
System.out.println("INVALID CHOICE");
}
}while(m==1);
ar.traversal();
}
}









































No comments:

Post a Comment