Write 1 or 2 programs in C++ to perform an experiment in order to discover which
ID: 640497 • Letter: W
Question
Write 1 or 2 programs in C++ to perform an experiment in order to discover which Data Structure has a faster Delete operation
Note: The program must be in C++. The data structures to be compared are Linked List and Vectors.
Explanation / Answer
/// /// Removes the specified item. /// /// The item. /// public bool Remove(T item) { // Replace index of value to remove with last item in values list int keyIndex = this.index[item]; T lastItem = this.items[this.items.Count - 1]; this.items[keyIndex] = lastItem; // Update index in dictionary for last item that was just moved this.index[lastItem] = keyIndex; // Remove old value this.index.Remove(item); this.items.RemoveAt(this.items.Count - 1); return true; } /// public bool Contains(T item) { return this.index.ContainsKey(item); } /// public void Clear() { this.index.Clear(); this.items.Clear(); } /// public int Count { get { return this.items.Count; } } /// public void CopyTo(T[] array, int arrayIndex) { this.items.CopyTo(array, arrayIndex); } /// public bool IsReadOnly { get { return false; } } /// public IEnumerator GetEnumerator() { foreach (var value in this.items) { yield return value; } } /// IEnumerator IEnumerable.GetEnumerator() { return this.GetEnumerator(); } /// public void CopyTo(Array array, int index) { this.CopyTo(array as T[], index); } /// public bool IsSynchronized { get { return false; } } /// public object SyncRoot { get { if (this.syncRoot == null) { Interlocked.CompareExchange( ref this.syncRoot, new object(), null); } return this.syncRoot; } } } using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Threading; /// /// This class represents an unordered bag of items with the /// the capability to get a random item. All operations are O(1). /// /// The type of the item. public class Bag : ICollection, IEnumerable, ICollection, IEnumerable { private Dictionary index; private List items; private Random rand; private object syncRoot; /// /// Initializes a new instance of the class. /// public Bag() : this(0) { } /// /// Initializes a new instance of the class. /// /// The capacity. public Bag(int capacity) { this.index = new Dictionary(capacity); this.items = new List(capacity); } /// /// Initializes a new instance of the class. /// /// The collection. public Bag(IEnumerable collection) { this.items = new List(collection); this.index = this.items .Select((value, index) => new { value, index }) .ToDictionary(pair => pair.value, pair => pair.index); } /// /// Get random item from bag. /// /// Random item from bag. /// /// The bag is empty. /// public T Random() { if (this.items.Count == 0) { throw new InvalidOperationException(); } if (this.rand == null) { this.rand = new Random(); } int randomIndex = this.rand.Next(0, this.items.Count); return this.items[randomIndex]; } /// /// Adds the specified item. /// /// The item. public void Add(T item) { this.index.Add(item, this.items.Count); this.items.Add(item); }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.