/// <summary> /// Add a new Node to the list. /// </summary> public void Add(T item) { // This is a more verbose implementation to avoid adding nodes to the head of the list var node = new NSLinkedListNode <T> { Value = item }; if (_head == null) { // This is the first node. Make it the head _head = node; } else { // This is not the head. Make it current's next node. _current.Next = node; } // Makes newly added node the current node _current = node; _size++; // This implementation is simpler but adds nodes in reverse order. It adds nodes to the head of the list }
/// <summary> /// Insert the specified index and item. /// </summary> /// <returns>The insert.</returns> /// <param name="index">Index.</param> /// <param name="item">Item.</param> public void Insert(int index, T item) { IndexInRangeCheck(index); var newNode = new NSLinkedListNode <T>(); newNode.Value = item; if (index == 0) { newNode.Next = _head; _head = newNode; } else { var aux = _head; while (index > 1) //for(int i = 0; i < posicion; i++) {nodoAux = nodoAux.Siguiente;} { aux = newNode.Next; index--; } newNode.Next = aux.Next; aux.Next = newNode; } _size++; }
/// <summary> /// Find the specified value. /// </summary> /// <returns>The find.</returns> /// <param name="value">Value.</param> internal NSLinkedListNode <T> Find(T value) { NSLinkedListNode <T> node = _head; EqualityComparer <T> c = EqualityComparer <T> .Default; if (node != null) { if (value != null) { do { if (c.Equals(node.Value, value)) { return(node); } node = node.Next; } while (node != null); } else { do { if (node.Value == null) { return(node); } node = node.Next; } while (node != null); } } return(null); }
/// <summary> /// Indexs the of. /// </summary> /// <returns>The of.</returns> /// <param name="item">Item.</param> public int IndexOf(T item) { if (Count == 0) { return(-1); } EqualityComparer <T> c = EqualityComparer <T> .Default; int index = 0; if (item == null) { for (NSLinkedListNode <T> x = _head; x != null; x = x.Next) { if (x.Value == null) { return(index); } index++; } } else { for (NSLinkedListNode <T> x = _head; x != null; x = x.Next) { if (c.Equals(item, x.Value)) { return(index); } index++; } } return(-1); }
/// <summary> /// Removes at index. /// </summary> /// <param name="index">Index.</param> public void RemoveAt(int index) { IndexInRangeCheck(index); if (index == 0) { // removing the first element must be handled specially _head = _head.Next; } else { // removing some element further down in the list; // traverse to the node before the one we want to remove var pointer = _head; for (int i = 0; i < index - 1; i++) { pointer = pointer.Next; } // change its next pointer to skip past the offending node pointer.Next = pointer.Next.Next; } _size--; }
public NSList() { _head = null; }
/// <summary> /// Clear this instance. /// </summary> public void Clear() { _head = null; _size = 0; }
/// <summary> /// Determines whether the specified NSNode is equal to the current node. /// </summary> /// <param name="other">The NSNode to compare with the current node.</param> /// <returns><c>true</c> if the specified NSNode is equal to the current /// node; otherwise, <c>false</c>.</returns> public bool Equals(NSLinkedListNode <T> other) { return(Value.Equals(other)); }