MATLAB Object-Oriented Programming

(Joyce) #1
n3

Properties
Next
Prev

n2

Properties
Next
Prev

n1

Properties
Next
Prev

Disconnect the nodes

removeNode removes n2 from the list and reconnects the remaining nodes with the
following steps:

n1 = n2.Prev;
n3 = n2.Next;
if n1 exists, then
n1.Next = n3;
if n3 exists, then
n3.Prev = n1

The list is rejoined because n1 connects to n3 and n3 connects to n1. The final step is to
ensure that n2.Next and n2.Prev are both empty (that is, n2 is not connected):

n2.Next = dlnode.empty;
n2.Prev = dlnode.empty;

Removing a Node from a List

Suppose that you create a list with 10 nodes and save the handle to the head of the list:

head = dlnode(1);
for i = 10:-1:2
new = dlnode(i);
insertAfter(new,head);
end

Now remove the third node (Data property assigned the value 3 ):

removeNode(head.Next.Next)

Now the third node in the list has a data value of 4 :

3 MATLAB Classes Overview

Free download pdf