value of strct object being overwritten

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #31
    I am beginning to sound like a stuck record, why are you not taking in what I am saying, you are still stopping the while loop 1 iteration too late, you have not changed your loop end condition.

    You have removed first as a parameter, which is good because then you can correctly set the head of the list if there isn't one yet.

    Unfortunately you operate directly on first, the first time you execute the code

    first - first->next;

    you destroy your current list and cause a memory leak (of the first item in the list). In fact your current while loop effective discards all of the current list and replaces with a single new item.

    Having removed first as an input parameter you need to heed the advice Laharl gave and copy first to a local variable before operating on it.

    I can see in the comments that you have tried using a local variable the and you definitely need to instate (most of) this. But that alone will not make you code work until you fix the logical errors in your while loop end condition.

    Comment

    • Parul Bagadia
      New Member
      • Mar 2008
      • 188

      #32
      Thank you sir for your guidence and sorry that you had to repeat the same thing again and again....
      But as you said if i change my while loop condition;
      while(first->next!=NULL)
      {
      the=the->next;
      }
      where the is equal to first; so when first is null; the also turns out to be null; and in case of while loop it checks the condition that the->next==NULL or not; but when the itself is NULL; there wont be any case of existance of the->next; and my compiler gives fatal error.
      So can you please tell me the solution now?
      What should be the condition of while loop?
      I have got many more assignments to cope up. And this assignment also has many more parts; n m still stuck on first one.
      Thank you.

      Comment

      • Parul Bagadia
        New Member
        • Mar 2008
        • 188

        #33
        Any ways to get out of it?

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #34
          [code=c]
          if(first==NULL)
          {
          first=add;
          printf("\n%d",& first);
          first->next=NULL;
          }
          else
          {
          // the=first;
          printf("\n%d",& first);
          while(first!=NU LL)
          {
          first=first->next;
          // the=the->next;
          printf("\n%d",& first);
          }
          first=add;
          first->next=NULL;
          }
          [/code]
          That is a chunk of your original code, the if statement on line 1 is correct as is the contents of the if-block (lines 2-6).

          In the else block the initial thing you need to do is not use first but use a copy. first should only appear in the else block at line 9 where you copy it to the if it appears anywhere else it is an error.

          The while statement needs to be the->next != NULL. This is guaranteed to be OK because first is not NULL (because of the if statement) and the is a copy of first.

          With that while condition when the loop finishes the will point to the last element in the list. You then need to assign add as the next element in the list, the new last element following the current last element. You assign it to the->next and clear (set to NULL) add->next.

          Comment

          • Parul Bagadia
            New Member
            • Mar 2008
            • 188

            #35
            I know you are fade of explaining me the same thing so many times; but m sorry i still have doubts.
            In the else block the initial thing you need to do is not use first but use a copy. first should only appear in the else block at line 9 where you copy it to the if it appears anywhere else it is an error.
            Why is it so?
            Yes the code is working now; thanx for that but its of no use coz i havnt understood the above logic.

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #36
              Hey I'm happier now we are making progress :-)

              In the else block you are look for the last node in the list so you can add another after it.

              first is your global variable pointing to the first item in the list. If you alter first in the else block, by using it as the while loop control variable you will irrevocably loose your pointer to the first item in the list.

              So because of this you need to use a local variable to step down the list looking for the last node and that local variables start point is first. Once you have used first to initialise this local variable it has served it's purpose in the else block, you have no further interest in the first item in the list your only interest is in finding the last item.

              Comment

              • Parul Bagadia
                New Member
                • Mar 2008
                • 188

                #37
                If i pass display function without first in its arfument; it doesnt work; it prints only the recent value...
                why is it so?

                Comment

                • Parul Bagadia
                  New Member
                  • Mar 2008
                  • 188

                  #38
                  In case of insertion ; we are not updating the value of first anywhere; i mean we hav juat equated the to first; i.e. the=first;
                  So value of the gets updated; but first is not assign next node value anywhere;
                  so how come display prints the correct value ; when passed by first.

                  Comment

                  • Laharl
                    Recognized Expert Contributor
                    • Sep 2007
                    • 849

                    #39
                    Because first always points to the first node in the list. Thus, when you pass it to display, it can start from the first node in the list. As a visual,

                    |_|->|_|->|_|
                    ^
                    |
                    first

                    Thus, when you pass first, the is set to first, and slides down the list to print all the nodes. First stays where it is, since an assignment is a copy, not a permanent link between the two. If you were to delete the after the while loop is done, first would still point to the first node (this is exactly what happens when the function exits and local scope variables are no longer usable).

                    If you insert a new node, because first still points to the head, all the nodes will still be printed. If you passed first->next to the function, only the last two nodes would be printed.

                    Comment

                    • Parul Bagadia
                      New Member
                      • Mar 2008
                      • 188

                      #40
                      Thanx for that.
                      But my question here is ; even if i dont pass first it should start from first coz first is global variabe;
                      and my second question is ; in my corrected code; i have not updated the value of first; insted just used the ;copy of it; so how it gets updated when i call display?

                      Comment

                      • Parul Bagadia
                        New Member
                        • Mar 2008
                        • 188

                        #41
                        Does somebody know about it?

                        Comment

                        • Banfa
                          Recognized Expert Expert
                          • Feb 2006
                          • 9067

                          #42
                          Originally posted by Parul Bagadia
                          Thanx for that.
                          But my question here is ; even if i dont pass first it should start from first coz first is global variabe;
                          and my second question is ; in my corrected code; i have not updated the value of first; insted just used the ;copy of it; so how it gets updated when i call display?
                          In answer to your first question your display function should also use a copy of first otherwise it will alter first which you don't want to do. It is better practice to pass the global first as a parameter to Display and let it operator on the parameter copy of first but you could just access the global variable first to initialise a parameter in Display.

                          In answer to your second question, your code and your post #38 appear to be saying different things about how you call Display and I suspect this is causing confusion.

                          Comment

                          • Parul Bagadia
                            New Member
                            • Mar 2008
                            • 188

                            #43
                            Ok here is my final code; the one which is working coz of your help;
                            [code=c]
                            //General insertion
                            void insertion()
                            {
                            struct linklist*the=NU LL;
                            struct linklist* add=NULL;
                            add= (struct linklist*)mallo c(sizeof(struct linklist));
                            the= (struct linklist*)mallo c(sizeof(struct linklist));
                            printf("\nEnter the no. you wish to add in link list\n");
                            scanf("\n %d",&add->value);
                            if(first==NULL)
                            {
                            first=add;
                            first->next=NULL;
                            }
                            else
                            {
                            the=first;
                            while(the->next!=NULL)
                            {
                            the=the->next;
                            }
                            the->next=add;
                            add->next=NULL;
                            }
                            }
                            //Display of list
                            void display(struct linklist*first )
                            {
                            printf("\nFollo wing are the elements you have added in the list till now\n");
                            while(first!=NU LL)
                            {
                            printf("\n%d\n" ,first->value);
                            first=first->next;
                            }
                            }[/code]
                            now; this works fine; but here my doubt is in insertion...i have not updated he value of first anywhere; i mean not exactly the value; but anything!
                            So, how come it displays the stuff correctly?
                            coz condition of display is it prints the value till first!=NULL;
                            but we hav just updated the and not first! so how it works?
                            Last edited by Banfa; Mar 14 '08, 09:41 AM. Reason: Added [Code=c]...[/code] tags

                            Comment

                            • Banfa
                              Recognized Expert Expert
                              • Feb 2006
                              • 9067

                              #44
                              Originally posted by Parul Bagadia
                              now; this works fine; but here my doubt is in insertion...i have not updated he value of first anywhere; i mean not exactly the value; but anything!
                              You did, you update the value of the->next when the was pointing to the last item in the list to which first points to the head.

                              Remember first is your head pointer, the pointer to the first item in the list. The only times it will get changed is the first time you add an item to the list (when it must be changed) or if you change the item at the head of the list.

                              Originally posted by Parul Bagadia
                              So, how come it displays the stuff correctly?
                              coz condition of display is it prints the value till first!=NULL;
                              but we hav just updated the and not first! so how it works?
                              Trying to think of a linked list as the variables you use to access it is definitely the wrong way to go. You only have 3 pointers in your code, the global variable first, the local variable the in insertion and the local parameter first in display.

                              Try to thing of the list as those 3 pointers will prevent you appreciating its true nature. The list is a number of allocated structures in which each structure points to the next one, apart from the last one that points at NULL. You have various pointers at various points in your code that point to specific structures in the list and give you a view of a little bit of it. Importantly you have a pointer that points to the first item in the list, your global variable first, which allows you to locate the start of the list and is therefore of high importance to the operation of the list as all operations start from here. However even this pointer just points to a single structure in the list.

                              I think you need to think about the structure of the data in a linked list, probably using a pen and paper to draw one out by hand and carry out some of programmed operations by hand on paper to get the idea.

                              Comment

                              Working...