Have a problem in the insertion in the linklist?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AnagJohari
    New Member
    • May 2010
    • 96

    #1

    Have a problem in the insertion in the linklist?

    Actualy i have a prob in the begaining to add the node in the linklist
    i write the following code
    i make a display fucnction which display all the value of linklist
    i check the value in display function by using printf
    by using following statement
    Code:
    printf("test=%d",current->next->no)
    i get the expected ans from this statement
    after that
    i use
    Code:
    while(current->next!=null)
    this loop is not working the control is not gone under this statement
    if currnet->next->no is shown a value that means
    current->next use a pointer which points to the value
    thatmeans its not null.
    why
    Code:
    current->next =null
    i shown
    help me guys
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I would base logic on current rather than current->next.

    Suppose your linked list is 1 followed by 2.

    When I am on the node with 2, then current->next is null so I don't process the 2. That is, I miss the least element of the list.

    After I process the 2, then current becomes
    current->next. That is, current becomes null.

    Code:
    while (current)
    {
       /* do stuff */
       current = current->next;
    }
    Now when you leave the loop, the last node was processed.

    Comment

    Working...