MATLAB Object-Oriented Programming

(Joyce) #1
How insertAfter Works

First, insertAfter calls the removeNode method to ensure that the new node is not
connected to any other nodes. Then, insertAfter assigns the newNode Next and Prev
properties to the handles of the nodes that are after and before the newNode location in
the list.

For example, suppose that you want to insert a new node, nnew, after an existing node,
n1, in a list containing n1—n2—n3.

First, create nnew:

nnew = dlnode(rand(3));

Next, call insertAfter to insert nnew into the list after n1:

nnew.insertAfter(n1)

The insertAfter method performs the following steps to insert nnew in the list between
n1 and n2:


  • Set nnew.Next to n1.Next (n1.Next is n2):


nnew.Next = n1.Next;


  • Set nnew.Prev to n1


nnew.Prev = n1;


  • If n1.Next is not empty, then n1.Next is still n2, so n1.Next.Prev is n2.Prev,
    which is set to nnew


n1.Next.Prev = nnew;


  • n1.Next is now set to nnew


n1.Next = nnew;

3 MATLAB Classes Overview

Free download pdf