value of strct object being overwritten

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Parul Bagadia
    New Member
    • Mar 2008
    • 188

    value of strct object being overwritten

    I have written a code for linklist; the different operations on it; but at the time of insertion; it doesn't create a new strct object; it's overwritting the value.....
    Just see if somebody can point out, what's wrong with it.
    As, the complete code is quite big; m writting only the insertion part.
    Here; first is my root node.
    And i have made a functon, which accepts the value of first.

    [CODE=c]//General insertion.
    #include<stdio. h>
    #include<stdlib .h>
    struct linklist
    {
    int value;
    struct linklist*next;
    }* first;
    void insertion(struc t linklist*first)
    {
    struct linklist* add=NULL;
    add= (struct linklist*)mallo c(sizeof(struct linklist));
    printf("\nEnter the no. you wish to add in link list\n");
    scanf("\n %d",&add->value);
    /*while(first!=N ULL)
    {
    first=first->next;
    }
    first=add;
    add->next=NULL;*/
    first->next=add;
    add->next=NULL;
    add=add->next;
    }
    //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]
    I know here it works only for two values........
    But then if i write a code for more than one value; it doesnt diplay even a single value.
    The commented code is what i thought should work for inserting different value; but if i go for it; display function doesn't even display 2 values......
    So, for the time being i have commented it out.
    I had also tried with if statements; but invain.
    What i need here is to update the value of first->next; so that there will be link in the objects; and every time user calls insertion function; a new add object should get created.
    And here its overwrittin .......
    Tell me what to do.
    Last edited by Ganon11; Mar 4 '08, 04:03 AM. Reason: Please use the [CODE] tags provided.
  • fual
    New Member
    • Feb 2008
    • 28

    #2
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    struct linklist
    {
      int value;
      struct linklist*next;
    }* first;
    
    void insertion(struct linklist*first) {
      struct linklist* add=NULL;
      add= (struct linklist*)malloc(sizeof(struct linklist));
      printf("\nEnter the no. you wish to add in link list\n");
      scanf("\n %d",&add->value);
      /*while(first!=NULL)
      {
      first=first->next;
      }
      first=add;
      add->next=NULL;*/
      first->next=add;
      add->next=NULL;
      add=add->next;
    }
    
    //Display of list
    void display(struct linklist* first) {
      printf("\nFollowing are the elements you have added in the list till now\n");
      while(first!=NULL) {
        printf("\n%d\n",first->value);
        first=first->next;
      }
    }
    That's the first step. Rule number #1 format your code :)

    Comment

    • Parul Bagadia
      New Member
      • Mar 2008
      • 188

      #3
      Originally posted by fual
      Code:
      #include<stdio.h>
      #include<stdlib.h>
      struct linklist
      {
        int value;
        struct linklist*next;
      }* first;
      
      void insertion(struct linklist*first) {
        struct linklist* add=NULL;
        add= (struct linklist*)malloc(sizeof(struct linklist));
        printf("\nEnter the no. you wish to add in link list\n");
        scanf("\n %d",&add->value);
        /*while(first!=NULL)
        {
        first=first->next;
        }
        first=add;
        add->next=NULL;*/
        first->next=add;
        add->next=NULL;
        add=add->next;
      }
      
      //Display of list
      void display(struct linklist* first) {
        printf("\nFollowing are the elements you have added in the list till now\n");
        while(first!=NULL) {
          printf("\n%d\n",first->value);
          first=first->next;
        }
      }
      That's the first step. Rule number #1 format your code :)
      That's a sign used for including directive

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        No, he's using # as the symbol for 'number'. He's taking a roundabout way of saying 'use the provided CODE tags when posting code here so we can read it easily'.

        Comment

        • Parul Bagadia
          New Member
          • Mar 2008
          • 188

          #5
          I'm not getting you....

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            The easiest place to add a new value to a linked list is at the head

            Code:
            // Pseudo Code
            void AddItem(void)
            {
                Get New Item -> NewItem
            
                NewItem->next = head;
                head = NewItem;
            }

            Comment

            • Parul Bagadia
              New Member
              • Mar 2008
              • 188

              #7
              Originally posted by Banfa
              The easiest place to add a new value to a linked list is at the head

              Code:
              // Pseudo Code
              void AddItem(void)
              {
                  Get New Item -> NewItem
              
                  NewItem->next = head;
                  head = NewItem;
              }
              No but i want it to get added after first and i want the sequence as the same as i go on adding.

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Originally posted by Parul Bagadia
                No but i want it to get added after first and i want the sequence as the same as i go on adding.
                Do you mean after the first or at the end?

                Comment

                • Parul Bagadia
                  New Member
                  • Mar 2008
                  • 188

                  #9
                  Originally posted by Banfa
                  Do you mean after the first or at the end?
                  after the first; and every new node should get added after the most recent one.

                  Comment

                  • Parul Bagadia
                    New Member
                    • Mar 2008
                    • 188

                    #10
                    I just want to know; what's wrong in my code. because according to me; my logic is correct.... or may be you can say i'm unable to point out the mistake.

                    Comment

                    • Parul Bagadia
                      New Member
                      • Mar 2008
                      • 188

                      #11
                      Does somebody gonna tell me wats wrong with this......... this is really my final post.

                      Comment

                      • Laharl
                        Recognized Expert Contributor
                        • Sep 2007
                        • 849

                        #12
                        If you were doing this yourself (no computer), how would you go through the list to get to the end to add a new one on? Translate that to code. Then compare it to what your current code does and you'll see why you're wrong.

                        Comment

                        • Parul Bagadia
                          New Member
                          • Mar 2008
                          • 188

                          #13
                          Originally posted by Laharl
                          If you were doing this yourself (no computer), how would you go through the list to get to the end to add a new one on? Translate that to code. Then compare it to what your current code does and you'll see why you're wrong.
                          I couldn't reach till the end of my list; coz i didnt come out of the insertion thing.
                          but according to me the code to reach till the end of the list would be;
                          while(first!=NU LL)
                          {
                          first=first->next;
                          }
                          first=add;
                          add->next=NULL;
                          where first is my root node and add is the node which ill be adding recently.

                          Comment

                          • Banfa
                            Recognized Expert Expert
                            • Feb 2006
                            • 9067

                            #14
                            Sorry not sure what happened there but you fell off my radar somehow.

                            On your assertion that your logic is right but you have a mistake in your code then in this case
                            [CODE=c]
                            /*while(first!=N ULL)
                            {
                            first=first->next;
                            }
                            first=add;
                            add->next=NULL;*/
                            [/CODE]
                            It doesn't work because first is a local variable and setting it's value can not effect anything outside the function, like your list for instance.

                            [CODE=c]
                            first->next=add;
                            add->next=NULL;
                            add=add->next;
                            [/CODE]
                            And this doesn't work because you do not attempt to traverse the the list to find the insertion point you just overwrite the second item in the list with the item to add.

                            Of the 2 the 1st chunk of code is closer to what you actually need but you need to stop the loop 1 iteration earlier than you are.

                            Comment

                            • Parul Bagadia
                              New Member
                              • Mar 2008
                              • 188

                              #15
                              first is my global variable and not local;
                              and second thing;
                              first->next=add;
                              add->next=NULL;
                              add=add->next;
                              i know this doesnt traverse the list;i did that coz my code will at least display 2 valus; instead of one;
                              but i didnt understand whats wrong with my while loop.
                              I know its overwritting the values;
                              but there lies my whole problem, why is it overwritting?
                              in case if i comment out
                              ; first->next=add;
                              add->next=NULL;
                              add=add->next;
                              and just stick to while loop it doesnt work...

                              Comment

                              Working...