Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I take it that you know that C++ already has a linked list class, and you want to implement your own because you want to learn how to do it. </p> <p>First, read <a href="https://stackoverflow.com/questions/392397/arrays-whats-the-point">Why do we use arrays instead of other data structures?</a> , which contains a good answer of basic data-structures. Then think about how to model them in C++:</p> <pre><code>struct Node { int data; Node * next; }; </code></pre> <p>Basically that's all you need to implement a list! (a <em>very</em> simple one). Yet it has no abstractions, you have to link the items per hand:</p> <pre><code>Node a={1}, b={20, &amp;a}, c={35, &amp;b} d={42, &amp;c}; </code></pre> <p>Now, you have have a linked list of nodes, all allocated on the stack:</p> <pre><code>d -&gt; c -&gt; b -&gt; a 42 35 20 1 </code></pre> <p>Next step is to write a wrapper class <code>List</code> that points to the start node, and allows to add nodes as needed, keeping track of the head of the list (the following is very simplified):</p> <pre><code>class List { struct Node { int data; Node * next; }; Node * head; public: List() { head = NULL; } ~List() { while(head != NULL) { Node * n = head-&gt;next; delete head; head = n; } } void add(int value) { Node * n = new Node; n-&gt;data = value; n-&gt;next = head; head = n; } // ... }; </code></pre> <p>Next step is to make the List a template, so that you can stuff other values (not only integers). </p> <p>If you are familiar with smart pointers, you can then replace the raw pointers used with smart pointers. Often i find people recommend smart pointers to starters. But in my opinion you should first understand why you need smart pointers, and then use them. But that requires that you need first understand raw pointers. Otherwise, you use some magic tool, without knowing why you need it. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload