The link Lulu suggested in the comments is probably the choice I would make if I were wanting to implement a linked list in MATLAB. However, this approach would stray off into the object-oriented features of MATLAB, which may not be what you want since you mention wanting "to understand the general structure of the language better." As such, you may do better with a simpler example that incorporates general core features of MATLAB programming.
A number of general features have been mentioned in other answers, such as matrices and matrix indexing, creating structures, and using nested functions and function handles. I'll go through an example that makes use of all these features and hopefully gives a nice introduction to a number of key concepts in MATLAB...
Sample code:
Save the code below in a file called linked_list.m
on the MATLAB path:
function listObject = linked_list(values)
data = reshape(values,1,[]);
listObject = struct('display',@display_list,...
'addAfter',@add_element,...
'delete',@delete_element);
function display_list
%# Displays the data in the list
disp(data);
end
function add_element(values,index)
%# Adds a set of data values after an index in the list, or at the end
%# of the list if the index is larger than the number of list elements
index = min(index,numel(data));
data = [data(1:index) reshape(values,1,[]) data(index+1:end)];
end
function delete_element(index)
%# Deletes an element at an index in the list
data(index) = [];
end
end
Description:
The function linked_list
accepts an arbitrary-sized matrix and first reshapes it into a row vector using the RESHAPE function. This becomes the initial "linked list", stored in the variable data
.
Next, a structure is created (using the STRUCT function) which has three elements: display
, addAfter
, and delete
. Each of these fields stores a function handle to one of three functions that is nested within the parent function linked_list
. These nested functions are able to access the variable data
stored in the parent function.
The listObject
structure is returned from linked_list
. As long as this structure exists in the workspace, and thus as long as the function handles it contains exist, then the data
variable will persist even after the function linked_list
returns. We can then invoke the nested functions (using their handles) to modify the variable data
. Here's an example...
First, create a linked list "object" and display the contents:
>> listObj = linked_list([1 2 3]); %# A linked list with three elements
>> listObj.display() %# Access the `display` field and invoke the function
1 2 3
Next, add an element "4" after the second list element and display:
>> listObj.addAfter(4,2) %# Access the `addAfter` field and invoke the function
>> listObj.display()
1 2 4 3
And finally, delete the second list element and display:
>> listObj.delete(2) %# Access the `delete` field and invoke the function
>> listObj.display()
1 4 3
Note how the nested functions add_element
and delete_element
use matrix indexing to modify the variable data
.
You can extend this example to create numerous other nested functions for operating on a linked list, adding new fields to the structure to store their function handles.