Live data from Hacker News

XOR Linked List

en.wikipedia.org

81–86 of 86 posts

Re: XOR Linked List

#81
post #73
post #60

Earlier quoted context omitted.

.. or you pass any node to a function that operates on the list. iterate up/down insert/delete etc. point being you dont have to iterate from the start of the list.

So your function needs to be passed current and next (or prev, depending on which direction you want to iterate). XOR-lists have lots of issues. This isn't one of them.

and your back to storing 2 pointers again.

Re: XOR Linked List

#82
post #69
post #60

Earlier quoted context omitted.

.. or you pass any node to a function that operates on the list. iterate up/down insert/delete etc. point being you dont have to iterate from the start of the list.

In theory yes, but how many references do you need to claim random access.

its how I use them. theres the list + some other structure/object that points/references the node, usually multiple time and the node is contained in multiple lists - big data + performance optimization

Re: XOR Linked List

#83
post #81
post #73

Earlier quoted context omitted.

So your function needs to be passed current and next (or prev, depending on which direction you want to iterate). XOR-lists have lots of issues. This isn't one of them.

and your back to storing 2 pointers again.

No you're not. Your function needs to accept two pointers. Your data structure doesn't need to store two pointers.

Unless you're storing external pointers to every node (in which case you'll actually break even with the traditional implementation in terms of size), you'll still have significant savings. Passing an extra pointer to a function is pretty trivial in terms of size.

Re: XOR Linked List

#84
post #18

Believe it or not, we actually had a C exam question where we were asked to sketch out an implementation of this. http://www.cl.cam.ac.uk/teaching/exams/pastpapers/y2010p3q6.... With classic mildly amusing exam humour introduction!

This question still haunts me.

Re: XOR Linked List

#85
post #28
post #5

Anyone actually using this is almost certainly committing a heinous premature optimization.

Why would you not just do the following? interface LinkedList class BasicLinkedList implements LinkedList class XORLinkedList implements LinkedList

XORLinkedList is most assuredly harder to debug then BasicLinkedList. Don't do it unless you hafta. In fact don't implement your own linked list at all, unless you have a specialized scenario that needs it (as proven by profiling).

Re: XOR Linked List

#86
post #74
post #28

Earlier quoted context omitted.

Why would you not just do the following? interface LinkedList class BasicLinkedList implements LinkedList class XORLinkedList implements LinkedList

LinkedList list = new XORLinkedList(Collections.fromArray(arrayList)); Element ele = list.get(4); //Ok, when we construct the list we can store the first and second pointers and traverse to the fourth Element next = ele.next(); //???

I don't understand your comment - are you saying "ele" doesn't have enough info to find the next node?

You need an Iterator type that has the address of the previous and current nodes, in addition to your existing node / Element type.

    LinkedList list = new XORLinkedList(Collections.fromArray(arrayList));
    Iterator it = list.get(4); //now you can access it.element for data from index 4
    it.next(); // now access it.element for data from index 5
Post reply on HN