MATLAB Object-Oriented Programming

(Joyce) #1

Class Properties


Only dlnode class methods can set the Next and Prev properties because these
properties have private set access (SetAccess = private). Using private set access
prevents client code from performing any incorrect operation with these properties. The
dlnode class methods perform all the operations that are allowed on these nodes.


The Data property has public set and get access, allowing you to query and modify the
value of Data as required.


Here is how the dlnode class defines the properties:


properties
Data
end
properties(SetAccess = private)
Next = dlnode.empty;
Prev = dlnode.empty;
end


Construct a Node Object


To create a node object, specify the node's data as an argument to the constructor:


function node = dlnode(Data)
if nargin > 0
node.Data = Data;
end
end


Insert Nodes


There are two methods for inserting nodes into the list — insertAfter and
insertBefore. These methods perform similar operations, so this section describes only
insertAfter in detail.


function insertAfter(newNode, nodeBefore)
removeNode(newNode);
newNode.Next = nodeBefore.Next;
newNode.Prev = nodeBefore;
if ~isempty(nodeBefore.Next)
nodeBefore.Next.Prev = newNode;
end
nodeBefore.Next = newNode;
end


Implementing Linked Lists with Classes
Free download pdf