Thread: One question on linked list need some ideas to solve it...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    54

    One question on linked list need some ideas to solve it...

    Hai friends i have one question on single linked list . . .
    You consider that there are n nodes in the single linked list and now the address pointer of one node in the linked list is given to you it could be any nodes not first ....And you want to delete that node....
    How we can do this please give some idea . I had tryed many things it was not possible...

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sure, to delete a node in the middle, you need to find the node before it, and set the next pointer of that node to the next pointer of the "deletee".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    Tell me that it is possible or not...???

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Or perhaps some rewording just in case.
    To make a node disappear from a list you need to delink it. Make a next pointer of the previous node point to the node after the old value. If you have a previous link, do the same thing working the other direction.

    Make sure that something points to the old value though, so that you can destroy or free() it properly.

    It's actually very simple and possible. Just try writing something, mats and I will see if it's right.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    In this there is only one node address is given we dont know the prev node . . . Address . . . . How to em this ....

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sreeramu View Post
    In this there is only one node address is given we dont know the prev node . . . Address . . . . How to em this ....
    So, you have to search the list until you find the node you want, and whilst you are walking the list, you make note of the "previous" node you walked.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If it's a linked list, then it will have members that point to the next in the list (and possible to the previous, as well).
    But it all depends on the implentation, and how the list looks like, since arguably, not all lists look alike.
    Last edited by Elysia; 12-06-2007 at 08:03 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    If it's a linked list, then it will have members that point to the next in the list (and possible to the previous, as well).
    So here's a sample of how it could be done:

    Code:
    MyLinkedList* p;
    MyLinkedList* pNext = p->pNext;
    MyLinkedList* pPrev = p->pPrevious;
    pPrevious->pNext = pNext;
    pNext->pPrevious = pPrev;
    free(p);
    p = NULL;
    But it all depends on the implentation, and how the list looks like, since arguably, not all lists look alike.
    Ehm, the original post says
    i have one question on single linked list
    . This I take to mean isn't a double linked list, so it only has a next-pointer. Which means that you need to, as I stated above, search the list for the node.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Alright, well I didn't know the exact difference between a single and double linked list. I find a double more efficient. Anyway, basic code would then be:

    Code:
    /* We assume p is pointing to the object you want to delete */
    /*MyLinkedList* p;*/
    MyLinkedList* pNext = p->pNext;
    MyLinkedList* pPrev = NULL;
    /* If we assume pFirst is a pointer to the first object in the list */
    MyLinkedList* pTemp = pFirst;
    /* Walk the tree until we find the previous node */
    for (; pTemp->pNext != p; pTemp = pTemp->pNext);
    pPrev = pTemp;
    /* Set the next node to the node after the node we want to delete */
    pPrev->pNext = pNext;
    free(p);
    p = NULL;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Location
    Bangalore, India
    Posts
    24
    I think he means that only a lone [address of a] node is given. Provided it's a singly linked list and the head is not known deleting that node while keeping rest of the list intact wouldn't be possible.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by pankaj401 View Post
    I think he means that only a lone [address of a] node is given. Provided it's a singly linked list and the head is not known deleting that node while keeping rest of the list intact wouldn't be possible.
    Not in the way you're used to perhaps, but with a few modifications to your iteration routines you can perform lazy-deletion. Simply take the address given and remember it somewhere. Then modify your iteration code to check for and automatically remove and items that have been remembered as needing to be deleted.

    This can be done intrusively by setting a kill-bit inside the node itself. Some self-balancing binary tree types use this kind of lazy-deletion approach.
    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"

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    Hai friends my question is on single linked list . . . So you consider the the pointer of the 3rd node of the linked list is given and we can travel to next only we cant travel to back so we came find the 2nd node pointer . . . So i think that it is not possible to do this problem....

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Presumably, the function that removes a node is also passed the list from which to remove it to. With that you just iterate through the list, until the next value's next value points to the passed node.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    Any one give the solution or tell that it is possible are not...

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by sreeramu View Post
    Hai friends my question is on single linked list . . . So you consider the the pointer of the 3rd node of the linked list is given and we can travel to next only we cant travel to back so we came find the 2nd node pointer . . . So i think that it is not possible to do this problem....
    Code:
    +-----+     +-----+     +-----+     +-----+
    |  1  |---->|  2  |---->|  3  |---->|  4  |---->NULL
    |_____|     |_____|     |_____|     |_____|
    So this is your linked list. If you wanted to delete the third node, you need to find and set a pointer to the node previous to that item. All you have to do is use a loop, and keep walking the list until you find the next pointer to the node you want to delete. So it might look like this when you're finished:

    Code:
             node * prev    node * todelete
                   v____|       v___|   
    +-----+     +-----+     +-----+     +-----+
    |  1  |---->|  2  |---->|  3  |---->|  4  |---->NULL
    |_____|     |_____|_    |_____|    >|_____|
                        \_____________/  
                      
                  prev->next = todelete->next
    If you can read my crazy ascii art that should make sense. Like I said before, unlinking a list node means that you set the next pointer of a previous node to point to the node _after_ the old node.

    Just do something like

    prev->next = todelete->next;

    Then the node is successfully unlinked, and you can call free( todelete ); or something, if you like. Like I said, very simple. Make sure that you write secure code though! Don't dereference a NULL pointer.
    Last edited by whiteflags; 12-06-2007 at 11:20 PM. Reason: With color!

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. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  5. Array, Linked List, or Binary Tree?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 01-05-2002, 10:07 PM