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
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 IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } public static MyList 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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
