c++ - Linked list problem -


There is a problem with the linked list I have written. I do not know whether this is either a problem with my insert function, or If this is my traversal function that is not correct. I hope for some input on one hand, I'm making a list in the main lifetime because I do not know that my initNode function is correct.

  #include & lt; Iostream & gt; using namespace std; Typedef configuration node {Int data; Node * next; }; Zero initNode (node ​​* head) {head = new node; Head-> gt; Next = null; } Insert zero (node ​​* head, int x) {node * temp; Temp = new node; Temp- & gt; Data = x; Temp- & gt; Next = head; Head = temporary; } Zero cross (node ​​* head) {node * temp; Temp = Head; If (head == faucet) {cout & lt; & Lt; "End of the list." & Lt; & Lt; Endl; } And {while (temp! = NULL) {cout & lt; & Lt; Temp- & gt; Data & lt; & Lt; ""; Temp = temp- & gt; next; }}} Int main () {node * head; Head = null; InsertNode (head, 5); InsertNode (head, 5); Cross (head); Return 0; }  

your head back main to insertNode . Note that even if head is an indicator, the pointer itself is a value and no change in the pointer value is visible in main . head : node * insertNode (node ​​* head, int x) {... return head; }

and update it to main :

  head = insertNode (head, 5);  

The second common way of doing this is to pass the pointer to the pointer and update it directly:

  Enter the zero node (node ​​** head, int X) {node * temp; Temp = new node; Temp- & gt; Data = x; Temp- & gt; Next = * Head; * Head = floating; }  

and call it like this:

  insertNode (and head, 5);  

Comments