MATLAB Object-Oriented Programming

(Joyce) #1

Remove a Node


The removeNode method removes a node from a list and reconnects the remaining
nodes. The insertBefore and insertAfter methods always call removeNode on the
node to insert before attempting to connect it to a linked list.


Calling removeNode ensures that the node is in a known state before assigning it to the
Next or Prev property:


function removeNode(node)
if ~isscalar(node)
error('Input must be scalar')
end
prevNode = node.Prev;
nextNode = node.Next;
if ~isempty(prevNode)
prevNode.Next = nextNode;
end
if ~isempty(nextNode)
nextNode.Prev = prevNode;
end
node.Next = dlnode.empty;
node.Prev = dlnode.empty;
end


For example, suppose that you remove n2 from a three-node list (n1–n2–n3):


n2.removeNode;


Implementing Linked Lists with Classes
Free download pdf