Note that there are some explanatory texts on larger screens.

plurals
  1. POLinked list issue with insert
    primarykey
    data
    text
    <p>The problem appears with the insert function that I wrote.</p> <p>3 conditions must work, I tested b/w 1 and 2, b/w 2 and 3 and as last element, they worked.</p> <hr> <p><strong>EDIT;</strong> It was my own problem. I did not realize I put MAXINPUT = 3 (instead of 4). I do appreciate all the efforts to help me becoming a better programmer, using more advance and more concise features of C++.</p> <p>Basically, the problem has been solved.</p> <hr> <p>Efficiency is not my concern here (not yet). Please guide me through this debug process.</p> <p>Thank you very much.</p> <pre><code>#include&lt;iostream&gt; #include&lt;string&gt; using namespace std; struct List // we create a structure called List { string name; string tele; List *nextAddr; }; void populate(List *); void display(List *); void insert(List *); int main() { const int MAXINPUT = 3; char ans; List * data, * current, * point; // create two pointers data = new List; current = data; for (int i = 0; i &lt; (MAXINPUT - 1); i++) { populate(current); current-&gt;nextAddr = new List; current = current-&gt;nextAddr; } // last record we want to do it sepeartely populate(current); current-&gt;nextAddr = NULL; cout &lt;&lt; "The current list consists of the following data records: " &lt;&lt; endl; display(data); // now ask whether user wants to insert new record or not cout &lt;&lt; "Do you want to add a new record (Y/N)?"; cin &gt;&gt; ans; if (ans == 'Y' || ans == 'y') { /* To insert b/w first and second, use point as parameter between second and third uses point-&gt;nextAddr between third and fourth uses point-&gt;nextAddr-&gt;nextAddr and insert as last element, uses current instead */ point = data; insert(()); display(data); } return 0; } void populate(List *data) { cout &lt;&lt; "Enter a name: "; cin &gt;&gt; data-&gt;name; cout &lt;&lt; "Enter a phone number: "; cin &gt;&gt; data-&gt;tele; return; } void display(List *content) { while (content != NULL) { cout &lt;&lt; content-&gt;name &lt;&lt; " " &lt;&lt; content-&gt;tele; content = content-&gt;nextAddr; cout &lt;&lt; endl; // we skip to next line } return; } void insert(List *last) { List * temp = last-&gt;nextAddr; //save the next address to temp last-&gt;nextAddr = new List; // now modify the address pointed to new allocation last = last-&gt;nextAddr; populate(last); last-&gt;nextAddr = temp; // now link all three together, eg 1-NEW-2 return; } </code></pre>
    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.
 

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