.. _Links:
.. raw:: html
.. |--| unicode:: U+2013 .. en dash
.. |---| unicode:: U+2014 .. em dash, trimming surrounding whitespace
:trim:
.. odsalink:: AV/Pointers/linkNodes5CON.css
.. odsalink:: AV/Pointers/linkNodes4CON.css
.. odsalink:: AV/Pointers/linkNodes3CON.css
.. odsalink:: AV/Pointers/linkNodes2CON.css
.. odsalink:: AV/Pointers/linkNodes1CON.css
.. This file is part of the OpenDSA eTextbook project. See
.. http://algoviz.org/OpenDSA for more details.
.. Copyright (c) 2012-2016 by the OpenDSA Project Contributors, and
.. distributed under an MIT open source license.
.. avmetadata::
:author: Cliff Shaffer, Mohammed Mostafa, and Margaret Ellis
:requires: Pointer intro
:satisfies: Link Nodes
:topic: Link Nodes
Link Nodes
==========
Link Nodes
----------
In this module, we introduce the idea of a :term:`link node`.
A link node has some sort of value field, and a pointer to another link
node.
Later, you will learn about :term:`linked lists `,
which are made from link nodes.
For now, we will just use them as a simple way to connect some objects
together.
Here is a class definition for a basic ``link`` object.
Note that, while the ``next`` field references another ``link``
object, the ``data`` field is of type ``Object``.
This denotes that we don't really care what sort of thing one stores
in a ``link`` node's data field.
.. codeinclude:: Pointers/Link
:tag: Link
Now we'll see some examples for how to use ``link`` nodes to put
chains together.
.. inlineav:: linkNodes1CON ss
:points: 0.0
:required: False
:threshold: 1.0
:id: 106838
:output: show
How do we set up the chain to begin with?
.. inlineav:: linkNodes2CON ss
:points: 0.0
:required: False
:threshold: 1.0
:id: 106839
:output: show
createList
----------
.. extrtoolembed:: 'createList'
:module: Links
:long_name: createList
:launch_url: https://opendsa-server.cs.vt.edu/lti/launch_extrtool/106840
:id: 65485
:learning_tool: code-workout-jhavepop
One can easily write a loop to iterate through all the Links on a
chain, without needing to know how many there actually are.
.. inlineav:: linkNodes3CON ss
:output: show
iterateOverList
---------------
.. extrtoolembed:: 'iterateOverList'
:module: Links
:long_name: iterateOverList
:launch_url: https://opendsa-server.cs.vt.edu/lti/launch_extrtool/106841
:id: 65486
:learning_tool: code-workout-jhavepop
One can remove a Link from a chain.
.. inlineav:: linkNodes4CON ss
:output: show
changePointeeNext1
------------------
.. extrtoolembed:: 'changePointeeNext1'
:module: Links
:long_name: changePointeeNext1
:launch_url: https://opendsa-server.cs.vt.edu/lti/launch_extrtool/106842
:id: 65487
:learning_tool: code-workout-jhavepop
Finally, we can also insert new Links.
.. inlineav:: linkNodes5CON ss
:output: show
addNodeAtHead
-------------
.. extrtoolembed:: 'addNodeAtHead'
:module: Links
:long_name: addNodeAtHead
:launch_url: https://opendsa-server.cs.vt.edu/lti/launch_extrtool/106843
:id: 65488
:learning_tool: code-workout-jhavepop
Here is an exercise to practice manipulating link nodes.
.. avembed:: Exercises/Pointers/PointerEX3PRO.html ka
:module: Links
.. odsascript:: AV/Pointers/linkNodes1CON.js
.. odsascript:: AV/Pointers/linkNodes2CON.js
.. odsascript:: AV/Pointers/linkNodes3CON.js
.. odsascript:: AV/Pointers/linkNodes4CON.js
.. odsascript:: AV/Pointers/linkNodes5CON.js