Here we create a linked list package. The package provides the ability to create a new linked list, and prepend to an existing linked list via the New and Prepend exports. Creating a new linked list returns a module node "object".
>
|
LinkedList := module()
option package;
export New, Prepend;
local DisplayNode;
New := proc( data )
module()
export Val, Next;
local ModulePrint;
Val := data;
Next := 0;
ModulePrint := proc()
DisplayNode(thismodule);
end proc;
end module;
end proc;
Prepend := proc( llist, val )
local node;
node := New(val);
node:-Next := eval(llist);
return node;
end proc;
DisplayNode := proc( node )
if node = 0 then
return NULL;
else
return node:-Val, DisplayNode(node:-Next);
end if;
end proc;
end module:
|
Now load the package and create a linked list. Note how the node modules get displayed. The head node is formatted by the ModulePrint command which calls DisplayNode to output the entire linked list.
>
|
|
>
|
|