C++ Pointers, Linked List Confusion -


I'm trying to create a linked list in C ++. I understand that the code I created is a You should create a node and then progressively link 4 more at the end. Unfortunately, when I see "12 123 1234 12345" as "12 123 1234 12345", I'm seeing "12 12 12 12" and in my main I am unable to cross the list - this bus Crashes.

I have the following code:

  struct listNode {int val; ListNode * Next; }; Int nodeCount = 0; ListNode * addToEnd (listNode * node) {listNode * newNode = new listNode; NewNode-> Val = ++ nodecount; NewNode-> Next = null; If (node ​​== faucet) {newNode return; } ListNode * Current = Node; The court's & lt; & Lt; "\ N \ n"; {If (current-> next == NULL) {current-> next = newNode; } Cout & lt; & Lt; Current-> Val & lt; & Lt; "\ N"; Current = current-> next; } While (current-> next! = Null); The court's & lt; & Lt; Current- & gt; The & lt; & Lt; Endl; } Int main () {listNode * first = addToEnd (NULL); AddToEnd (first); AddToEnd (first); AddToEnd (first); AddToEnd (first); Cout & lt; & Lt; "Third:" & lt; & Lt; First-> Next-> Next-> Val; }  

Any help is appreciated, because I am at the end of intelligence!

It is clear that the function addToEnd is incorrect

  listNode * addToEnd (listNode * node) {listNode * newNode = new listNode; NewNode-> Val = ++ nodecount; NewNode-> Next = null; If (node ​​== faucet) {newNode return; } ListNode * Current = Node; The court's & lt; & Lt; "\ N \ n"; {If (current-> next == NULL) {current-> next = newNode; } Cout & lt; & Lt; Current-> Val & lt; & Lt; "\ N"; Current = current-> next; } While (current-> next! = Null); The court's & lt; & Lt; Current- & gt; The & lt; & Lt; Endl; }  

Suppose that the list already has two nodes and within the function, consider the do-all loop. First current_next ! = Null is executed on the following statement

  current = current-> gt; next;  

Now the current digit for the second node is equal to its data member next NULL, then the state of the loop

 } while ( Current-> next! = Null);  

will be wrong and will not be repeated.

  listNode *  

AddToEnd (listNode * node) next! = Null) current = current->; Current-> Next = newnode; Return the new node; // or // return node; }

Keep this statement in mind that

  cout & lt; & Lt; "Third:" & lt; & Lt; First-> Next-> & gt; Val  

Only shows the value of the third node, if you want to output all the list, you should write

  (listNode * current = first; current; Current = current-> gt; next) {std :: cout & lt; & Lt; Current-gt; Val & lt; & Lt; ''; } Std :: cout & lt; & Lt; Std :: endl;  

By using my work, you can primarily type in the following ways:

  listNode * first; Adwords (Adtoound (Adtoind (first = Adendend))));  

Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -