Ordered/Unordered Lists 100 points Modify the supplied class UnorderedArrayList
ID: 3742961 • Letter: O
Question
Ordered/Unordered Lists 100 points Modify the supplied class UnorderedArrayList the following ways: a. the method remove) removes an element from the list by shifting the elements of the ist. However, if the element to be removed is at the beginning of the list and the list is fairly large, it could take a lot of computer time to perform the operation. Because the list elements are in no particular order, you could simply remove the element by copying the last element in the list at the position of the item to be removed and reducing the length of the list. the method remove removes only the first occurrence of an element. Add the method removeAll0 that will remove all occurrences of a given element. add the methods min) and max() which will return the smallest and largest respective elements in the list add an insertion sort method that puts the list in order. b. c. d. e. write a Main) method that thoroughly tests these modifications and demonstrates correctness Evaluation: remove) removeAll(O min) and Max) insertionSort) Testing 20 points 20 points 20 points 20 points 20 pointsExplanation / Answer
public class UnorderedArrayList
{
protected int[] list;
protected int next;
public UnorderedArrayList()
{
list=new int[100];
next=0;
}
public void insert(ref int item)
{
list[next]=item;
next++;
}
public int removed(int start,ref int item)//Some changes in remove function
{
if(next==0)
{return -1;}
else
{
for(int i=start;i<next;i++)
{
if(list[i]==item)
{
list[i]=list[next-1];
next--;
return i;
}
}
}
return -1;
}
public void removeall(ref int item)//REMOVE ALL OCCURENCE
{
int start=0;
if(start=removed(start,ref item)==-1)
{
Console.WriteLine(" All Item Removed ");
}
else
{
removed(start,ref item);
}
}
public void insertion_sort()//INSERTION SORT
{
int i, key, j;
for (i = 1; i < next; i++)
{
key = list[i];
j = i-1;
while (j >= 0 && list[j] > key)
{
list[j+1] = list[j];
j = j-1;
}
list[j+1] = key;
}
}
public void print()
{
for(int i=0;i<next;i++)
{
Console.WriteLine(list[i]);
}
Console.WriteLine();
}
}
//SOME CHANGES IN MAIN FUNCTION
public static void Main(string[] args)
{
UnorderedArrayList u = new UnorderedArrayList();
//u.print();
int val = 5;
u.insert(ref val);
val = 12;
u.insert(ref val);
val = 2;
u.insert(ref val);
val = 29;
u.insert(ref val);
u.print();
val = 5;
u.removed(0,ref val);
u.print();
val = 12;
u.insert(ref val);
val = 2;
u.insert(ref val);
val = 29;
u.insert(ref val);
u.print();
u.removeall(ref val);
u.print();
val = 12;
u.removeall(ref val);
u.print();
val = 12;
u.insert(ref val);
val = 2;
u.insert(ref val);
val = 29;
u.insert(ref val);
val = 12;
u.insert(ref val);
val = 15;
u.insert(ref val);
u.insertion_sort();
u.print();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.