Program should be different from any other program, Thank you:)
Output should be identical with this
At index 0 there is: 9 At index 1 there is: 8 At index 2 there is: 7 At index 3 there is: 6 At index 4 there is: 5 At index 5 there is: 4 At index 6 there is: 3 At index 7 there is: 2 At index 8 there is: 1 At index 9 there is: 0 At index 10 there is: 0 At index 11 there is: 1 At index 12 there is: 2 At index 13 there is: 3 At index 14 there is: 4 At index 15 there is: 5 At index 16 there is: 6 At index 17 there is: 7 At index 18 there is: 8 At index 19 there is: 9 find(2) returned: 7 find(20) returned: -1 delete(8) returned: 1 delete(15) returned: 6 At index 0 there is: 9 At index 1 there is: 8 At index 2 there is: 7 At index 3 there is: 6 At index 4 there is: 5 At index 5 there is: 4 At index 6 there is: 3 At index 7 there is: 2 At index 8 there is: 0 At index 9 there is: 0 At index 10 there is: 1 At index 11 there is: 2 At index 12 there is: 3 At index 13 there is: 4 At index 14 there is: 5 At index 15 there is: 7 At index 16 there is: 8 At index 17 there is: 9
EXPERT ANSWER
intList.java
public class intList
{
private int list[];
private int count;
// constructor
public intList(int size)
{
list = new int[size];
count = 0;
}
// method to insert a value at the beginning
public boolean insert(int val)
{
if(count<list.length)
{
if(count == 0)
{
list[count] = val;
}
else
{
for(int j=count;j>=0;j--)
{
list[j+1] = list[j];
}
list[0] = val;
}
count++;
return true;
}
else
{
return false;
}
}
// method to add a value at the end
public boolean add(int val)
{
if(count<list.length)
{
list[count] = val;
count++;
return true;
}
else
{
return false;
}
}
// method to delete
public int delete(int index)
{
if(count!=0 && index<count)
{
int temp = list[index];
for(int j=index+1;j<count;j++)
{
list[j-1] = list[j];
}
list[count-1] = 0;
count--;
return temp;
}
else
{
return -1;
}
}
// method to find a value in the list
public int find(int val)
{
for(int i=0;i<count;i++)
{
if(list[i]==val)
{
return i;
}
}
return -1;
}
// method to print the list
public void print()
{
for(int i=0;i<count;i++)
{
System.out.println("At index "+i+" there is: "+list[i]);
}
}
}
p3.java
// class p3 to test the class intList
public class p3
{
public static void main(String[] args)
{
// instantiating
intList myList = new intList(20);
// looping 10 times
for(int i=0;i<10;i++)
{
// adding at front and beginning
myList.insert(i);
myList.add(i);
}
// printing list
myList.print();
// printing the results of find and delete
System.out.println("find(2) returned: "+myList.find(2));
System.out.println("find(20) returned: "+myList.find(20));
System.out.println("delete(8) returned: "+myList.delete(8));
System.out.println("delete(15) returned: "+myList.delete(15));
// printing the list again
myList.print();
}
}
SAMPLE OUTPUT
