Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to implement doubly linked lists

Is it possible to have a doubly linked list in Haskell, and what's the ideal solution to implementing them? I'm implementing a scene graph where every widget has a parent as well as a child, and it's beneficial to look both up and down the graph.

like image 216
Jonathan Dunlap Avatar asked Apr 30 '12 15:04

Jonathan Dunlap


People also ask

Is it possible to implement a doubly linked list?

Is it possible to create a doubly linked list using only one pointer with every node. (B) Yes, possible by storing XOR of addresses of previous and next nodes.

What is doubly linked list explain and implement it?

Doubly Linked List contains a link element called first and last. Each link carries a data field(s) and two link fields called next and prev. Each link is linked with its next link using its next link. Each link is linked with its previous link using its previous link.

How is doubly linked list implemented in stack?

Assign top and start as the new node. Otherwise, take a new node, add data to it and assign the “previous” pointer of the new node to the “top” node earlier and next as “null”. Further, update the “top” pointer to hold the value of the new node as that will be the top element of the stack now.

How is doubly linked list implemented using singly linked list?

This C Program implements doubly linked list using singly linked list. It makes use of 2 pointers, one points at the current node, other points at the head. When user requests to move back, the pointer from head travels to a previous node of the current pointer. The pointer to previous node is the resulting node.


1 Answers

It isn't really practical to have a doubly linked list in Haskell, because you have to construct it all at once.

For example, imagine that you have a list [1, 2, 3, 4, 5] that you want to make doubly linked. Now, let's imagine how the list is represented:

data DoubleList a   = LeftEnd  a (DoubleList a)   | Middle   a (DoubleList a) (DoubleList a)   | RightEnd a (DoubleList a) 

(I use two different constructors for the two ends for simplicity)

To build the list above, you have to first construct the first element:

let e1 = LeftEnd  1 ... 

But to construct the first element, you already need to have the second element:

let e1 = LeftEnd  1 e2     e2 = Middle   2 e1 ... 

And for the second element, you need the third, etc:

let e1 = LeftEnd  1 e2     e2 = Middle   2 e1 e3     e3 = Middle   3 e2 e4     e4 = Middle   4 e3 e5     e5 = RightEnd 5 e4 

This is possible to do in Haskell due to lazy evaluation; this strategy is called "tying the knot" (And you don't have to literally put it all in one let block; you can divide up the construction into functions)

But, in other words, to make a doubly-linked list, you need to construct it all at once, and if you ever want to change any part of it, you either need to use a Zipper or just make a complete copy of it every time.

I would recommend to instead use Data.Sequence, which is an optimized finger-tree based sequential storage implementation. It supports very fast insertion, deletion and iteration while still being a purely functional data structure.

Otherwise, you might want to just use Zippers, but use them for Trees instead of for Lists. More information about Zippers can be found on the Haskell Wiki. Zippers would fit very well in this situation, because they offer the exact functionality that you're after: if you visit a tree with a Zipper, you gain access to the "parents" of the part of the tree that you're visiting, but the tree itself doesn't have to contain the parent references.

like image 127
dflemstr Avatar answered Sep 21 '22 17:09

dflemstr