Thread: Linked Lists

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    61

    Linked Lists

    I was wondering how to find the previous node in a singly, circular linked list.

    In this picture I have 4 nodes.
    http://i.imgur.com/hlvzT.png

    If I wanted to get rid of the node Sarah, I would have to save it in a temporary struct before I removed it so I wouldn't lose the address of Dave. But how would I access the node David so I could assign the address of Dave into it?

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    25
    I was wondering how to find the previous node in a singly, circular linked list.

    you probably need to go full circle to find out what it is.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    61
    well it didn't work out the way i had hoped. I'll just ask my professor.
    Last edited by november1992; 09-21-2012 at 06:07 PM.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by november1992 View Post
    I was wondering how to find the previous node in a singly, circular linked list.

    In this picture I have 4 nodes.
    http://i.imgur.com/hlvzT.png

    If I wanted to get rid of the node Sarah, I would have to save it in a temporary struct before I removed it so I wouldn't lose the address of Dave. But how would I access the node David so I could assign the address of Dave into it?
    You can keep track of the previous node,by having an extra pointer named prev

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    61
    That would be easier, but it was supposed to be singly linked not doubly.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    In the snippet below, curr and prev are pointer to the nodes.

    Code:
    curr = prev = 0x250; /* pointer to Sarah */
    do prev = prev->next; while (prev->next != curr); /* find prev */
    
    /* curr now points to Sarah; prev points to David; curr->next points to Dave */
    prev->next = curr->next; /* get Sarah off the list */
    free(curr); /* release resources for Sarah */
    Last edited by qny; 09-22-2012 at 03:17 AM. Reason: added what curr->next points to

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Quote Originally Posted by qny View Post
    Code:
    free(curr); /* release resources for Sarah */
    Don't forget to reset any remaining dangling pointers!

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by november1992 View Post
    That would be easier, but it was supposed to be singly linked not doubly.
    Then you can't get to the previous node without starting from some node earlier in the list and traversing up to the desired node.

    While it's great that you will or did go and ask your professor, and in fact we sometimes have to push people to do so, primarily when the requirements are unclear; there is no reason to think that they know any better than some of the better programmers on here. In fact the knowledge on here is generally a lot more up-to-date.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 01:07 PM
  2. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  3. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 02:49 AM
  4. question on linked lists(stack with linked lists)
    By dionys in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 11:08 AM
  5. Linked List of Linked lists Revisited.
    By Qui in forum C++ Programming
    Replies: 11
    Last Post: 04-11-2004, 09:45 PM