Question: Please add/modify the following to the original code using C# 1. DeleteFirst = Count) throw new IndexOutOfRangeException(); array[i] = value; } } public void Clear()//O(1)

Please add/modify the following to the original code using C#

1. DeleteFirst <-- deletes the first value from the list (if it exists!)

2. DeleteLast <-- deletes the last value from the list (if it exists!)

3. DeleteValue(given a value) <- it should delete the first occurrence (if any!) of the value in the array list

4. Delete(given an index)

5. Reverse <-- this should reverse the list, not merely display it!

MODIFY/ADD THE CODE TO THE FOLLOWING AND PLEASE ADD MEANINGFUL COMMENTS TO THE CODE ( I WILL BE SURE TO UPVOTE):

using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks;

namespace Array_Lists { internal class MyList : IEnumerable { //data: fields, properties private T[] array; public int Count { get; private set; }

public int Capacity { get { return array.Length; } }

//actions: methods public bool IsFull() { //if (Count == Capacity) // return true; //else // return false; return Count == Capacity; }

public bool IsEmpty() { return Count == 0; }

public void PrintArray()//O(n) { for(int i= 0; i< Count; i++) { Console.Write(array[i] + ", "); } Console.WriteLine(); }

public void Insert(int index, T newValue)//O(n) { //validation on index if (index < 0 || index > Count) throw new IndexOutOfRangeException();

if (IsFull()) DoubleTheCapacity();

//shift elements to the right for (int i = Count; i > index; i--) { array[i] = array[i - 1]; } //put in your new values array[index] = newValue; Count++; } public T this[int i] { get { // validation on index if (i < 0 || i >= Count) throw new IndexOutOfRangeException(); return array[i]; }

set { // validation on index if (i < 0 || i >= Count) throw new IndexOutOfRangeException(); array[i] = value; } }

public void Clear()//O(1) { //array = new T[Capacity]; Count = 0; }

public void AddFirst(T newValue)//O(n) { Insert(0, newValue); } public void AddLast(T newValue)//O(n) worst case, O(1) on average { //check if there is space in the array if(IsFull()) { DoubleTheCapacity(); } //there is space in the array array[Count] = newValue; Count++; }

public void Add(T newValue) { AddLast(newValue); }

private void DoubleTheCapacity() { //create a larger array, twice capacity T[] newArray = new T[Capacity*2];

//copy old values into the new array for(int i=0; i< Count; i++) { newArray[i] = array[i]; }

//change the array to point to the new array array = newArray; }

public IEnumerator GetEnumerator() { for(int i= 0; i

IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); }

public static MyList operator+ (MyList firstList, MyList secondList) { MyList newList = new MyList(firstList.Capacity + secondList.Capacity);

foreach (var val in firstList) newList.Add(val);

foreach (var val in secondList) newList.Add(val);

return newList; }

//ctors public MyList(int initialCapacity = 1) { //Count = 0; array = new T[initialCapacity]; }

public MyList(MyList oldList) { Count = oldList.Count; array = new T[oldList.Capacity]; for (int i = 0; i < Count; i++) array[i] = oldList[i]; } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!