Design your own List ADT named StringList to provide the fol
Design your own List ADT named StringList to provide the following operations on a list object: 1. add (void add(String item) 2. remove (void remove(int index) and void remove(String item)) 3. size() (int size()) 4. isEmpty (boolean isEmpty() ) 5. indexOf (int indexOf(String item)) 6. hasItem (boolean hasItem(String item) ) 7. isFull (boolean isFull() ) 8. getByIndex (String getByIndex(int index)) 9. toString (String toString(), which returns items in the list in the format as specified below: [Bottle water, Turkey, Oil, Banana] You are required to use array to implement this ADT. Array only, no other data structures can be used to implement the structure.
Solution
public class ADT
{
String a[] = new String[1000];
int len=0;
void add(String s)//add item to array
{
a[len++]=s;
System.out.println(\"\ data added to array\ \");
}
void remove(String s)//remove item from array
{
int i,j;
i = indexOf(s);
if(i!=-1){
for(j=i;j<len-1;j++)
{
a[j]=a[j+1];
}
a[j]=\"\";len--;
}
}
void remove(int i)//remove item at index from array
{
int j;
for(j=i;j<len-1;j++)
{
a[j]=a[j+1];
}
a[j]=\"\";len--;
}
int size()//returning size
{
return len;
}
boolean isEmpty()//checking whether empty or not
{
if(len==0)return true;
return false;
}
int indexOf(String s)//finding index of a string in array
{
int i;
for(i=0;i<len;i++)
{
if(a[i]==s)return i;
}
return -1;
}
boolean hasitem(String s)//checking whether item is present or not
{
int i=indexOf(s);
if(i!=-1)return true;
return false;
}
boolean isfull()//checking whether list is full or not
{
if(len==1000)return true;
return false;
}
String getByindex(int i)//get string by index
{
return a[i];
}
String tostring()//get array in string format..
{
int i;
String s=\"[\";
for(i=0;i<len-1;i++)
s=s+a[i]+\", \";
s=s+a[i]+\" ]\";
return s;
}
public static void main(String argv[])
{
//testing
ADT a =new ADT();
String s;
a.add(\"surya\");
a.add(\"phani\");
s= a.tostring();
System.out.println(\"\ The list:\"+s);
a.add(\"suresh\");
a.remove(\"phani\");
System.out.println(\"\ The list:\"+s);
a.remove(1);
a.add(\"rk\");
System.out.println(\"\ The list:\"+s);
System.out.println(\"value at index 1:\"+a.getByindex(1));
System.out.println(\"\ checking whether list is full:\"+a.isfull());
System.out.println(\"\ checking whether list is empty:\"+a.isEmpty());
System.out.println(\"\ checking SIZE OF LIST:\"+a.size());
System.out.println(\"\ checking whether the item is present in list:\"+a.hasitem(\"surya\"));
}
}
ouput:-
run:
data added to array
data added to array
The list:[surya, phani ]
data added to array
The list:[surya, phani ]
data added to array
The list:[surya, phani ]
value at index 1:rk
checking whether list is full:false
checking whether list is empty:false
checking SIZE OF LIST:2
checking whether the item is present in list:true
BUILD SUCCESSFUL (total time: 0 seconds)


