public void RemoveAt(int index) { if (index == 0) { core = core.Next; } else { if (index >= this.Count) { throw new IndexOutOfRangeException(); } ListComponent <T> pointer = core; for (int i = 0; i < index - 1; i++) { if (pointer is NullElement <T> ) { throw new IndexOutOfRangeException(); } pointer = pointer.Next; } ListComponent <T> afterCompontent = pointer.Next.Next; pointer.PushNext(afterCompontent); } }
/// <summary> /// You may think this is the traditional way to seek element via index... But... /// Let's have a f*cking Python-like code in C# and make possible to get element via negative index. /// If you've never coded on Python before: -i index = element with i steps position from end. /// </summary> /// <param name="index">Any integer number</param> /// <returns>Element, stored by this index</returns> public T this[int index] { get { if (index >= 0) { int i = 0; ListComponent <T> pointer = core; while (!(pointer is NullElement <T>)) { if (i == index) { return(pointer.Value); } pointer = pointer.Next; i++; } throw new IndexOutOfRangeException(); } else { return(this[Count - (Math.Abs(index) % (Count - 1))]); } } set { if (index >= 0) { int i = 0; ListComponent <T> pointer = core; if (pointer is NullElement <T> ) { throw new IndexOutOfRangeException(); } while (!(pointer.Next is NullElement <T>)) { if (i == index) { pointer.Value = value; return; } pointer = pointer.Next; i++; } throw new IndexOutOfRangeException(); } else { this[Count - (Math.Abs(index) % (Count - 1))] = value; } } }
public override void PopEnd() { ListComponent <T> pointer = this; while (!(pointer.Next is NullElement <T>)) { pointer = pointer.Next; } pointer.PopNext(); }
public override void PushEnd(ListComponent <T> component) { ListComponent <T> pointer = this; while (!(pointer.Next is NullElement <T>)) { pointer = pointer.Next; } pointer.PushNext(component); }
public bool Remove(T item) { ListComponent <T> pointer = core; for (int i = 0; i < this.Count; i++) { if (pointer.Value.Equals(item)) { this.RemoveAt(i); return(true); } pointer = pointer.Next; } return(false); }
public bool Contains(T item) { ListComponent <T> pointer = core; while (!(pointer is NullElement <T>)) { if (pointer.Value.Equals(item)) { return(true); } pointer = pointer.Next; } return(false); }
public int IndexOf(T item) { int i = 0; ListComponent <T> pointer = core; while (!(pointer is NullElement <T>)) { if (pointer.Value.Equals(item)) { return(i); } pointer = pointer.Next; i++; } return(-1); }
public void Add(T item) { if (core is NullElement <T> ) { core = new ListComposite <T>() { Value = item } } ; else { core.PushEnd(new ListComposite <T>() { Value = item }); } }
public void Insert(int index, T item) { ListComponent <T> pointer = core; for (int i = 0; i < index; i++) { if (pointer is NullElement <T> ) { throw new IndexOutOfRangeException(); } pointer = pointer.Next; } ListComponent <T> afterCompontent = pointer.Next; ListComponent <T> intermediateComp = new ListComposite <T>() { Value = item }; intermediateComp.PushNext(afterCompontent); pointer.PushNext(intermediateComp); }
public override void PushNext(ListComponent <T> component) { next = component; }
public void Clear() { core = new NullElement <T>(); }
public virtual void PushEnd(ListComponent <T> component) { }
public virtual void PushNext(ListComponent <T> component) { }