Thread: Dont understand something in linked list

  1. #16
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    No, that part of the code appears to be correct. What is happening is that prev is stepping through the linked list as it is being built. prev = current fixes that.

  2. #17
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    i know thats logical but whats puzzling me is we now allocating memory right in this loop iteration so when we allocate by malloc that memory gets passed to both prev and current i tried to remove prev it didnt work coz prev points to previous member i think but what i dont understand is how it works ....

  3. #18
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    Quote Originally Posted by Kennedy View Post
    No, that part of the code appears to be correct. What is happening is that prev is stepping through the linked list as it is being built. prev = current fixes that.
    k ic but when memory is allocated now wouldnt at end of the loop prev=current; and remove the else and code would work aswell ? since we assigned ptrs then all stuff inside are = ?

  4. #19
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    Quote Originally Posted by bithub View Post
    No, they are different.

    current->next = NULL would be the same as prev->next->next = NULL.
    yes yes i c i understand it now i just didnt concentrate well on the test lol
    on first time it wont enter the tests but the perv will keep track by setting perv to current i feel so stupid lol

  5. #20
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    so basically here first time loop runs perv gets assigned to current then after that on 2nd time when loops runs it sets perv->next which sets perv->next to previous next of current right ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM