<html>
<head>
<title>firstworks   Programming with Rudiments using the singlylinkedlist class</title>
<link href="../css/styles.css" rel="stylesheet">
</head>
<body>

<span class="heading">Using the singlylinkedlist class</span><br><br>

<ul>
<li><a href="#intro">Introduction</a></li>
<li><a href="#values">Manipulating Values</a></li>
<li><a href="#nodes">Manipulating Nodes</a></li>
<li><a href="#sort">Sorting</a></li>
<li><a href="#iterate">Iterating Manually</a></li>
<li><a href="#types">Data Types</a></li>
</ul>

<br>
<a name="intro"></a>
<h3>Introduction</h3>

<p>The <a href="../classes/html/classsinglylinkedlist.html">singlylinkedlist class</a> allows you to store an arbitrary number of values in a singly-linked list.  Since the singlylinkedlist class is template-based, you can store arbitrary types of values.</p>

<p>Each singlylinkedlist is composed of a series of singlylinkedlistnodes.  Each singlylinkedlistnode contains a value.</p>

<p>This class is similar to the <a href="../classes/html/classlinkedlist.html">linkedlist class</a> but uses less memory and many of its operations are faster.</p>

<p>However, the move, detach and remove operations are much slower.  If your application must run these operations regularly, you should consider using the linkedlist class instead.</p>

<br>
<a name="values"></a>
<h3>Manipulating Values</h3>

<p>The singlylinkedlist class provides methods for prepending, appending, inserting and removing values from a linked list, for finding values in the list, for getting the size of the list, clearing the list, and printing the list.</p>

<p>Note that unlike the linkedlist class, there is no method for inserting a value before another value, only a method for inserting a value after another value.</p>

<blockquote class="code">
@singlylinkedlist-values.cpp.html@
</blockquote>

<br>
<a name="nodes"></a>
<h3>Manipulating Nodes</h3>

<p>The singlylinkedlist class also provides methods for manipulating list nodes directly.</p>

<p>Note that unlike the linkedlist class, there are no methods for inserting or moving a node before another node, only methods for inserting and moving a node after another node.</p>

<blockquote class="code">
@singlylinkedlist-nodes.cpp.html@
</blockquote>

<br>
<a name="sort"></a>
<h3>Sorting</h3>

<p>The singlylinkedlist class also provides methods for sorting the list.</p>

<p>Methods for both insertion and heap sort are provided.  Insertion sort is slow, for large lists, but doesn't require any additional memory.  Heap sort is fast, even for large lists, but requires additional memory.</p>

<blockquote class="code">
@singlylinkedlist-sort.cpp.html@
</blockquote>

<br>
<a name="iterate"></a>
<h3>Iterating Manually</h3>

<p>The singlylinkedlist class also provides methods for manually iterating through the list.</p>

<p>Note, that unlike the linkedlistnode class, the singlylinkedlistnode class does not have a getPrevious() method.  Thus, it is impossible to iterate backwards through a singlylinkedlist.</p>

<blockquote class="code">
@singlylinkedlist-iterate.cpp.html@
</blockquote>

<br>
<a name="types"></a>
<h3>Data Types</h3>

<p>Since the singlylinkedlist class is template-based, it can store any type of data.</p>

<p>Note that the print() method works for primitive types and strings, but for more complex types, it only prints the address of the object.</p>

<p>Note also that the singlylinkedlist class does not manage the data stored in it.  If you store a list of dynamically allocated strings or objects, they will not be deleted automatically when a node is removed or the list is cleared.  They must be deleted manually.</p>

<blockquote class="code">
@singlylinkedlist-types.cpp.html@
</blockquote>

</body>
</html>
