Question
C# Ordered/Unordered Lists Modify the supplied class UnorderedArrayList the following ways: a . the method remove() removes an element from the list by shifting the
C#
Ordered/Unordered Lists
Modify the supplied class UnorderedArrayList the following ways:
a. the method remove() removes an element from the list by shifting the elements of the list. 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.
b. the method remove removes only the first occurrence of an element. Add the method removeAll() that will remove all occurrences of a given element.
c. add the methods min() and max() which will return the smallest and largest respective elements in the list
d. add an insertion sort method that puts the list in order.
e. write a Main() method that thoroughly tests these modifications and demonstrates correctness.
Files are:
//Programs.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using UnorderedArrayListNamespace;
namespace test { class Program { static void Main(string[] args) { UnorderedArrayList u = new UnorderedArrayList(); u.print(); int var = 5; u.insert(ref var); var = 12; u.insert(ref var); var = 2; u.insert(ref var); var = 29; u.insert(ref var); u.print(); var = 5; u.remove(ref var); u.print(); } } }
//UnorderedArrayList.cs
using System;
namespace UnorderedArrayListNamespace { 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 void remove(ref int item) { if (next == 0) { } else { //find value, if it exists for (int i = 0; i < next; i++) { if (item.Equals(list[i])) { for (int j = i; j < next; j++) list[j] = list[j + 1]; next--; break; } } } }
public void print() { for (int i = 0; i < next; i++) { Console.WriteLine(list[i]); } Console.WriteLine(); } }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started