Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ linked list help (pointers)?
    primarykey
    data
    text
    <pre><code>template &lt;typename T&gt; class LinkedNode { public: T data; LinkedNode&lt;T&gt; *next; LinkedNode&lt;T&gt; *prev; LinkedNode&lt;T&gt;(); LinkedNode&lt;T&gt;(T); }; // Default constructor template &lt;typename T&gt; LinkedNode&lt;T&gt;::LinkedNode() { next = NULL; prev = NULL; } template &lt;typename T&gt; class LinkedList { private: LinkedNode&lt;T&gt; *head; public: LinkedList&lt;T&gt;(); ~LinkedList&lt;T&gt;(); void addFront(T); void addBack(T); void addAt(T, int); void removeFront(); void removeBack(); void removeAt(int); void printList(); }; // Constructor template &lt;typename T&gt; LinkedList&lt;T&gt;::LinkedList() { head = NULL; } // Add new node to front template &lt;typename T&gt; void LinkedList&lt;T&gt;::addFront(T d) { LinkedNode&lt;T&gt; temp; temp.data = d; if (head == NULL) { head = &amp;temp; } else { temp.next = head; head-&gt;prev = &amp;temp; head = &amp;temp; } } // Add new node to back template &lt;typename T&gt; void LinkedList&lt;T&gt;::addBack(T d) { // Find the back of this list LinkedNode&lt;T&gt; *ptr; ptr = head; while (ptr-&gt;next != NULL) // &lt;------- DIES HERE, MEMORY ACCESS VIOLATION { ptr = ptr-&gt;next; } // Make a new node and join it to the back LinkedNode&lt;T&gt; temp; temp.data = d; temp.prev = ptr; ptr-&gt;next = &amp;temp; } </code></pre> <p>Here is a snippet from my linked list system. The issue is that it throws an error on the indicated line. The debugger says that the "head" pointer is pointing to a legitimate memory address of a LinkedNode with no "next" or "prev", but the "ptr" pointer points to address 0xcccccc, not the address of head? I'm really confused, I thought I understood pointers!</p>
    singulars
    1. This table or related slice is empty.
    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. 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