Thread: xor linked list

  1. #31
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
            while((cur != NULL) && (cur->data < newnode->data))
            {
                    node *next = (node *)(cur->link ^ (unsigned long)prev);
                    prev = cur;
                    cur = next;
            }
            if(cur->data >= newnode->data)
    Well, right here cur could be NULL, so when you hit your first if check, you could be dying there.

    Quzah.
    Hope is the first step on the road to disappointment.

  2. #32
    Registered User
    Join Date
    Oct 2009
    Posts
    6
    If cur is NULL it is an empty list and you want to skip that loop because it walks the list to find where to insert, right? The thing is, won't it always be NULL because the list will always be empty every time you run the program? Could a segmentation error in this instance be the result of a Null Pointer Exception?

  3. #33
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Typically, an XOR-linked list is circular, thus you never encounter the problem of running off the end.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #34
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    I am not sure how to fix this

  5. #35
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    in my main when i call insert

    should i be passing (node **)head and (node **)tail

    or just &head, &tail

  6. #36
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by quzah View Post
    It also defeats the purpose of a double linked list, because you cannot just start with any random node and do whatever you like to it. You can't just start in the middle of the list and find your way through it. You also can't just chop out a node whenever you please. Which again, completely defeats the purpose of using a double linked list.
    That's not true. How often do you start in the middle of any list anyway? You almost always have to have iterated to there at some point.
    All an XOR list means is that the iterators have to be a little more complex to achieve about the same functionality. They have to store the pointer values of two adjacent nodes, rather than just the usual one (unless there is only one item). From this iterator it is possible to iterate forwards or backwards as you please, and thus clearly allowing you to remove the item currently pointed to by the iterator since you can find the nodes either side, similar for insertion.
    All that's diferent (to the user) from a std::list, is the iterator invalidation rules. Erasing or inserting an item invalidates iterators to either side of that iterator (in addition to that iterator if erasing).

    There's a lot more to it than you realise and I really do urge you to research it a little more. Things like this still have there place in embedded (okay "really embedded") devices.
    It has no place in garbage collected languages because the pointers are obviously mutilated and would get collected erroneously.
    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"

  7. #37
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by iMalc View Post
    That's not true. How often do you start in the middle of any list anyway?
    Any time you use a function which ends up returning a node.
    Quote Originally Posted by iMalc View Post
    There's a lot more to it than you realise and I really do urge you to research it a little more. Things like this still have there place in embedded (okay "really embedded") devices.
    It has no place in garbage collected languages because the pointers are obviously mutilated and would get collected erroneously.
    Honestly I can't ever see myself using one of these, and the added complexity negates any benefit I could possibly hope to gain out of it. I'd have to be really really bored to want to implement one of these.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #38
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by quzah View Post
    Any time you use a function which ends up returning a node.
    In reality you would actually return an iterator, so the iteration is there it's just done in another function. Unless of course you were actually trying to make things difficult for yourself, or it's an intrusive list (the case you're probably talking about).
    You can throw the iterator away and only go for the thing it points to, any time you want to lose the ability to iterate over the other items in the list.
    Agreed it's more complex and usually not worth using.
    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. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM