Vous êtes sur la page 1sur 8

import java.util.

*;

interface Ilist

void orderedInset(String item);

void insertAtpos(int pos,String item);

void setElementAtpos(int pos,String item);

void DeleteAtpos(int pos);

void deleteElement(String item);

//void disp();

public class List implements Ilist

String a[]=new String[100];

int pos,size;

static int ind;

List(int n)

size=n;

ind=0;

for(int i=0;i<n;i++)

a[i]="\0";

List(List z)

for(int i=0;i<z.size;i++)

if(z.a[i]=="\0")
{

a[i]="\0";

else

a[i]=z.a[i];

public void orderedInset(String item)

try

while(a[ind]!="\0")

ind++;

if(ind>size-1)

throw new Exception("outofrangeException");

a[ind]=item;

ind++;

catch(Exception p)

{
System.out.println(p);

public void insertAtpos(int pos,String item)

try

if(pos>size-1)

throw new Exception("outofrangeException");

else if(a[pos]!="\0")

throw new Exception("positionNotNullException");

a[pos]=item;

catch(Exception e)

System.out.println(e);

public void setElementAtpos(int pos,String item)

try

if(pos>size-1)

throw new Exception("outofrangeException");

a[pos]=item;
}

catch(Exception e)

System.out.println(e);

public void DeleteAtpos(int pos)

try

if(pos>size-1)

throw new Exception("outofrangeException");

else if(a[pos]=="\0")

throw new Exception("alreadyNullException");

if(pos>=ind)

a[pos]="\0";

else if(pos<ind)

a[pos]="\0";

ind=pos;

catch(Exception e)

{
System.out.println(e);

public void deleteElement(String item)

int count=-1;

for(int i=0;i<size;i++)

if(item.equals(a[i]))

count=i;

break;

try{

if(count==-1)

throw new Exception("elementNotFoundException");

catch(Exception e)

System.out.println(e);

if(count>=ind)

a[count]="\0";

else if(count<ind)
{

a[count]="\0";

ind=count;

public void disp()

int k;

for(int i=0;i<size;i++)

k=i+1;

System.out.println(k+a[i]);

public static Scanner sc=new Scanner(System.in);

public static void main(String args[])

int b,pos,r=1;

String item;

System.out.print("enter size of the list :");

b=sc.nextInt();

List l=new List(b);

while(r==1)
{

System.out.print("\n1.orderedInset \n2.insertAtpos \n3.setElementAtpos


\n4.DeleteAtpos(int pos) \n5.deleteElement \n6.display list \n7.copy to new list \n8.exit \nenter
your choice:");

int c=sc.nextInt();

switch(c)

case 1:

System.out.println("enter the item;");

item=sc.next();

l.orderedInset(item);

break;

case 2:

System.out.println("enter the item and position;");

item=sc.next();

pos=sc.nextInt();

l.insertAtpos(pos-1,item);

break;

case 3:

System.out.println("enter the item and position;");

item=sc.next();

pos=sc.nextInt();

l.setElementAtpos(pos-1,item);

break;

case 4:

System.out.println("enter the position;");

pos=sc.nextInt();
l.DeleteAtpos(pos-1);

break;

case 5:

System.out.println("enter the item;");

item=sc.next();

l.deleteElement(item);

break;

case 6:

System.out.println("\n");

l.disp();

break;

case 7:

List l1=new List(l);

l1.disp();

break;

case 8:

System.exit(0);

}}

Vous aimerez peut-être aussi