Wednesday 15 January 2014

Linked Lists C - after adding nodes print only the last value -


After reading here about linked lists, I started messing with it, I define the structure, it It is inserted, but some have to face, maybe I did something wrong but I think your advice or small knowledge is needed. I

This is my code:

  Structure node {int val; Structure node * next; }; Typedf Structure node item; Item print kur (item * curu) {printf ("curr -> val% d \ n", curr- & gt; val); } Item Adnode (item * curr, item * head, int num) {curr- & gt; Val = num; Curr- & gt; Next = head; Head = curr; } Zero main () {item * curr, * head; Int I, ITEMS, num; Head = null; Printf ("how many things? \"); Scanf ("% d", & amp; objects); Printf ("Please include your numbers \ n"); For (i = 1; i & lt; = ITEMS; i ++) {curr = (item *) malloc (sizeof (item)); Scanf ("% d", and number); Addnode (curr, head, number); } While curr {printcurr (curr); Curr = curr- & gt; next; }}   

For example, I get the number of ITEM 3, then I put 1,2,3 and the output will be 3. I'm missing something here; How do I print all the lists and not only the final numbers?

The problem is in addnote (). The value of the head inside the main () never changes because only the local version (addnode) inside the exchange is revised. (As a side note: addnote () should be returned by zero.)

Instead of

  zero addnode (item * curr, item ** head, Int num) {curr - & gt; Val = num; Curr- & gt; Next = * head; * Head = currew; } ... addnode (curr, and head, num);   

Alternate versions:

  zero addnode (item * curr, item * head, int num) {curr- & gt; Val = num; Curr- & gt; Next = * head; } ... addnode (curr, head, num); Head = curue;    

No comments:

Post a Comment