MATLAB Object-Oriented Programming

(Joyce) #1

  • Objects enforce rules that control how objects interact.


To illustrate these advantages, consider the implementation of a data structure called a
doubly linked list. See “Implementing Linked Lists with Classes” on page 3-31 for the
actual implementation.

Here is a diagram of a three-element list:

n3

Properties
Next
Prev

n2

Properties
Next
Prev

n1

Properties
Next
Prev

n2.Prev n2 n2.Next

To add a node to the list, disconnect the existing nodes in the list, insert the new node,
and reconnect the nodes appropriately. Here are the basic steps:

First disconnect the nodes:

(^1) Unlink n2.Prev from n1
(^2) Unlink n1.Next from n2
Now create the new node, connect it, and renumber the original nodes:
(^1) Link new.Prev to n1
(^2) Link new.Next to n3 (was n2)
(^3) Link n1.Next to new (will be n2)
4 Link n3.Prev to new (will be n2)
n4
Properties
Next
Prev
n3
Properties
Next
Prev
n2
Properties
Next
Prev
n1
Properties
Next
Prev
Newly inserted node
1 Using Object-Oriented Design in MATLAB

Free download pdf