// AddObject- Add an object to the list after a particular node. public LLNode AddObject(LLNode previousNode, object toAdd) { // Create a new node with the object attached. LLNode newNode = new LLNode(toAdd); // Start with the easiest case: // If the list is empty then... if (_head == null && _tail == null) { // ...start off with just the one node in the list. _head = newNode; _tail = newNode; return newNode; } // OK, are we adding the new node to the middle of the list? if (previousNode != null && previousNode._forward != null) { // Just switch the pointers around and we're done. LLNode nextNode = previousNode._forward; // First, store the forward pointers. newNode._forward = nextNode; previousNode._forward = newNode; // Now the backward pointers. nextNode._backward = newNode; newNode._backward = previousNode; return newNode; } // Are we adding it to the beginning? if (previousNode == null) { // Make this the head man. LLNode nextNode = _head; newNode._forward = nextNode; nextNode._backward = newNode; _head = newNode; return newNode; } // Must be the end of the list. newNode._backward = previousNode; previousNode._forward = newNode; _tail = newNode; return newNode; }
// Reset - move the iterator back to immediately prior // to the first node in the list public void Reset() { currentNode = null; nextNode = linkedList.head; }
// MoveNext - Move to the next item in the list unless we // have already reached the end. public bool MoveNext() { _currentNode = _nextNode; if (_currentNode == null) { return false; } _nextNode = _nextNode._forward; return true; }
// Reset - Move the iterator back to immediately prior // to the first node in the list. public void Reset() { _currentNode = null; _nextNode = _linkedList._head; }
// RemoveObject - Remove an object from the list. public void RemoveObject(LLNode currentNode) { // Get the current node's neighbors. LLNode previousNode = currentNode._backward; LLNode nextNode = currentNode._forward; // Remove the current object's pointers. currentNode._forward = currentNode._backward = null; // Now... if this was the last element in the list. if (_head == currentNode && _tail == currentNode) { _head = _tail = null; return; } // Ok, if this node is in the middle... if (_head != currentNode && _tail != currentNode) { previousNode._forward = nextNode; nextNode._backward = previousNode; return; } // At the front of the list? if (_head == currentNode && _tail != currentNode) { _head = nextNode; nextNode._backward = null; return; } // Must be at the end of the list. _tail = previousNode; previousNode._forward = null; }