Everything2
Near Matches
Ignore Exact
Full Text
Everything2

Storing a doubly-linked list using just a single pointer field

created by ariels

(idea) by ariels (1.8 d) (print)   ?   (I like it!) 2 C!s Sun Apr 09 2000 at 13:47:21

Here's an incredibly ancient hack, which still isn't known well enough. A linked list is a venerable data structure; every node on the list contains a next pointer, allowing us to advance one position in the list in O(1) time. On a doubly linked list, we also keep a prev pointer, to we can go back. But we're using twice as much space for our pointers! A doubly-linked list of 1-byte chars, when every pointer is another 4-byte word, will require at least 9 bytes for every 1 byte usefully stored (and often 12 bytes, assuming today's usual memory alignment restrictions).

Can we have a doubly-linked list with just the (singly-) linked list overhead? Surely not!

It turns out that we can. On each node, keep the XOR of the next and prev pointers in the single link field. When traversing the list, keep an iterator <cur,prev>, consisting of a pointer to the current element and a pointer to the previous element. Then (abusing C notation) next = cur->link ^ prev, so we know how to advance our iterator. All of these operations are also easy:

Reversing the direction of the iterator:
Just do prev = cur->link ^ prev.
Checking for end-of-list:
Assuming we use the special address 0 to mark the ends, we just check that next->link == prev (similar expressions exist for any other sentinel value you may pick for the ends, of course).
Passing iteration starting points to a function:
Just pass an appropriately-initialised iterator pointing to the starting point and its predecessor.

And an unexpected bonus is that now all functions work equally well on the list and the reversed list!

Unfortunately, there is no portable way of implementing such a list in C or C++: neither language allows you to XOR two pointers (what could the type of the result be?), and while you could cast a pointer to a large-enough integer type, you are not guaranteed even to have such a large-enough int, and you are not guaranteed that these XORs will work. However, it appears that the C++ STL list<> templated type could be implemented in such a way, assuming sufficient cooperation between the writers of the compiler and the library. I'm not aware of any implementation which actually does this, though.


Agthorr below is not quite right. Of course if all I have is a pointer into the list, I cannot traverse it. But the thing to realise is that the correct object to store is an iterator -- a pointer to two adjacent nodes of the list (depending on the application you might also need to know the iterator's direction, ``forwards'' or ``backwards''). For this data structure, typically you'd have many nodes in the list, and only a few such iterators from outside it, so the savings remain.

(idea) by Agthorr (2.6 y) (print)   ?   (I like it!) Sun Apr 09 2000 at 19:47:23

Although this technique has a lot of hack value, it has a down side. You can't navigate the list if all you have is pointer into the middle of the list. Thus, this technique can only be used if you know that you'll only ever have to navigate the list from the beginning or the end.

Depending on what you're doing, this may or may not be acceptable.


printable version
chaos

never use variable arguments in C++ Post-breakup recovery techniques Call by value return Fibonacci Heap
XOR Problem hack value linked list doubly linked list
XOR data structure Determining if a linked list loops using only two pointers the list is not an unimportant thing
Shortcomings of Java Hack XOR swap iterator
sentinel value AZT C Template
Nulled Ada hypertension fighter plane
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
Just another sprinkling of indeterminacy
Things Are Different Now
The Great Prajna Paramita Heart Sutra
Behind the scenes at a supermarket produce department
electrical engineering
Little Anthony and the Imperials
Ancient Roman Graffiti
t.A.T.u.
Red Auerbach
Chess notation
Treblinka
Empress Theodora
To Whom It May Concern
Music's saddest time
New Writeups
Heitah
Why I love Everything2(person)
trixingee
Dungeon Mastering for the first time(idea)
Netrat0
It's Called Subtext, Honey(person)
eyeofthebeholder
The Dragon(idea)
Heitah
consist, comprise, constitute, or compose(idea)
Meezzio
Gotlandssnus(thing)
argv
Astral Plane(idea)
Madara
One Winged Angel(fiction)
Tom Rook
Talk is cheap(poetry)
shaogo
Adelle Davis(person)
Aerobe
race car g sfjsgsd(poetry)
Binah
Dream Log: July 5, 2008(dream)
StrawberryFrog
Forgotten things in space(idea)
antigravpussy
velvet revolution fairy tale(idea)
Heitah
Nerve agent VX(thing)
This page courtesy of The Everything Development Company