public void TestGetPrevious() { Employee emp1 = new Employee(1); Employee emp2 = new Employee(2); Node <Employee> node1 = new Node <Employee>(emp1); Node <Employee> node2 = new Node <Employee>(emp2, node1, null); Assert.That(node2.GetPrevious(), Is.EqualTo(node1)); }
public void TestDataArgContructor() { Employee emp1 = new Employee(1); Node <Employee> node1 = new Node <Employee>(emp1); Assert.That(node1, Is.Not.Null); Assert.That(node1.GetData(), Is.EqualTo(emp1)); Assert.That(node1.GetNext(), Is.Null); Assert.That(node1.GetPrevious(), Is.Null); }
/// <summary> /// Removes the specified node from the linked list /// </summary> /// <param name="node">The node to be removed</param> /// <returns>Data from the node being removed</returns> private T Unlink(Node <T> node) { Node <T> previous = node.GetPrevious(); Node <T> next = node.GetNext(); previous.SetNext(next); next.SetPrevious(previous); size--; return(node.GetData()); }
/// <summary> /// Remove the node at the tail of the linked list and return the data it contains /// </summary> /// <returns>The data stored in the node being removed</returns> private T UnlinkTail() { Node <T> oldTail = tail; tail = tail.GetPrevious(); tail.SetNext(null); size--; return(oldTail.GetData()); }
public void TestAllArgContructor() { Employee emp1 = new Employee(1); Employee emp2 = new Employee(2); Employee emp3 = new Employee(3); Node <Employee> node1 = new Node <Employee>(emp1); Node <Employee> node2 = new Node <Employee>(emp2); Node <Employee> testNode = new Node <Employee>(emp3, node1, node2); Assert.That(testNode, Is.Not.Null); Assert.That(testNode.GetData(), Is.EqualTo(emp3)); Assert.That(testNode.GetNext(), Is.EqualTo(node2)); Assert.That(testNode.GetPrevious(), Is.EqualTo(node1)); }
/// <summary> /// Add a new node containing the passed data to the list before the node position specified /// </summary> /// <param name="data">Data to add</param> /// <param name="position">Numeric position to insert before</param> /// <returns>True when complete</returns> public bool AddBefore(T data, int position) { ValidatePositon(position); // Determine if position is equal to 1 if (position == 1) { LinkHead(data); } else { Node <T> next = Find(position); Node <T> previous = next.GetPrevious(); Link(data, previous, next); } return(true); }
/// <summary> /// Add a new node with the specified data to the link list. This new node will be added after the node containing the oldData. /// </summary> /// <param name="data">The data to be added to the list</param> /// <param name="oldData">Value used to to find the node containing the oldData</param> /// <returns>True when complete</returns> public bool AddBefore(T data, T oldData) { Node <T> node = Find(oldData); // Determine if node is null and throw exception if it is if (node == null) { throw new IndexOutOfRangeException("No such element"); } else { // Determine if the node is the head if (node == head) { LinkHead(data); } else { Link(data, node.GetPrevious(), node); } } return(true); }