/// <summary><para>Removes the value at the provided index</para> /// <para>returns the value removed</para></summary> public T RemoveAt(int index) { T temp = default(T); if (index > _size) { throw new IndexException(index); } if (index == 0) { temp = Head.Value; Head = Head.Next; _size--; return(temp); } var runner = Head; for (int i = 1; i < index; i++) { runner = runner.Next; } temp = runner.Next.Value; runner.Next = runner.Next.Next; _size--; return(temp); }
/// <summary>Quickly adds a value to the start of the list</summary> public void Add(T value) { SLNode <T> node = new SLNode <T>(value); node.Next = Head; Head = node; _size++; }
// overloaded so a user can add a value to the beginning of the list like so // SLL<int> list1 = new SLL<int>(){ 1, 2, 3}; // list1 = 4 + list1; public static SLL <T> operator +(T value, SLL <T> list) { SLNode <T> node = new SLNode <T>(value); node.Next = list.Head; list.Head = node; list._size++; return(list); }
public bool MoveNext() { if (Runner == null) { Runner = Head; } else { Runner = Runner.Next; } return(Runner != null); }
/// <summary>Reverse the order of values in the SLL</summary> public void Reverse() { SLNode <T> prev = null, curr = Head, next = null; while (curr != null) { next = curr.Next; curr.Next = prev; prev = curr; curr = next; } Head = prev; }
/// <summary>Slowly adds a value to the end of the list</summary> public void Push(T value) { var node = new SLNode <T>(value); if (Head == null) { Head = node; return; } var runner = Head; while (runner.Next != null) { runner = runner.Next; } runner.Next = node; _size++; }
/// <summary><para>Removes the first instance of the value from the list</para> /// <para>returns true if a value is removed false otherwise</para></summary> public bool Remove(T val) { var runner = Head; if (runner.Value.Equals(val)) { Head = Head.Next; _size--; return(true); } while (runner.Next != null) { if (runner.Next.Value.Equals(val)) { runner.Next = runner.Next.Next; _size--; return(true); } runner = runner.Next; } return(false); }
public void Reset() { Runner = null; }
public SLLEnumumerator(SLNode <T> head) { Head = head; }