/// <summary> /// default constructor /// </summary> public LList() { first = default; last = default; current = default; Count = default; }
/// <summary> /// Add new node at the end of the list /// </summary> /// <param name="lastItem">data that is to be added to the end of the list<</param> public void AddBack(T lastItem) { //TODO: need to find out how to get invalid type exception on generics //create new node with the lastItem data LListNode toAdd = new LListNode(lastItem); //since this node is being added to the end of the list, the next node does not need to be assigned //because when we created the node, the next and previous nodes automatically is set to null. //check to see if there is already a last node if (last != null) { //since this node is being added to the end of the list, the prev node needs to be the orignal last node toAdd.Prev = last; //add reference to the new node as the next of the original last node last.Next = toAdd; } else { //if this is the first node to be added to the list then the previous and next node is null //and we need to make the new node both the first and last node first = toAdd; } //make the new node the first node of the list last = toAdd; //increase the count of the List count++; }
public void TestEnumerator() { LListNode <string> .LList list = new LListNode <string> .LList(); list.AddLast("B"); list.AddFirst("A"); System.Collections.Generic.IEnumerator <LListNode <string> > e = list.GetEnumerator(); using (e) { Assert.IsTrue(e.MoveNext()); Assert.IsTrue(ReferenceEquals(e.Current, ((System.Collections.IEnumerator)e).Current)); Assert.AreEqual("A", e.Current.Value); list.Remove(e.Current); Assert.IsTrue(e.MoveNext()); Assert.IsTrue(ReferenceEquals(e.Current, ((System.Collections.IEnumerator)e).Current)); Assert.AreEqual("B", e.Current.Value); list.Remove(e.Current); Assert.IsFalse(e.MoveNext()); Assert.IsTrue(list.IsEmpty); list.AddLast(String.Empty); e.Reset(); Assert.IsTrue(e.MoveNext()); Assert.AreEqual(String.Empty, e.Current.Value); Assert.IsFalse(e.MoveNext()); try { GC.KeepAlive(e.Current); Assert.Fail(); } catch (InvalidOperationException) { } } //now e is disposed... try { e.MoveNext(); Assert.Fail(); } catch (ObjectDisposedException) { } try { GC.KeepAlive(e.Current); Assert.Fail(); } catch (ObjectDisposedException) { } try { e.Reset(); Assert.Fail(); } catch (ObjectDisposedException) { } }
/// <summary> /// Default Constructor for empty node /// </summary> public LListNode() { //when empty node is made we need to set Item data to it's default value Item = default; //next we put a default value to the next and previous nodes next = null; previous = null; }
/// <summary> /// constructor for the Node nested class. /// </summary> /// <param name="dataType">The data type that is passed into the LList T parameter</param> public LListNode(T dataType) { //when a node is initially made, //we need to save the date into the node Item = dataType; //next we put a default value to the next and previous nodes. next = null; previous = null; }
/// <summary> /// This allows for the List to loop through the list with a foreach loop /// </summary> /// <returns></returns> public IEnumerator <T> GetEnumerator() { current = first; while (current != null) { yield return(current.Item); current = current.Next; } }
public void TestAddFirst() { LListNode <string> .LList list = new LListNode <string> .LList(); Assert.IsTrue(list.IsEmpty); list.AddLast("B"); Assert.IsFalse(list.IsEmpty); list.AddFirst("A"); Assert.AreEqual("A", list.First.Value); Assert.AreEqual("B", list.Last.Value); }
public T Find(Func <T, bool> condition) { LListNode currentNode = _firstNode; while (currentNode != null) { if (condition.Invoke(currentNode.Value)) { return(currentNode.Value); } currentNode = currentNode.Next; } return(default);
public void TestLinks() { LListNode <string> empty = new LListNode <string>(); LListNode <string> valueA = new LListNode <string>("A"); LListNode <string> valueB = new LListNode <string>("B"); LListNode <string> .LList list = new LListNode <string> .LList(); list.AddFirst(empty); Assert.AreEqual(1, list.Count); Assert.AreEqual(empty, list.First); Assert.AreEqual(empty, list.Last); Assert.IsNull(empty.Next); Assert.IsNull(empty.Previous); list.AddLast(valueB); Assert.AreEqual(2, list.Count); Assert.AreEqual(empty, list.First); Assert.AreEqual(valueB, list.Last); Assert.AreEqual(valueB, empty.Next); Assert.IsNull(empty.Previous); Assert.AreEqual(empty, valueB.Previous); Assert.IsNull(valueB.Next); list.AddFirst(valueA); Assert.AreEqual(3, list.Count); Assert.AreEqual(valueA, list.First); Assert.AreEqual(valueB, list.Last); Assert.AreEqual(valueB, empty.Next); Assert.AreEqual(valueA, empty.Previous); Assert.AreEqual(empty, valueA.Next); Assert.IsNull(valueA.Previous); Assert.AreEqual(empty, valueB.Previous); Assert.IsNull(valueB.Next); list.Remove(empty); Assert.AreEqual(2, list.Count); Assert.AreEqual(valueA, list.First); Assert.AreEqual(valueB, list.Last); Assert.AreEqual(valueB, valueA.Next); Assert.IsNull(valueA.Previous); Assert.AreEqual(valueA, valueB.Previous); Assert.IsNull(valueB.Next); }
/// <summary> /// Compares Node items in list to findItem and returns the item if it is in the list or default if it doesn't /// </summary> /// <param name="findItem"></param> public LListNode Find(T findItem) { for (int i = 0; i < Count - 1; i++) { current = first; if (current.Item.Equals(findItem)) { //found = true; //break; return(current); } } return(null); }
public void LListAddLast() { var list1 = new LList <int>(); list1.AddLast(1); list1.AddLast(2); Console.WriteLine("list1: " + list1.ToString()); var list2 = new LList <int>(); var node1 = new LListNode <int>(1); var node2 = new LListNode <int>(2); list2.AddLast(node1); list2.AddLast(node2); Console.WriteLine("list2: " + list2.ToString()); }
public void LListAddFirst() { var list1 = new LList <string>(); var list2 = new LList <string>(); var node1 = new LListNode <string>("node1"); var node2 = new LListNode <string>("node2"); list1.AddFirst("111"); list1.AddFirst("222"); list2.AddFirst(node1); list2.AddFirst(node2); Console.WriteLine("List1: " + list1.ToString()); Console.WriteLine("List2: " + list2.ToString()); }
public void AddBefore(T after, T item) { LListNode node = FindFirstNode(after); if (node == null) { return; } LListNode nodeLast = node.Last; node.Next = new LListNode { Value = item, Next = node, Last = nodeLast }; nodeLast.Next = node.Last; }
public void AddAfter(T after, T item) { LListNode node = FindFirstNode(after); if (node == null) { return; } LListNode nodeNext = node.Next; node.Next = new LListNode { Value = item, Next = nodeNext, Last = node }; nodeNext.Last = node.Next; }
/// <summary> /// Find the LListNode containing the maximum value stored in the LList /// </summary> /// <returns>LListNoce.Item</returns> public T Max() { //set starting point at beginning of list current = first; //create temp minimum var max = first.Next; for (int i = 0; i < Count - 1; i++) { //if max is < current then max = current if (max.Compare(max.Item, current.Item) != 0) { max = current; } //go to next node current = current.Next; } return(max.Item); }
/// <summary> /// Find the LListNode containing the minimum value stored in the LList /// </summary> /// <returns>LListNode.Item</returns> public T Min() { //set starting point at beginning of list current = first; //create temp minimum var min = first.Next; for (int i = 0; i < Count; i++) { //if min is > current then min = current if (min.Compare(min.Item, current.Item) != 0) { min = current; } //go to next node current = current.Next; } return(min.Item); }
/// <summary> /// Deletes element from list /// </summary> /// <param name="delItem">data that is to be deleted from the list</param> public void Delete(T delItem) { for (int i = 0; i < Count - 1; i++) { current = first; if (current.Item.Equals(delItem)) { //copy the node to be deleted into a temp variable var toDelete = current; //update the next and previous references to the two nodes on either side of the node to be deleted current.Next.Prev = current.Prev; current.Prev.Next = current.Next; //remove the references of the next and previous nodes on the node to be deleted toDelete.Next = default; toDelete.Prev = default; //decrease the list node count Count--; } } }
public bool MoveNext() { _currentNode = _currentNode.Next; return(_currentNode.Next != null); }
public void Reset() => _currentNode = _list._firstNode;