C Linked list only contains first element...not sure what happens to rest -


I posted a question about a linked list in a CD some days ago. I thought that everything is fine, then Professor email is telling us that instead of this signature:

  int insert_intlist (INTLIST * lst, int n); / * Inserts int (n) in an intlist from the beginning * /  

By mistake it means:

  int insert_intlist (INTLIST ** Lst, int n); / * Inserts int (n) in an intlist from the beginning * /  

I thought now that I am quiet that I have an indicator for the indicator, I can move the pointer out and when I return to the main, I still have my complete list of links.

He starts with giving us this:

  INTLIST * init_intlist (int n) {INTLIST * lst; // Store = penter node lst = (INTLIST *) malloc (sizeof (INTLIST)); Make enough memory for // node - lst- gt; Datum = n; // Set lst-> Next = NULL; // Set the Pointer Return List; // Return new list}  

Which is primarily to start the list:

  if (lst == NULL) lst = Init_intlist (i); Else insert_intlist (LST, I);  

The list type is of INTLIST *, so it is defined as INTLIST * lst. So I read in a few numbers from a text file like in 193. It is considered to be preparing a linked list from this ... so the first number will go to init_listlist (1); And if it was defined above, it takes the next number 3 in this case and calls the insert_intlist (lst, 3). Well this is my listing list and what I want to do is included at the beginning of the list:

  int insert_listlist (INTLIST ** lst, int n) {INTLIST * lstTemp; // lstTemp = (INTLIST *) malloc (sizeof (INTLIST)) to store a temporary node to be added to the indicator list; Create enough memory for // node lstTemp-> Datum = n; // Assign value // If there is anything in the list, then // should be there, but only if (* lst == NULL) {* lst = lstTemp; LstTemp-> Next = zero; } And {lstTemp- & gt; Next = * lst; // Attach the front node * lst = lstTemp; // coming to the new node list becomes main} 0 is returning; }  

So if the list starts at 1, then this function will only create a new node and then this will make the floating node-> the head of the list the next point (which I thought) And then reassign the list head to this new temporary node.

It looks like it's working fine, but when I try to print my list on the screen, it prints number 1.

Does anyone have any clue that I am doing wrong?

You are going to point to an indicator that you want to change the pointer The pointer is not at the pointer itself. Does this make sense?

  If (lst == NULL)  

Here you are checking to see if you have been given a zero indicator. Good practice to check the error, but not what you were doing there for it. If lst is zero, then you do not have any indicators, you can not do anything, and you should return a nonzero error code without doing anything.

Once you make sure that your pointer is not faucet, then you point to the pointer ( * lst ). Point-to-pointers are the indicator for the first list item. If that indicator is faucet, then you can change it to the new item's pointer. Basically, where you use lst , you should use * lst or (* lst) . (Reminder: * operator followed by goes to -> gt operator! An indicator indicating that indicator

PS

This type of pointer is serious to learn with a good programmer, especially C.

P.P.S. The second thing that got you wrong is that instead of

  insert_intlist (lst, i);  

You should call it

  insert_intlist (& amp; lst, i);  

... and, for Brownie Point, check the return code for errors.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -